python range
时间: 2023-11-11 13:06:10 浏览: 70
The `range()` function in Python is used to generate a sequence of numbers. It takes three parameters: `start`, `stop`, and `step`. The `start` parameter is the starting number of the sequence, `stop` is the last number of the sequence (not included), and `step` is the increment between the numbers in the sequence.
Here is the syntax for the `range()` function:
```
range(start, stop, step)
```
For example, `range(0, 10, 2)` will generate the sequence of even numbers from 0 to 8 (not including 10), with a step of 2 between each number.
The `range()` function can also be used with just one parameter, which represents the `stop` value. In this case, the `start` value defaults to 0 and the `step` value defaults to 1. For example, `range(5)` will generate the sequence of numbers from 0 to 4.
The `range()` function returns a range object, which can be converted to a list using the `list()` function.
Here is an example of using the `range()` function:
```
for i in range(0, 10, 2):
print(i)
```
This will output:
```
0
2
4
6
8
```
阅读全文