World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITian's, only for AI Learners.
How to create and use Enum data type in python?
pip install enum34
Creating an enum :
from enum import Enum class Color(Enum): red = 1 green = 2 blue = 3 print(Color.red) # Color.red print(Color(1)) # Color.red print(Color['red']) # Color.red
Enum Iteration :
Enums are iterable:
class Color(Enum): red = 1 green = 2 blue = 3 [c for c in Color] # [<Color.red: 1>, <Color.green: 2>, <Color.blue: 3>]