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
1import enum
2# Using enum class create enumerations
3class Days(enum.Enum):
4 Sun = 1
5 Mon = 2
6 Tue = 3
7# print the enum member as a string
8print ("The enum member as a string is : ",end="")
9print (Days.Mon)
10
11# print the enum member as a repr
12print ("he enum member as a repr is : ",end="")
13print (repr(Days.Sun))
14
15# Check type of enum member
16print ("The type of enum member is : ",end ="")
17print (type(Days.Mon))
18
19# print name of enum member
20print ("The name of enum member is : ",end ="")
21print (Days.Tue.name)
1for index,char in enumerate("abcdef"):
2 print("{}-->{}".format(index,char))
3
40-->a
51-->b
62-->c
73-->d
84-->e
95-->f
1from enum import Enum
2
3class Color(Enum):
4 RED = 1
5 GREEN = 2
6 BLUE = 3
7
8print(Color.RED)
9
10class Shake(Enum):
11 VANILLA = 7
12 CHOCOLATE = 4
13 COOKIES = 9
14 MINT = 3
1enumerate(iterable, start=0)
2
3Parameters:
4Iterable: any object that supports iteration
5Start: the index value from which the counter is
6 to be started, by default it is 0