Python for Loop with range() Explained
In Python, you can use a for loop and the range() function to iterate over a range of integer sequences. The range() function can generate an integer sequence with the following syntax:
range(start, stop, step)
start refers to the initial value of the sequence (default is 0), stop refers to the end value of the sequence (not included in the sequence), and step represents the interval between each value in the sequence (default is 1). For example:
for i in range(5):
print(i)
This code will output 0, 1, 2, 3, 4 because range(5) generates a sequence of integers from 0 to 4. If you want to specify a starting value and a step size, you can pass in the corresponding parameters, for example:
for i in range(1, 10, 2):
print(i)
This code will output 1, 3, 5, 7, 9 because range(1, 10, 2) generates a sequence of integers from 1 to 9 with a step size of 2.