enumerator python code example

Example 1: enumerate in python

languages = ['Python', 'C', 'C++', 'C#', 'Java']

#Bad way
i = 0 #counter variable
for language in languages:
    print(i, language)
    i+=1

#Good Way
for i, language in enumerate(languages):
    print(i, language)

Example 2: enumerate python

for index,subj in enumerate(subjects):
    print(index,subj) ## enumerate will fetch the index
    
0 Statistics
1 Artificial intelligence
2 Biology
3 Commerce
4 Science
5 Maths

Example 3: enum in python

import enum
# Using enum class create enumerations
class Days(enum.Enum):
   Sun = 1
   Mon = 2
   Tue = 3
# print the enum member as a string
print ("The enum member as a string is : ",end="")
print (Days.Mon)

# print the enum member as a repr
print ("he enum member as a repr is : ",end="")
print (repr(Days.Sun))

# Check type of enum member
print ("The type of enum member is : ",end ="")
print (type(Days.Mon))

# print name of enum member
print ("The name of enum member is : ",end ="")
print (Days.Tue.name)

Example 4: enumeration python

from enum import Enum

class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

print(Color.RED)

class Shake(Enum):
	VANILLA = 7
    CHOCOLATE = 4
    COOKIES = 9
    MINT = 3