Tuples – Practical Use, online

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

Tuples – Practical Use in one minute

What students need to know

Using enum members makes comparisons self-documenting. Looping over an enum yields each member in definition order.

An invalid enum name raises AttributeError; a mistyped string silently passes. if status == Status.ACTIVE: is clearer and safer than if status == 'active':.

Mistakes students make

  • Compare with enum member, not string: prevents typos.
  • String key typo passes silently. Enum key would cause clear error.
  • auto needs parentheses: auto() not just auto.
  • String 'rad' passes silently. Enum Color.RED would error immediately.

Discover other journeys with this module

What students will learn in this module

Every question in Module 61

Quick questions

  1. Why use Color.RED instead of 'red'?AIt's shorterBTypos cause errors immediately
  2. Can enums be used in if/elif chains?ANoBYes great for state machines
  3. What does auto() do?AAutomatically assigns incrementing valuesBCreates the class
  4. Can enums be dictionary keys?AYes they are hashableBNo
  5. When should you use enums?AFor a fixed set of choices (like status, direction, color)BFor all variables

Key terms

Students type the meaning of:

  • Tuples

Match the pairs

if status = Status.ACTIVE:Match it to what it does
for item in Status.members:Match it to what it does
if status == Status.ACTIVE:Match it to what it does
for item in Status:Match it to what it does
min(3, 1, 4, 1, 5)Match it to what it does
max(3, 1, 4, 1, 5)Match it to what it does

Fill in the blanks

  1. from enum import Enum, ____
    
    class Color(Enum):
        RED = auto()
  2. prices = {Size.SMALL: 5}
    print(prices[Size._____])
  3. if status __ Status.ACTIVE:
        print('Active')
  4. for item in _____:
        print(item.name)
  5. class Size(Enum):
        SMALL = ____()

Spot the error

Question 1

Afrom enum import EnumBclass Status(Enum):C    ACTIVE = 1D    INACTIVE = 2Eif status == 'active':

Question 2

Aprices = {'SMALL': 5, 'MEDIUM': 8}Bsize = 'SMALL'Ccost = prices['SMAL']Dprint('Cost:', cost)

Question 3

Afrom enum import Enum, autoBCclass Color(Enum):D    RED = autoE    GREEN = auto

Question 4

Adef paint(color):B    if color == 'red':C        passDEpaint('rad')

Question 5

Afrom enum import EnumBclass Color(Enum):C    RED = 1DEif color == Color.RED.value: