- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create list of numbers with given range in Python
Python can handle any requirement in data manipulation through its wide variety of libraries and methods. When we need to generate all the numbers between a pair of given numbers, we can use python’s inbuilt functions as well as some of the libraries. This article describes such approaches.
Using range
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 ending at a specified number. We can of curse change the starting, ending as well as increment steps to suit our need.
Example
def getnums(s, e,i): return list(range(s, e,i)) # Driver Code start, end, intval = -3, 6,2 print(getnums(start, end,intval))
Output
Running the above code gives us the following result −
[-3, -1, 1, 3, 5]
Using randrange
The random module can also generate a random number between in a similar way as above. It involves calling the randrange method and supplying the parameters for start, end and interval values.
Example
import random def getnums(s, e,i): return (random.randrange(s, e,i)) # Driver Code start, end, intval = 3, 16,2 print(getnums(start, end,intval))
Output
Running the above code gives us the following result −
7
With numpy.arrange
The numpy library also provides a very wide range of functions for these requirements. We use arrange function which will also take the required parameters and give the output as a list.
Example
import numpy as np def getnums(s, e,i): return (np.arange(s, e,i)) # Driver Code start, end, intval = 3, 16,2 print(getnums(start, end,intval))
Output
Running the above code gives us the following result −
[ 3 5 7 9 11 13 15]
- Related Articles
- Python Generate random numbers within a given range and store in a list
- Program to find bitwise AND of range of numbers in given range in Python
- Java Program to create an array with randomly shuffled numbers in a given range
- Python program to generate random numbers within a given range and store in a list?
- Python - Find the number of prime numbers within a given range of numbers
- Find missing numbers in a sorted list range in Python
- Python Program to replace list elements within a range with a given number
- Count numbers with unit digit k in given range in C++
- Python - Largest number possible from list of given numbers
- Python – Display the key of list value with maximum range
- Find numbers with K odd divisors in a given range in C++
- Python program to extract characters in given range from a string list
- C++ Program to find Numbers in a Range with Given Digital Root
- C++ code to get two numbers in range x with given rules
- Program to find out the number of special numbers in a given range in Python
