Concept: range()
The range function is a built-in Python function that generates a sequence of numbers. It is commonly used in for loops to iterate over a specific range of values. The range() function can take one, two, or three arguments:
When called with a single argument, it generates numbers from up to (but not including) the specified value.
for i in range(5):
print(i, end=" ")
# Output: 0 1 2 3 4When called with two arguments, it generates numbers from the first argument up to (but not including) the second argument.
for i in range(1, 5):
print(i, end=" ")
# Output: 1 2 3 4When called with three arguments, it generates numbers from the first argument up to (but not including) the second argument, incrementing by the third argument.
for i in range(1, 5, 2):
print(i, end=" ")
# Output: 1 3While the examples above used positive argmument, the range() function can also accept negative arguments, allowing for reverse iteration.
for i in range(5, 0, -1):
print(i, end=" ")
# Output: 5 4 3 2 1