Error in user YAML: (<unknown>): found a tab character that violate indentation while scanning a plain scalar at line 3 column 3
---
- oeasy Python 0123
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
- 本教程同步发布在:
- 个人网站: `https://oeasy.org`
- 蓝桥云课: `https://www.lanqiao.cn/courses/3584`
- GitHub: `https://github.com/overmind1980/oeasy-python-tutorial`
- Gitee: `https://gitee.com/overmind1980/oeasypython`
---- 上次我们了解了元组(tuple)
- 元组 也可以
- 支持 索引 和 切片
- 还可以 用
- 计数函数(count)
- 索引函数(index)
- 既然 有了 列表
- 为什么 还需要有 元组 呢?
- 用 元组 不用列表的原因是
- 元组 是 不可变的(immutable)
- 更适合 表示 固定的序列
- 元组 比 列表 占用更少内存
- 元组 是 不可变的(immutable)
- 元组 有什么
具体应用吗?🤔
- 大写数字 表示法
- 怎么 把 整型数值 转成 大写 呢?
given = int(input("Please input a number of 5-digit:"))
wan = given // 10000
qian = (given // 1000) % 10
bai = (given // 100) % 10
shi = (given // 10) % 10
ge = given % 10
position = tuple("萬仟佰拾")
numbers = tuple("零壹贰叁肆伍陆柒捌玖")
result = numbers[wan] + position[0]
result += numbers[qian] + position[1]
result += numbers[bai] + position[2]
result += numbers[shi] + position[3]
result += numbers[ge]
print(result)- 转化效果
- 先点 小红点
- 再点 小虫子
- 进入调试
- 点击step over 跳过
- 一步步执行
- 注意输入 数字
- 观察 本地变量 变化
2026, 3, 8
- 等价于
(2026, 3, 8)
- 用逗号连接的 就是元组
- 赋值的时候 可以解包
year, month, date = 2026, 3, 8
year
month
date
- 用逗号 分隔
- 也会被 自动封包成 元组
- 所谓 解包 解的是 元组包
year, month, date = 2026, 3
- 需要三个值
- 只解出来了 两个
year, month = 2026, 3, 8
- 需要 两个值
- 解出来 三个
- 总共 有几种赋值 呢?
a = 1
a = b = 1
a += 1
a, b = 0, 1
| 赋值类型 | 英文名称 | 示例代码 |
|---|---|---|
| 一般赋值 | assignment | a = 1 |
| 链式赋值 | chained assignment | a = b = 1 |
| 增强赋值 | augmented assignment | a += 1 |
| 平行赋值 | parallel assignment | a, b = 0, 1 |
- 除此之外
- 还有海象运算符
- 海象运算符
- 本质上 是 赋值运算符
- 赋值之后
- 返回这个具体的值
- 先赋值
- 再比较
n
(n := 6) > 7
n
- 不但可以 声明并赋值
- 还可以 将返回值 进行比较
- 可以用列表进行平行赋值吗?
a, b = [0, 1]
- 用 列表 赋值
- 确实是可以的
- 如何理解呢?
- 列表 经历了
- 类型转化
- 解包
- unpacking
-
完成了赋值
-
回忆之前的 缝合
students = ["oeasy", "o2z", "o3z"]
math = [95, 96, 97]
chinese = [91, 92, 93]
score = list(zip(students, math, chinese))
- 缝合之后
- 按照lambda表达式
- 排序
shuxue = sorted(score, key=lambda x: x[1], reverse=True)
shuxue
- 元组 能 做加法、乘法吗
- 元组 加法 和 列表加法
(1, 2) + (2, 3)
[1, 2] + [2, 3]
- 和 列表 运算很像
- 都是 item链接
- 元组乘法 和 列表乘法 也很像
(1, 2) * 3
[1, 2] * 3
3 * (1, 2)
3 * [1, 2]
- 元组乘法 相当于 元组累加
- 计算的结果还是元组
- 元组有什么应用场景呢?🤔
- 列表项 是 元组
from midiutil import MIDIFile
# 创建MIDI对象,1个轨道
midi = MIDIFile(1)
piano_track = 0 # 钢琴主旋律
tempo = 130
# 设置音量
piano_volume = 100
# 设置轨道速度
midi.addTempo(piano_track, 0, tempo)
# 整合音符和持续时间到一个列表
# 格式:(音符, 持续时间)
# 添加钢琴主旋律
melody = [
(76, 1), (76, 1), (77, 1), (79, 1), # 3 3 4 5
(79, 1), (77, 1), (76, 1), (74, 1), # 5 4 3 2
(72, 1), (72, 1), (74, 1), (76, 1), # 1 1 2 3
(76, 1.5), (74, 0.5), (74, 2), # 3 2 2
(76, 1), (76, 1), (77, 1), (79, 1), # 3 3 4 5
(79, 1), (77, 1), (76, 1), (74, 1), # 5 4 3 2
(72, 1), (72, 1), (74, 1), (76, 1), # 1 1 2 3
(74, 1.5), (72, 0.5), (72, 2), # 2 1 1
(74, 1), (74, 1), (76, 1), (72, 1), # 2 2 3 1
(74, 1), (76, 0.5), (77, 0.5), (76, 1),(72, 1), # 2 3 4 3 1
(74, 1), (76, 0.5), (77, 0.5), (76, 1),(74, 1), # 2 3 4 3 2
(72, 1), (74, 1), (67, 2), # 1 2 _5
(76, 1), (76, 1), (77, 1), (79, 1), # 3 3 4 5
(79, 1), (77, 1), (76, 1), (74, 1), # 5 4 3 2
(72, 1), (72, 1), (74, 1), (76, 1), # 1 1 2 3
(74, 1.5), (72, 0.5), (72, 2) # 2 1 1
]
current_time = 0
for note, duration in melody:
midi.addNote(piano_track, 0, note, current_time, duration, piano_volume)
current_time += duration
# 保存MIDI文件
with open("joy_simple.mid", "wb") as output_file:
midi.writeFile(output_file)
print("简化版欢乐颂MIDI文件已生成完成!")
- 这次 我们用 元组 做了
- 大写数字
- 欢乐颂
- 元组 和 列表 都是 序列
- 还有 啥序列类型 呢?🤔
- 下次再说 👋
- 本文来自 oeasy Python 系统教程。
- 想完整、扎实学 Python,
- 搜索 oeasy 即可。


















