python - set/tuple/dict quiz

这组题目专门为考察深度理解而设计,涵盖了 Tuple(元组)、Dict(字典)和 Set(集合)中最容易混淆的"边缘场景"(Edge Cases)。


Part 1: Tuple - The "Immutable" Container?

考察点:元组的不可变性是"相对"的吗?单元素元组的陷阱。

Q1: The Secret Change (元组里的列表)

Look at this code. Will it cause an error? If not, what is the final value of my_tuple?

python 复制代码
my_tuple = (1, 2, [3, 4])
my_tuple[2].append(5)
print(my_tuple)
  • Teacher's Note (中文解释): 这是一个非常经典的面试题。元组本身不可变(你不能把 [3, 4] 替换成别的对象),但如果元组里面装着一个可变对象(如列表),列表内部是可以改的。这考察学生是否真正理解"不可变"是指指向的地址不变。

Q2: The "Liar" Tuple (伪装成元组的数字)

What is the type of x and y?

python 复制代码
x = (5)
y = (5,)
  • Teacher's Note (中文解释): 很多初学者认为圆括号就代表元组。实际上,单个元素的元组必须加逗号 (5,)。否则 (5) 只是一个带括号的整数。

Part 2: Dictionary - Keys and Logic

考察点:哪些东西能当 Key?重复 Key 会发生什么?

Q3: The Identity Crisis (重复键的覆盖)

What will this code print?

python 复制代码
data = {1: "Apple", 1.0: "Banana", True: "Cherry"}
print(data[1])
print(len(data))
  • Teacher's Note (中文解释): 这是一个高级边缘场景。在 Python 中,1 == 1.0 == True。字典在存储时会认为它们是同一个 Key。后来的值会覆盖前面的值。

Q4: The Forbidden Key (禁忌之键)

Which of these will crash? Why?

python 复制代码
d1 = {(1, 2): "Coordinate"} 
d2 = {([1, 2]): "Coordinate"}
  • Teacher's Note (中文解释): 强化上节课的内容。d1 的键是元组(不可变,OK);d2 的键是列表(可变,会报错)。

Part 3: Set - Uniqueness and Rules

考察点:集合的去重逻辑,以及集合里能放什么。

Q5: The Magic Eraser (集合去重)

What is the result of len(my_set)?

python 复制代码
my_set = {1, 2, 2, "2", (1, 2), (1, 2)}
print(len(my_set))
  • Teacher's Note (中文解释): 考察集合如何处理重复。注意 2"2" 是不同的(数字 vs 字符串),但两个 (1, 2) 元组是完全一样的,会被去重。

Q6: The Set inside a Set (集合的嵌套)

Will this code work?

python 复制代码
inner_set = {1, 2}
outer_set = {inner_set, 3, 4}
  • Teacher's Note (中文解释): 集合(Set)里的元素必须是不可变 的。因为集合本身是可变的,所以集合不能嵌套集合 。如果想在集合里放集合,必须使用 frozenset(但这可能对孩子来说太深了,只要让他知道报错原因即可)。

Part 4: Mixed Logic (综合应用)

Q7: The Quick Clean (快速去重)

I have a list: names = ["Alice", "Bob", "Alice", "Charlie"]. How can I remove the duplicates using only one line of code?

  • Teacher's Note (中文解释): 考察对数据结构转换的实战应用:list -> set -> list

Answer Key for Tutor (答案与详解)

Question Answer Reason (English Explanations for the student)
Q1 (1, 2, [3, 4, 5]) No Error. The tuple's structure is fixed, but the list inside it is still a list and can be changed.
Q2 x is int, y is tuple A single element in parentheses is just a value. You need a comma to make it a tuple!
Q3 "Cherry", len is 1 In Python, 1, 1.0, and True are treated as the same value. The last one ("Cherry") overwrites the others.
Q4 d2 will crash You cannot use a List as a dictionary key because it is mutable.
Q5 4 The elements are: 1, 2, "2", and (1, 2). Duplicate 2 and duplicate (1, 2) are removed.
Q6 Error Sets can only contain immutable items. Since a Set is mutable, you cannot put a Set inside another Set.
Q7 list(set(names)) Converting a list to a set automatically removes duplicates. Then convert it back to a list.

给家教老师的建议技巧:

  1. Q1(元组里改列表) 最能考出学生是否真的懂了内存地址。如果他答错了,请带他回看 id() 函数。
  2. Q3(1 vs True) 这是一个"哇"时刻(Aha moment),可以顺便告诉他 Python 内部是如何处理布尔值的。
  3. Q7 是最实用的,告诉他这是程序员在现实工作中去除重复数据的"快捷方式"。
相关推荐
IT_Octopus2 小时前
Spring Boot 实战:@PostConstruct + Caffeine 缓存初始化与定时刷新
spring boot·后端·缓存
swipe3 小时前
从本地开发到生产部署:用 Docker Compose 跑通 NestJS、MySQL 与 Milvus
后端·langchain·llm
码事漫谈3 小时前
SenseNova Skills Studio:为商汤SenseNova U1打造的本地办公技能包
后端
zhangxingchao3 小时前
AI应用开发七:可以替代 RAG 的技术
前端·人工智能·后端
excel4 小时前
🧠 Prisma 表名大写 vs SQL 导出小写问题深度解析(附踩坑与解决方案)
前端·后端
GetcharZp5 小时前
Hermes Agent:一个真正“会成长”的开源 AI Agent,正在改变 AI 自动化玩法
后端
Gopher_HBo5 小时前
Go依赖管理
后端
ltl5 小时前
Layer Normalization:为什么 Transformer 用 LN,不用 BN
后端
ltl5 小时前
title: 【Transformer 与注意力机制】24|
后端