Python range allows you to create individual number sequences. Using the function is particularly convenient in combination with loops for Python.
Overview of Python range application fields¶
In principle, the range function is used to create a number sequence that follows a specific pattern. As the function is above all used in conjunction with for loops, it is explained in any Python tutorial. Python range makes it easy to specify the range in which you want to run a block of code using a single function call.
Using Python is also great for web projects. If you’re starting your own web project, using IONOS’ Deploy Now can pay off: automatically deploy all your advancements via GitHub and always stay in control of your project!
Python range syntax¶
The syntactic structure of the range function in Python does not present any difficulty. The function accepts up to three transfer parameters and returns you a sequence of numbers as a result.
range(start, stop, step)
python
There is no need to specify all the parameters when calling the Python range function. Only the stop parameter is mandatory. You use this to set the end value. Keep in mind, however, that the specified value is not included in the returned number sequence :
for i in range(6):
print(i)
python
The preceding code example therefore returns the sequence of numbers “0, 1, 2, 3, 4, 5” to the screen.
THE start parameter is optional and sets a start value from which your enumeration begins. If you don’t specify your own start value, Python’s range number sequence starts at the value 0, as you can see in the sample code above. For example, if you want to start from the value three, your code might look like this:
for i in range(3, 6):
print(i)
python
The sequence of numbers « 3, 4, 5 » is now displayed on the screen.
THE step parameter is not required if you want to use Python range function in your code. You can use this parameter to more precisely determine the increment of your sequence of numbers. The default value used if you do not specify a custom value is 1. The following code allows you to use the step parameter, for example to display the three numbers on the screen:
for i in range(2, 10, 3):
print(i)
python
Starting with the number 2, which is used as the start parameter, the three numbers are now output up to 10. The sequence of numbers returned is: « 2, 5, 8 »
Usage example: display all even numbers below 100¶
The range function makes it easier to output certain mathematical sequences. The sequence of even numbers is an example. To do this, simply start with the number 0. As mentioned earlier, this is the default value for the start parameter. However, in order for the Python interpreter to identify the passed number to be interpreted as a start, stop, or step value, you must specify all three values when using the step parameter.
Select the number 100 as the value of the stop parameter to display all numbers below 100. To display only even numbers on the screen, you can set the step parameter to 2. So only every other number is output from 0.
for i in range(0, 100, 2):
print(i)
python
This simple code snippet now displays all even numbers that are less than 100 on the screen.
Decreasing number sequences with Python range¶
The range function also allows you to set descending number sequences. Here you have to make sure that the start value is greater than the stop value. Just specify negative values in the step parameter to indicate that you don’t want to count in ascending order, but in descending order. A sample code gives us more clarity:
for i in range(10, 0, -1):
print(i)
python
This sample code returns the following number sequence: « 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ».