1languages = ['Python', 'C', 'C++', 'C#', 'Java']
2
3#Bad way
4i = 0 #counter variable
5for language in languages:
6 print(i, language)
7 i+=1
8
9#Good Way
10for i, language in enumerate(languages):
11 print(i, language)
12
1for index,char in enumerate("abcdef"):
2 print("{}-->{}".format(index,char))
3
40-->a
51-->b
62-->c
73-->d
84-->e
95-->f
1grocery = ['bread', 'milk', 'butter']
2
3for item in enumerate(grocery):
4 print(item)
5''' output
6(0, 'bread')
7(1, 'milk')
8(2, 'butter')
9'''
10
11for count, item in enumerate(grocery):
12 print(count, item)
13''' output
140 bread
151 milk
162 butter
17'''
18
19# changing default start value
20for count, item in enumerate(grocery, 100):
21 print(count, item)
22''' output
23100 bread
24101 milk
25102 butter
26'''