❮ Built-in Functions
Example
Create a sequence of numbers from 0 to 5, and print each item in the sequence:
x = range(6)
for n in x:
print(n)
Try it Yourself »
Definition and Usage
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
Syntax
Parameter Values
start | Optional. An integer number specifying at which position to start. Default is 0 |
stop | Required. An integer number specifying at which position to stop (not included). |
step | Optional. An integer number specifying the incrementation. Default is 1 |
More Examples
Example
Create a sequence of numbers from 3 to 5, and print each item in the sequence:
x = range(3, 6)
for n in x:
print(n)
Try it Yourself »
Example
Create a sequence of numbers from 3 to 19, but increment by 2 instead of 1:
x = range(3, 20, 2)
for n in x:
print(n)
Try it Yourself »
❮ Built-in Functions
The range() function returns a sequence of numbers between the give range.
Example
# create a sequence of numbers from 0 to 3 numbers = range(4) # iterating through the sequence of numbers for i in numbers: print(i) # Output: # 0 # 1 # 2 # 3Note: range() returns an immutable sequence of numbers that can be easily converted to lists, tuples, sets etc.
Syntax of range()
The range() function can take a maximum of three arguments:
range(start, stop, step)The start and step parameters in range() are optional.
Now, let's see how range() works with different number of arguments.
Example 1: range() with Stop Argument
If we pass a single argument to range(), it means we are passing the stop argument.
In this case, range() returns a sequence of numbers starting from 0 up to the number (but not including the number).
# numbers from 0 to 3 (4 is not included) numbers = range(4) print(list(numbers)) # [0, 1, 2, 3] # if 0 or negative number is passed, we get an empty sequence numbers = range(-4) print(list(numbers)) # []Example 2: range() with Start and Stop Arguments
If we pass two arguments to range(), it means we are passing start and stop arguments.
In this case, range() returns a sequence of numbers starting from start (inclusive) up to stop (exclusive).
# numbers from 2 to 4 (5 is not included) numbers = range(2, 5) print(list(numbers)) # [2, 3, 4] # numbers from -2 to 3 (4 is not included) numbers = range(-2, 4) print(list(numbers)) # [-2, -1, 0, 1, 2, 3] # returns an empty sequence of numbers numbers = range(4, 2) print(list(numbers)) # []Example 3: range() with Start, Stop and Step Arguments
If we pass all three arguments,
- the first argument is start
- the second argument is stop
- the third argument is step
The step argument specifies the incrementation between two numbers in the sequence.
# numbers from 2 to 10 with increment 3 between numbers numbers = range(2, 10, 3) print(list(numbers)) # [2, 5, 8] # numbers from 4 to -1 with increment of -1 numbers = range(4, -1, -1) print(list(numbers)) # [4, 3, 2, 1, 0] # numbers from 1 to 4 with increment of 1 # range(0, 5, 1) is equivalent to range(5) numbers = range(0, 5, 1) print(list(numbers)) # [0, 1, 2, 3, 4]Note: The default value of start is 0, and the default value of step is 1. That's why range(0, 5, 1) is equivalent to range(5).
range() in for Loop
The range() function is commonly used in a for loop to iterate the loop a certain number of times. For example,
# iterate the loop 5 times for i in range(5): print(i, 'Hello') 0 Hello 1 Hello 2 Hello 3 Hello 4 HelloLearn about the Python Range() function and its capabilities
Photo by Kay on UnsplashIntroduction
The range() is an in-built function in Python. It returns a sequence of numbers starting from zero and increment by 1 by default and stops before the given number.
Now that we know the definition of range, let’s see the syntax:
range(start, stop, step)It has three parameters, in which two are optional:
- start: It’s an optional parameter used to define the starting point of the sequence. By default, it’s zero.
- stop: It’s a mandatory parameter, used to define the stopping point of the sequence
- step: It’s also an optional parameter used to specify the incrementation on each iteration; by default, the value is one.
General Usage
As it returns a sequence of numbers, most of the developers use the range to write loops. This comes handy when you don’t have a list or tuple, but only a specific value to implement the loop.
Variation One
Here we will implement a for loop with only one parameter — stop value.
Here x is the range that we used to implement the loop, and n is the value in each iteration. Observe that the output ends before the stop value; it is never a part of the range iteration similar to list.size().
Variation Two
Here we will implement the for loop using start and stop as parameters.
Variation Three
Now, we will use all three parameters: start, stop and step. Have a look:
As the step value is 2, instead of incrementing by 1, the loop increment by 2 on each iteration. One of the important things that we need to keep in mind is the step value should never be zero; else, It will throw a ValueError exception.
Iteration over list types
Along with loops, range() is also used to iterate over the list types using the len function and access the values through the index. Have a look:
Reverse Range
We can give either positive or negative numbers for any of the parameters in the range. This feature offers the opportunity to implement reverse loops. We can do this by passing a higher index as a start and a negative step value. Have a look:
Create List, Set and Tuple using Range
range() comes handy in many situations, rather than only using to write loops. For example, we create List, Set, and Tuple using range function instead of using loops to avoid boilerplate code. Have a look:
To make it a bit more fun, we can pass negative values in step to create ascending order lists. Have a look:
Indexing Range
Like the way we access values in the list using the index, we can do the same for range. The syntax is also similar to list index access.
Float Arguments in Range
By default, the range() function only allow integers as parameters. If you pass flow value, then it throws the following error:
TypeError: 'float' object cannot be interpreted as an integerBut there is a workaround for this; we can write a custom Python function similar to the one below. It will allow you to specify a float value for the step argument.
That is all for now, hope you learned something useful, thanks for reading.
You can find me on Medium, Twitter, Quora and LinkedIn.