Tuples – Operations, online

Six activities your students do on their own devices. See who's finished, and their scores.

Tuples – Operations in one minute

What students need to know

Enum is imported from Python's built-in enum module. .value returns the integer (or string) assigned to the member.

An enum gives meaningful names to a fixed set of values — like Color.RED instead of 1. Color.REED would be an error; 'reed' would silently pass through.

Mistakes students make

  • Must import Enum first: from enum import Enum.
  • Duplicate values create aliases, not new members. Use unique values.
  • Cannot reassign enum members they are immutable.
  • Convention: enum member names should be UPPER_CASE.

Discover other journeys with this module

What students will learn in this module

Every question in Module 60

Quick questions

  1. What is an enumeration?AA set of named constantsBA type of list
  2. Why use enums instead of strings?APrevents typos and invalid valuesBThey're faster
  3. Color.RED.value returns?AThe value assigned to RED (e.g., 1)BThe string 'RED'
  4. Color.RED.name returns?A1B'RED' (the name as a string)
  5. Can enum members be compared?ANoBYes Color.RED == Color.RED is True

Key terms

Students type the meaning of:

  • Enum
  • Member
  • .value
  • .name

Match the pairs

from Enum import enumMatch it to what it does
Color(RED)Match it to what it does
from enum import EnumMatch it to what it does
Color.RED.valueMatch it to what it does
list(range(3))Match it to what it does
type(True)Match it to what it does

Fill in the blanks

  1. from enum import ____
    
    class Color(Enum):
        RED = 1
  2. class Color(____):
        RED = 1
  3. print(Color.RED._____)
  4. print(Color.RED.____)
  5. for c in _____:
        print(c)

Spot the error

Question 1

Aclass Color(Enum):B    RED = 1

Question 2

Afrom enum import EnumBCclass Color(Enum):D    RED = 1E    GREEN = 1

Question 3

Afrom enum import EnumBCclass Color(Enum):D    RED = 1EFColor.RED = 2

Question 4

Afrom enum import EnumBCclass Color(Enum):D    red = 1

Question 5

Afrom enum import EnumBCclass Color(Enum):D    RED = 1EFif color == 'RED':