The for i in range() statement in Python is used to iterate over a sequence of numbers (like a list, tuple, or range object). Here's a basic example:
for i in range(5):
print(i)
Explanation:
range(5)generates numbers from0to4(inclusive).- The loop will print
0,1,2,3, and4.
Example with a list:
numbers = [1, 2, 3, 4, 5]
for i in range(len(numbers)):
print(numbers[i])
Range vs. range():
range()returns a range object (not a list), which is memory-efficient.range()is used inforloops, not in list comprehensions.
Example with range() and range() in a list:
for i in range(10):
print(f"Number: {i}")
Let me know if you'd like to see more examples or use it in a different context!