为了方便你明天讲课,我为你整理了一份中英结合、结构清晰的教学大纲(Lesson Plan)。你可以直接按照这个逻辑引导学生。
🐍 Python Tutoring Plan: Tuple & Set
Part 1: Tuple (元组) ------ The Immutable List
1. Concept (概念)
A Tuple is a collection which is ordered (有序) and immutable (不可变). 你可以把它当成被锁进保险箱的 List(列表)。一旦创建,里面的东西就不能再做任何修改。
2. Key Features (核心特性)
- Ordered (有序) : Elements have a defined order, and you can access them by index (可以通过索引访问,比如
t[0])。 - Immutable (不可变): You CANNOT change, add, or remove items after the tuple is created (不能增删改)。
- Allow Duplicates (允许重复): It can have items with the same value (允许存在相同的值)。
3. Syntax & Examples (语法与示例)
用圆括号 () 来创建 Tuple。
python
# 1. Creation (创建)
my_tuple = (1, 2, 3, "apple", "apple")
print(my_tuple) # Output: (1, 2, 3, 'apple', 'apple')
# ⚠️ 教学重点 (Important Tip):
# 创建只有一个元素的 Tuple 时,必须加逗号!否则 Python 会把它当成普通的括号运算。
single_tuple = (5,) # Type: tuple
not_a_tuple = (5) # Type: int
# 2. Accessing Items (访问元素)
print(my_tuple[1]) # Output: 2 (Zero-indexed / 从0开始索引)
print(my_tuple[-1]) # Output: 'apple' (Negative indexing / 负索引)
# 3. Immutability Test (不可变性测试 - 会报错)
# my_tuple[0] = 100 # ❌ TypeError: 'tuple' object does not support item assignment
4. Cool Trick: Unpacking (炫酷技巧:解包)
这是 Python 非常 "Pythonic" 的写法,学生一定会喜欢:
python
coordinates = (10.5, 20.8)
lat, lon = coordinates # Unpacking (解包)
print(f"Latitude: {lat}, Longitude: {lon}")
5. Why use Tuples? (使用场景:既然有了 List,为什么还要 Tuple?)
- Data Safety (数据安全) : Write-protect data that shouldn't change (比如地理坐标
(latitude, longitude)或者 RGB 颜色(255, 0, 0))。 - Performance (性能): Tuples are slightly faster than lists (因为不可变,内存分配更高效)。
- Dictionary Keys (作为字典的键) : Because they are immutable, tuples can be used as keys in a
dict(List 就不可以)。
Part 2: Set (集合) ------ The Math Master
1. Concept (概念)
A Set is a collection which is unordered (无序) , unindexed (无索引) , and contains only UNIQUE elements (元素唯一). 它完美对应了数学中"集合"的概念。
2. Key Features (核心特性)
- Unordered & Unindexed (无序且无索引) : The items have no defined order, so you cannot use
set[0](没有顺序,每次打印出来的顺序可能都不一样,也不能用索引提取)。 - Unique (唯一性 - 自动去重): No duplicate members! (天生自带去重功能,这是它最常用的特性)。
- Mutable (可变): The set itself is mutable (can add/remove), BUT the items inside must be immutable (like int, string, tuple). (集合本身可以增删元素,但放进去的元素不能是 List 或 Dict)。
3. Syntax & Examples (语法与示例)
用花括号 {} 创建,或者使用 set() 函数。
python
# 1. Creation (创建)
my_set = {1, 2, 3, 3, 3, "apple"}
print(my_set) # Output: {1, 2, 3, 'apple'} (Duplicates are ignored! 重复项被自动去除了)
# ⚠️ 教学重点 (Important Tip):
# 如何创建一个空的 Set?
empty_set = set()
# empty_dict = {} <-- 注意:单写 {} 创建的是 Dictionary(字典),不是 Set!
# 2. Add and Remove (增删)
my_set.add("orange") # 增加元素
my_set.remove("apple") # 删除元素 (If item doesn't exist, it raises an error)
my_set.discard("banana") # 安全删除 (If item doesn't exist, NO error is raised)
4. Set Operations (集合运算 - Set的杀手锏)
Set 最强大的地方在于处理数学集合运算 (Venn Diagram / 韦恩图的概念):
python
set_A = {1, 2, 3, 4}
set_B = {3, 4, 5, 6}
# 1. Union (并集): All unique elements from both sets
print(set_A | set_B) # Output: {1, 2, 3, 4, 5, 6}
# print(set_A.union(set_B))
# 2. Intersection (交集): Elements present in BOTH sets
print(set_A & set_B) # Output: {3, 4}
# print(set_A.intersection(set_B))
# 3. Difference (差集): Elements in A but not in B
print(set_A - set_B) # Output: {1, 2}
# print(set_A.difference(set_B))
# 4. Symmetric Difference (对称差集): Elements in A or B, but NOT both (非交集的部分)
print(set_A ^ set_B) # Output: {1, 2, 5, 6}
5. Why use Sets? (使用场景)
- Removing Duplicates (一键去重) :
unique_list = list(set(my_list))(这是 Python 开发中最常用的去重套路)。 - Fast Membership Testing (超快查找) : Checking
if item in my_setis incredibly fast (O(1) time complexity) compared to checking in a list (O(n)). (判断一个元素是否在集合里,速度极快!)。
Part 3: Quick Comparison Table (给学生的复习总结)
为了让学生直观感受到它们的区别,明天可以带着他一起填这个表:
| Feature (特性) | List (列表) [] |
Tuple (元组) () |
Set (集合) {} |
|---|---|---|---|
| Mutable (可变性) | ✅ Yes (可变) | ❌ No (不可变) | ✅ Yes (可变) |
| Ordered (是否有序) | ✅ Yes (有序) | ✅ Yes (有序) | ❌ No (无序) |
| Duplicates (重复项) | ✅ Allowed | ✅ Allowed | ❌ No (自动去重) |
| Indexing (索引) | ✅ Yes ([0]) |
✅ Yes ([0]) |
❌ No |
💡 给你的教学小建议 (Tutor Tips):
- 互动提问 (Interactive Question): 讲完之后,可以问学生:"If I have a list of students' names, and I want to find out all the unique names, which data structure should I use?" (答案是先转成 Set)。
- 踩坑警告 (Common Pitfall) : 一定要让学生亲自敲一下
a = (5)和a = (5,)的代码,用type(a)看一下区别,这是初学者必踩的坑。 - 韦恩图 (Venn Diagram): 讲 Set 的交集并集时,如果有条件,在纸上画两个相交的圆圈(韦恩图),结合代码讲,学生会"秒懂"。
祝你明天的 Python 家教一切顺利!(Good luck with your tutoring session tomorrow!) 如果需要增加练习题,随时告诉我!