很多人学Python时都会遇到一个阶段:语法看着都懂,一写代码就报错。问题通常不在你,而在于Python的基础规则如果没有系统过一遍,靠零散记忆是很难稳的。
一、程序的书写规范:Python到底“怎么写”
1️⃣ 一行一条语句(标准写法)
解释:
2️⃣ 一行多条语句(一行写法)
a = 1; b = 2; print(a + b)
解释:
3️⃣ 一条语句写多行(非常常见)
✅ 用括号(强烈推荐)
total = (1 + 2 + 3 + 4 + 5 + 6)print(total)
解释:
✅ 用反斜杠(知道即可)
total = 1 + 2 + 3 + \ 4 + 5 + 6
4️⃣ 写在 [] / {}里的跨行语句
student = { "name": "Jack", "age": 20, "score": 86.5}print(student)
解释:
二、代码块与缩进:Python最核心的规则
Python 不用大括号,完全靠缩进来表示代码块。
案例 1:if代码块
score = 95if score >= 60: print("及格")print("程序结束")
解释:
同一代码块必须缩进一致
只要少一个空格,程序就会报错
案例 2:if/elif/else(完整结构)
score = 90if score >= 90: result = "优秀"elif score >= 60: result = "及格"else: result = "不及格"print(result)
案例 3:for循环+累加
total = 0for i in range(1, 11): total += iprint("1~10的和是:", total)
案例 4:while循环(避免死循环,运行代码会报错吗?)
n = 5while n > 0: print(n) n -= 1print(n)
三、注释:写给“人”看的代码说明
案例 5:单行注释
# 计算矩形面积width = 20height = 10print(width * height)
案例 6:多行注释
"""给出两个数输出它们的平均值"""a = 18b = 30print((a + b) / 2)
四、标识符和关键字:变量名不能乱起
案例 7:合法 vs 非法变量名
my_name = "Jack"_userAge = 20# 555name = "X" # ❌ 数字开头# my name = "X" # ❌ 有空格# while = 3 # ❌ 关键字
案例 8:用程序判断是不是关键字(非常实用)
import keywordprint(keyword.iskeyword("while")) # Trueprint(keyword.iskeyword("do")) # False
案例 9:三种常见命名风格
MyName = "大驼峰"myName = "小驼峰"my_name = "下划线(最常用)"
五、Python 的数据类型(9种)
1️⃣ int(整数)
print(200)print(0b1111)print(0x10F)
2️⃣ float(浮点数)
print(3.1415)print(3.14e3)print(1.5e-3)
3️⃣ complex(复数)
c = 10 + 2jprint(c.real, c.imag)
4️⃣ bool(布尔,本质是特殊整型)
print(True + 10)print(False + 10)
5️⃣ str(字符串)
s = "Python"print(len(s))print(s[0])print(s.replace("Py", "Pay"))
6️⃣ list(列表,可变)
lst = [1, 2, 3]lst.append(4)print(lst)
7️⃣ tuple(元组,不可变)
t = (1, 2, 3)# t.append(4) # ❌print(t)
8️⃣ dict(字典)
student = {"name": "Jack", "age": 20}print(student["name"])print(student.get("class", "未知"))
9️⃣ set(集合,去重神器)
nums = [1, 2, 2, 3, 3]print(set(nums))
六、变量与赋值
案例 10:类型由“值”决定
x = 82print(type(x))x = 3.1415print(type(x))x = "hello"print(type(x))
案例 11:多变量赋值&交换
a = b = c = 100x, y = 1, 2x, y = y, xprint(x, y)
案例 12:输入一定要记得转类型
a = int(input("输入整数:"))b = float(input("输入浮点数:"))print(a + b)
七、Python 运算符(重点+易错)
案例 13:算术运算符
a, b = 24, 5print(a / b)print(a // b)print(a % b)print(a ** b)
案例 14:比较运算符
print(5 > 8)print("10" < "2") # True(按字符比较)
案例 15:逻辑运算符(短路是重点)
x, y = 12, 0print(x and y)print(x or y)print(not x)
案例 16:or设置默认值
nickname = ""name = nickname or "默认"print(name)
案例 18:赋值运算符
x = 10x += 5x *= 2x //= 4print(x)
案例 19:位运算符(直观理解)
a, b = 5, 3print(a & b)print(a | b)print(a ^ b)print(a << 1)print(a >> 1)
八、运算符优先级
案例 20::不确定就加括号
x = 3 + 10 * ((3*12) - 8) // 10print(x)
如果你能做到三点:
那么后面的流程控制、函数、数据结构,都会顺很多。
👉「跟着敲一敲」练习区
题目 1:输出基本信息(变量+print)
要求:定义变量name和age,并输出一句话:
✅ 参考答案
name = "小明"age = 18print("我的名字是", name, "今年", age, "岁")
说明:考察变量定义和print()的基本使用。
题目 2:计算长方形面积(算术运算)
要求:已知长为8,宽为5,计算并输出长方形面积。
✅ 参考答案
length = 8width = 5area = length * widthprint(area)
说明:考察乘法运算符*和变量赋值。
题目 3:判断及格(if+比较运算符)
要求:定义变量score,如果成绩大于等于60,输出“及格”,否则输出“不及格”。
✅ 参考答案
score = 75if score >= 60: print("及格")else: print("不及格")
说明:考察if语句和比较运算符>=。
题目 4:判断奇偶数(%运算符)
要求:定义一个整数n,判断它是奇数还是偶数。
✅ 参考答案
n = 10if n % 2 == 0: print("偶数")else: print("奇数")
说明:%表示取余,是判断奇偶最常用的方法。
题目 5:累加求和(for循环)
要求:计算1到5的和,并输出结果。
✅ 参考答案
total = 0for i in range(1, 6): total += iprint(total)
说明:考察for循环和复合赋值运算符+=。
题目 6:列表操作(list)
要求:定义一个列表 [1, 2, 3],在末尾添加数字4并输出。
✅ 参考答案
lst = [1, 2, 3]lst.append(4)print(lst)
说明:考察列表的基本操作和append()。
题目 7:字典取值(dict)
要求:定义一个字典保存学生姓名和年龄,并输出姓名。
✅ 参考答案
student = {"name": "Rose", "age": 18}print(student["name"])
说明:考察字典定义和通过键取值。
题目 8:去除重复元素(set)
要求:将列表 [1, 2, 2, 3, 3] 去重并输出结果。
✅ 参考答案
nums = [1, 2, 2, 3, 3]result = set(nums)print(result)
说明:集合set的一个典型用途就是去重。
题目 9:运算符优先级(不加括号 vs 加括号)
要求:计算并输出表达式:3 * 4 ** 2 // 8 % 7
✅ 参考答案
result = 3 * 4 ** 2 // 8 % 7print(result)
输出结果:6
说明:先算**,再算*、//,最后算%。