Answer key · 12 questions

Python Enums Exercises With Answers

All 12 questions for Python Enums in one place. Try the practice page first, then use this page to check the exact answer and the reason.

Quick answer

This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python Enums, with the exact answer and a short explanation for every question.

The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.

01 · Multiple choice

What is Color.RED?

  1. The string red
  2. A class
  3. A dictionary
  4. An enum membercorrect

Answer: An enum member

Why: Enum members are unique objects; the string is stored in .value.

02 · Multiple choice

What does this code print?

from enum import Enum
class Color(Enum):
    RED = "red"
print(Color.RED.value)
  1. RED
  2. redcorrect
  3. None
  4. Color.RED

Answer: red

Why: The value attribute returns the underlying string.

03 · Multiple choice

What does iterating over an Enum yield?

  1. Only its names
  2. Its memberscorrect
  3. Integer indexes
  4. Only its values

Answer: Its members

Why: Iteration yields the member objects in definition order.

04 · Multiple choice

What does Color('green') return?

  1. A new enum class
  2. ValueError
  3. The string green
  4. The Color.GREEN membercorrect

Answer: The Color.GREEN member

Why: Calling the enum with a value looks up the matching member.

05 · Fill in the blank

Complete the base class for an enum.

from enum import Enum
class Color(____):
    RED = "red"

Answer: Enum

Why: Enum is the standard base class for named members.

06 · Fill in the blank

Complete the attribute that returns the underlying value.

from enum import Enum
class Color(Enum):
    RED = "red"
print(Color.RED.____)

Answer: value

Why: A member stores its assigned value in .value.

07 · Fill in the blank

Complete the attribute that returns the member name.

from enum import Enum
class Color(Enum):
    RED = "red"
print(Color.RED.____)

Answer: name

Why: A member stores its identifier in .name.

08 · Fill in the blank

Complete the name lookup.

from enum import Enum
class Color(Enum):
    RED = "red"
print(Color["____"])

Answer: RED

Why: Square brackets look up a member by its name.

09 · Predict output

What does the program output?

from enum import Enum
class Color(Enum):
    RED = "red"
print(Color.RED.name)

Answer: RED

Why: The name attribute is the identifier RED.

10 · Predict output

What appears on the screen?

from enum import Enum
class Color(Enum):
    RED = "red"
    GREEN = "green"
print(Color("green").value)

Answer: green

Why: Lookup by value returns GREEN, whose value is green.

11 · Predict output

What is printed?

from enum import Enum
class Color(Enum):
    RED = "red"
    GREEN = "green"
print(len(list(Color)))

Answer: 2

Why: There are two members, so the list has length two.

12 · Predict output

What is the output?

from enum import Enum
class Color(Enum):
    RED = "red"
print(Color.RED is Color.RED)

Answer: True

Why: Enum members are singletons, so the identity comparison is true.

Try the questions first

Open the practice page for Python Enums and answer the same 12 questions with instant feedback.

Practice this lesson