The `range()` function is used to generate a sequence of numbers. It is often used in loops to iterate over a specific range of values. Let's see an example:
for num in range(5): print(num)
0 1 2 3 4
In this example, we use the `range()` function to generate a sequence of numbers from `0` to `4`. The `range(5)` generates numbers starting from `0` up to, but not including, `5`. The `for` loop then iterates over each number in the sequence and prints it. The output of this code will be: ``` 0 1 2 3 4 ``` You can also specify a starting value and an ending value for the range. For example:
for num in range(2, 8): print(num)
2 3 4 5 6 7
In this code, we use `range(2, 8)` to generate numbers starting from `2` up to, but not including, `8`. The output of this code will be: ``` 2 3 4 5 6 7 ``` You can also specify a step value to skip numbers in the sequence. For example:
for num in range(1, 10, 2): print(num)
1 3 5 7 9
In this code, we use `range(1, 10, 2)` to generate numbers starting from `1` up to, but not including, `10`, with a step of `2`. The output of this code will be: ``` 1 3 5 7 9 ``` The `range()` function is very useful when you want to repeat a certain action a specific number of times or iterate over a range of values in a loop. In addition to using the built-in functions like `range`, we can also [import](/tutorials/import) modules from the Python standard library to add more functionality to our programs.