Last updated: Apr 10, 2024
Reading timeยท5 min
step
with numpy
for
loopwhile
loopTo create a list of numbers from 1 to N:
range()
class to create a range
object from 1 to N.list()
class to convert the range
object to a list.list_of_numbers = list(range(1, 6)) print(list_of_numbers) # ๐๏ธ [1, 2, 3, 4, 5] list_of_numbers = list(range(1, 10, 2)) print(list_of_numbers) # ๐๏ธ [1, 3, 5, 7, 9]
The range() class is commonly used for looping
a specific number of times in for
loops and takes the following arguments:
Name | Description |
---|---|
start | An integer representing the start of the range (defaults to 0 ) |
stop | Go up to, but not including the provided integer |
step | Range will consist of every N numbers from start to stop (defaults to 1 ) |
If you only pass a single argument to the range()
constructor, it is
considered to be the value for the stop
parameter.
list_of_numbers = list(range(5)) # ๐๏ธ [0, 1, 2, 3, 4] print(list_of_numbers)
start
argument is omitted, it defaults to 0
and if the step
argument is omitted, it defaults to 1
.If values for the start
and stop
parameters are provided, the start
value
is inclusive, whereas the stop
value is exclusive.
list_of_numbers = list(range(1, 5)) # ๐๏ธ [1, 2, 3, 4] print(list_of_numbers)
If you need to specify a step, pass a third argument to the range()
class.
list_of_numbers = list(range(1, 10, 2)) print(list_of_numbers) # ๐๏ธ [1, 3, 5, 7, 9]
The list contains every second number starting from 1
to 10
.
The step
argument can only be set to an integer.
You can also use a list comprehension to create a list of numbers from 1 to N.
start = 1 stop = 5 step = 0.5 list_of_numbers = [ x * step for x in range(2*start, 2*stop + 1) ] # ๐๏ธ [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0] print(list_of_numbers)
The list in the example starts at 1 and goes to 5 (inclusive) in increments of
0.5
.
You can update the start
, stop
and setup
values.
This approach is useful when you have a step
value that is not an integer
because the range()
class only supports integer values.
step
with numpy
If you need to use a floating-point number for the step
, use the
numpy.arange()
method.
import numpy as np list_of_numbers = np.arange(1, 10, 0.5).tolist() # ๐๏ธ [1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, # 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5] print(list_of_numbers)
The numpy.arange() method returns an array of evenly spaced values within the given interval.
We used the tolist() method to convert the array to a list.
If you need to install the NumPy package, run the following command.
pip install numpy # ๐๏ธ or pip3 pip3 install numpy
You can omit the step
argument to get a list of values from 1 to N in
increments of 1.
import numpy as np list_of_numbers = np.arange(1, 11).tolist() # ๐๏ธ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(list_of_numbers)
The numpy.arange()
method is very similar to the range()
class but allows
for step
values that are floating-point numbers.
You can also use a list comprehension to create a list of numbers from 1 to N.
list_of_numbers = [number for number in range(1, 6)] print(list_of_numbers) # ๐๏ธ [1, 2, 3, 4, 5]
On each iteration, we return the current number.
The new list contains the numbers in the range
object.
However, this isn't necessary unless you need to perform some operation for
every number in the range
object.
for
loopThe same can be achieved using a simple for
loop.
list_of_numbers = [] for number in range(1, 6): list_of_numbers.append(number) print(list_of_numbers) # ๐๏ธ [1, 2, 3, 4, 5]
We used a for
loop to iterate over the range
object.
On each iteration, we use the list.append()
method to append the current
number to a list.
The list.append() method adds an item to the end of the list.
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
Using the list() class to convert the
range
object to a list should be sufficient unless you have to perform some
operation for every number in the range
object.
You can also define a reusable function that creates a list of numbers in a
for
loop.
def get_range(n): list_of_numbers = [] for number in range(1, n + 1): list_of_numbers.append(number) return list_of_numbers print(get_range(6)) # ๐๏ธ [1, 2, 3, 4, 5, 6] print(get_range(7)) # ๐๏ธ [1, 2, 3, 4, 5, 6, 7]
The function takes n
as a parameter and creates a list of numbers from 1 to n.
while
loopYou can also use a while
loop to create a list of numbers from 1 to N.
def get_range(n): list_of_numbers = [] start = 1 while start < n: list_of_numbers.append(start) start += 1 return list_of_numbers print(get_range(6)) # ๐๏ธ [1, 2, 3, 4, 5] print(get_range(7)) # ๐๏ธ [1, 2, 3, 4, 5, 6]
The get_range
function takes n
as a parameter and creates a list of numbers
from 1 to n
.
On each iteration, we append the current start
value to the list and increment
it.
Once the start
value is equal to or greater than n
, the condition is no
longer met and the while
loop exits.
You can learn more about the related topics by checking out the following tutorials: