网站首页 网站地图
网站首页 > 游戏秘籍 > for i in range

for i in range

时间:2026-04-01 20:25:56

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 from 0 to 4 (inclusive).
  • The loop will print 0, 1, 2, 3, and 4.

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 in for loops, 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!