Tuples – Practical Use, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
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.
PREVIOUS · MODULE 60Tuples – Operations
YOU ARE HERE · MODULE 61Tuples – Practical Use
NEXT · MODULE 62Dictionaries – Advanced (comprehensions, merging)
What students will learn in this module
Every question in Module 61
Quick questions
- Why use Color.RED instead of 'red'?AIt's shorterBTypos cause errors immediately
- Can enums be used in if/elif chains?ANoBYes great for state machines
- What does auto() do?AAutomatically assigns incrementing valuesBCreates the class
- Can enums be dictionary keys?AYes they are hashableBNo
- 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 doesfor item in Status.members:Match it to what it doesif status == Status.ACTIVE:Match it to what it doesfor item in Status:Match it to what it doesmin(3, 1, 4, 1, 5)Match it to what it doesmax(3, 1, 4, 1, 5)Match it to what it doesFill in the blanks
from enum import Enum, ____ class Color(Enum): RED = auto()prices = {Size.SMALL: 5} print(prices[Size._____])if status __ Status.ACTIVE: print('Active')for item in _____: print(item.name)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:
