An enum defines a fixed set of named members for representing a limited set of meaningful states or choices.
Convert external values at the boundary and use members consistently inside the program.
A misspelled status string can quietly skip the next step of a workflow. An enum gives the permitted states explicit names and identities. Learn how those members differ from their raw values, how external text becomes a member, and why a set of valid states still needs a separate rule for transitions.

Order status without enum
A delivery app tracks each order's status as a string. It works — until one line checks the status against a misspelled string:
Nothing crashes. The archive step just silently never runs, and the bug could hide for months. An Enum turns the same typo into an error at the exact line that has it:
from enum import Enum
class Status(Enum):
PENDING = "pending"
DONE = "done"
FAILED = "failed"
print(Status.DNOE)Two things worth knowing up front: an Enum member has both a .name and a .value,
and it does not equal its raw value by default:
Preparing this topic’s interactive investigation…
Convert at the boundary, then use one representation
An API payload is text; internal decisions can use enum members. Convert once where the text enters, so the rest of the program does not repeatedly guess whether it received a string or a member.
Status("done") accepts the external value. Status["DONE"] looks up the
member name; an unknown name raises KeyError instead. These two interfaces
are intentionally different. Serialise .value when that is the external
contract, and keep explicit values stable when other systems store them.
An enum limits the set of states but does not define legal transitions.
DONE → PENDING still needs a rule. For a job workflow, draw allowed arrows
and encode a transition mapping; reject an absent arrow instead of treating
every enum member as reachable from every other member.
Transfer the traffic-light exercise to a job with PENDING, RUNNING, and
DONE: specify which transition is allowed and which should fail. For numeric
interoperability, use IntEnum deliberately: members can compare equal to
ordinary integers, and arithmetic produces an integer rather than preserving
membership. Ordinary Enum keeps that boundary stronger.
Traffic light states
A traffic light only ever has three states, and nobody wants to remember whether
1 means red or green. auto() hands out the numbers so you never have to pick
them by hand:
For this ordinary Enum with default generation, auto() counts from 1 in definition order — reordering the lines changes the
numbers, so never rely on the exact value if you used auto(), only on the name.
Priority levels that sort
A plain Enum gives you a fixed set, but not an order — two members can't be
compared with < unless you ask for that specifically:
from enum import Enum
class Priority(Enum):
LOW = 1
MEDIUM = 2
HIGH = 3
print(Priority.LOW < Priority.HIGH)IntEnum says so: a member is an int underneath, so ordering, sorting and even
comparing to a plain number all just work:
Quick practice
from enum import Enum
class Suit(Enum):
CLUBS = 1
HEARTS = 2
print(Suit.CLUBS.value == 1, Suit(1) == Suit.CLUBS)from enum import Enum
class Priority(Enum):
LOW = 1
MEDIUM = 2
HIGH = 3
print([p.name for p in Priority])The function is supposed to work with either a string or a Status member. Right now only one of the two calls comes back True. Fix the comparison so both do.
🎯 Your turn
Write next_light(name). name is "RED", "GREEN" or "YELLOW" — the light's
current state. Return the name of the next state in the cycle
RED → GREEN → YELLOW → RED.
next_light("RED")→"GREEN"next_light("YELLOW")→"RED"
Hint: define class Light(Enum): RED = 1; GREEN = 2; YELLOW = 3, keep
ORDER = [Light.RED, Light.GREEN, Light.YELLOW], look up Light[name], find its
index with .index(...), and step forward with % len(ORDER).
Press ▶ Run and hit ✓ Check. ✅
Practice — level up
0/5 solvedSolve each in its own editor — Run to try, Check to grade. Stuck? Reveal a Hint, or the full Solution + walkthrough.
Define class Status(Enum): PENDING = "pending"; DONE = "done"; FAILED = "failed". Write status_value(name) returning the .value of the member whose name is name.
Define class Priority(IntEnum): LOW = 1; MEDIUM = 2; HIGH = 3. Write higher_priority(a_name, b_name) returning whichever name has the higher priority (on a tie, return a_name).
Define class Suit(Enum): CLUBS = auto(); DIAMONDS = auto(); HEARTS = auto(); SPADES = auto(). Write suit_color(name) returning "red" for DIAMONDS/HEARTS and "black" for CLUBS/SPADES.
Reuse Priority (LOW=1, MEDIUM=2, HIGH=3). Write sort_by_priority(names) returning names sorted from lowest to highest priority.
Reuse Light (RED, GREEN, YELLOW in that cycle order). Write cycle_forward(name, steps) returning the name of the state reached after advancing steps times from name (steps can be 0 or larger than one full cycle).