- 掌握四个基本类型:
int / float / bool / str。 - 理解 Python 的「动态强类型」与
type() 的用法。 - 认识
None 与类型转换 int() / float() / str()。
二、学习路线
| | |
|---|
| int / float / bool / str 与 type() | |
| | |
| | |
| | |
三、知识点讲解
1. 四个基本类型 - int:整数,如 18、-3、0。 - float:浮点数(带小数点的数),如 3.5、-0.1。注意 3 是 int,3.0 是 float。 - bool:布尔值,只有 True 和 False 两个值(首字母大写)。 - str:字符串,用引号包裹的任意文本,如 "小明"、'hello'。
2. 动态强类型 - 动态:变量不用提前声明类型,且同一个名字可以先后指向不同类型(今天指向数字,明天指向字符串)。 - 强类型:Python 不会偷偷把 "3"(字符串)和 3(整数)相加混在一起,类型不对就直接报错。这是「强」的体现,能帮你早发现问题。 - 用 type(变量) 随时查看它现在指向什么类型。
3. 变量命名规则 - 只能由字母、数字、下划线组成,且不能以数字开头:age1 合法,1age 非法。 - 区分大小写:Age 和 age 是两个不同变量。 - 不能用关键字(if、for、def 等)当变量名。 - 推荐「见名知意」:user_name 比 a 好。
4. None - 表示「空 / 还没有值 / 不存在」,是一个特殊的单例对象,类型是 NoneType。常见于函数「什么都不返回」时。
5. 类型转换 - int("123") 把数字字符串变整数;float("3.14") 变浮点;str(99) 把数字变字符串。 - bool(...):规则是 0、空字符串 ""、空容器都算 False,其余算 True。
6. 不可变与可变(初识) - 不可变:int / float / bool / str。所谓「修改」其实是让变量指向一个全新的对象,原来的不动。 - 可变:如 list 列表。对它的修改(如 append)会就地改变同一个对象,所有引用它的变量都会看到变化。第 2 周会深入。
四、实例代码
对应文件:day02_types.py
# Day02: 变量与基本类型# 知识点:int / float / bool / str、动态类型、type()、变量命名、# None、类型转换、不可变与可变初识。# 1) 四个最基本的内置类型age = 18 # int 整数price = 3.5 # float 浮点数(带小数点)is_student = True # bool 布尔值(只有 True / False)name = "小明" # str 字符串(用引号包裹)print("age 的类型:", type(age), "值:", age)print("price 的类型:", type(price), "值:", price)print("is_student 的类型:", type(is_student), "值:", is_student)print("name 的类型:", type(name), "值:", name)# 2) 动态类型:同一个变量可以先指向 int,再指向 str# Python 的变量只是「名字」,可以指向任意类型的对象。x = 10print("x 现在是:", x, type(x))x = "hello"print("x 后来变成:", x, type(x))# 3) None 表示「空 / 还没有值」,是一个特殊的单例对象result = Noneprint("result 是:", result, "类型:", type(result))# 4) 类型转换:用 int() / float() / str() 等构造函数转换s = "123"n = int(s)print("字符串 '123' 转成 int:", n, type(n))f = float("3.14")print("字符串 '3.14' 转成 float:", f, type(f))t = str(99)print("数字 99 转成 str:", t, type(t))# bool() 转换规则:0、空字符串、空容器都算 False,其余算 Trueprint("bool(0):", bool(0))print("bool(''):", bool(""))print("bool('hi'):", bool("hi"))# 5) 不可变与可变初识# 不可变:int / float / bool / str —— 「修改」其实是让变量指向新对象a = 10b = a # b 和 a 指向同一个整数对象a = a + 1 # a 重新指向新对象,b 不受影响print("不可变示例 -> a:", a, "b:", b)# 可变:list 列表 —— 修改会就地改变对象本身(第 2 周会深入讲解)lst1 = [1, 2, 3]lst2 = lst1 # lst2 和 lst1 指向同一个列表对象lst1.append(4) # 就地修改同一个对象print("可变示例 -> lst1:", lst1, "lst2:", lst2)
五、调试注意事项
- 类型转换要求字符串「长得像数字」:
int("abc") 会报 ValueError。 - 浮点数运算可能有精度误差(如
0.1 + 0.2 != 0.3),这是二进制表示的共性,不是 bug。 - 变量未赋值就使用会报
NameError,先赋值再使用。
六、运行结果提示
命令(建议Thonny直接运行更加简单):
cd /code && python3.11 day02_types.py
真实输出:
age 的类型: <class 'int'> 值: 18price 的类型: <class 'float'> 值: 3.5is_student 的类型: <class 'bool'> 值: Truename 的类型: <class 'str'> 值: 小明x 现在是: 10 <class 'int'>x 后来变成: hello <class 'str'>result 是: None 类型: <class 'NoneType'>字符串 '123' 转成 int: 123 <class 'int'>字符串 '3.14' 转成 float: 3.14 <class 'float'>数字 99 转成 str: 99 <class 'str'>bool(0): Falsebool(''): Falsebool('hi'): True不可变示例 -> a: 11 b: 10可变示例 -> lst1: [1, 2, 3, 4] lst2: [1, 2, 3, 4]
七、常见错误与避坑
- 把
True/False 写成 true/false(Python 首字母必须大写)。 - 用
== 比较值、用 is 比较对象身份;初学比较内容用 == 更稳妥。 - 字符串拼接时混用类型:
"年龄" + 18 会报错,要写成 "年龄" + str(18)。