The range() function in Python is used to generate a sequence of numbers. It is a built-in function that returns an object of range type, which represents a sequence of numbers from a starting value to an ending value with a given step.
The syntax of the range() function is:
range(start, stop[, step])
where:
- start (optional): The starting value of the sequence. The default value is 0.
- stop: The ending value of the sequence. This value is not included in the sequence.
- step (optional): The step value of the sequence. The default value is 1.
# Example 1: Generate a sequence of numbers from 0 to 9 for i in range(10): print(i) # Example 2: Generate a sequence of even numbers from 0 to 20 for i in range(0, 21, 2): print(i) # Example 3: Generate a sequence of odd numbers from 1 to 19 for i in range(1, 20, 2): print(i)
In the above examples, the range() function is used to generate a sequence of numbers and the for loop is used to iterate over the sequence and print each value.
In summary, the range() function is a useful built-in Python function for generating a sequence of numbers with a given start, stop, and step.
0 Comments