Code Explanation:
๐น 1. Importing the Enum Class
from enum import Enum
Explanation
Imports the Enum class from Python's built-in enum module.
Enum is used to create a collection of named constant values.
It improves code readability and reduces the use of magic numbers.
๐น 2. Creating an Enum Class
class Day(Enum):
Explanation
Defines a new Enum class named Day.
Day inherits from the Enum class.
All members inside this class become Enum members.
๐น 3. Creating the First Enum Member
MON = 1
Explanation
Creates an Enum member named MON.
Assigns it the value 1.
Represents Monday.
๐น 4. Creating the Second Enum Member
TUE = 2
Explanation
Creates another Enum member named TUE.
Assigns it the value 2.
Represents Tuesday.
๐น 5. Comparing Enum Members
print(Day.MON == Day.TUE)
Explanation
Compares two Enum members:
Day.MON
Day.TUE
Since they are different members, the comparison returns False.
Output
False
๐น 6. Accessing the Enum Value
print(Day.MON.value)
Explanation
.value retrieves the actual value assigned to the Enum member.
Day.MON.value returns 1.
Output
1
๐น 7. Program Output
False
1

0 Comments:
Post a Comment