for i in range array python code example
Example 1: python how to get index in for loop
index = 0
foods = ["burger", "pizza", "apple", "donut", "coconut"]
for food in foods:
print("Food", index, "is", food)
index += 1
foods = ["burger", "pizza", "apple", "donut", "coconut"]
for index, value in enumerate(foods):
print("Food", index, "is", value)
Example 2: how to iterate through range in python
for i in range(start, end):
dosomething()
Example 3: for i in range python
for item in range(10):
print(item)
Example 4: python for loop index
for num, line in enumerate(lines):
print("{0:03d}: {}".format(num, line))
Example 5: python range of array
>>> new_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(new_list[5:9])
[6, 7, 8, 9]
Example 6: for i in range python
for i in range(start, end):
expression