1items=['baseball','basketball','football']
2for index, item in enumerate(items):
3 print(index, item)
1# There are two ways to do this
2# The "beginner" one:
3index = 0
4foods = ["burger", "pizza", "apple", "donut", "coconut"]
5for food in foods:
6 print("Food", index, "is", food)
7 index += 1
8
9# Or using enumerate:
10foods = ["burger", "pizza", "apple", "donut", "coconut"]
11for index, value in enumerate(foods):
12 print("Food", index, "is", value)
13
14# By convention, you should understand and use the enumerate function as it makes the code look much cleaner.
1presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe", "Adams", "Jackson"]
2for num, name in enumerate(presidents, start=1):
3 print("President {}: {}".format(num, name))
4
1presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe", "Adams", "Jackson"]
2for i in range(len(presidents)):
3 print("President {}: {}".format(i + 1, presidents[i]))
4