前言 作为一个主要写前端(JavaScript/TypeScript/Vue)的开发者,我一直觉得后端语言是技能树上缺失的一块。最近终于下定决心系统学习 Python,原因很简单:
语法简洁 ,上手快,能快速把想法变成代码
生态丰富 ,从 Web 开发到数据分析到 AI,覆盖面广
与前端互补 ,以后能独立搭建全栈项目
这篇文章记录第一天的学习成果——Python 四大核心数据类型、循环控制流、字符串操作和文件读写。全部内容都配有可运行的代码示例,跟着敲一遍基本就能掌握。
环境与工具
项目目录结构:
1 2 3 4 5 6 7 8 9 pythonLearn/ ├── 01_basics/ │ ├── 01_data_types.py # 数据类型 │ ├── 02_loops.py # 循环 │ ├── 03_strings.py # 字符串 │ ├── 04_file_io.py # 文件读写 │ └── exercise_*.py # 各课练习 └── demo/ └── BMI.py # 入门作:BMI 计算器
一、四大核心数据类型 每种语言都有自己的核心数据结构,Python 的 list / tuple / dict / set 基本覆盖了 90% 的日常场景。
1.1 list — 列表 列表是 有序、可变 的序列,用 [] 表示。类比 JS 的数组,但 Python 的切片语法更灵活:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 scores: list [int ] = [85 , 92 , 78 , 90 , 88 ] scores[0 ] scores[-1 ] scores[:3 ] scores[1 :4 ] scores.append(95 ) scores.insert(1 , 100 ) scores.remove(78 ) scores.pop() doubled = [s * 2 for s in scores] passed = [s for s in scores if s >= 90 ]
感悟 :列表推导式是第一个让我觉得 Python 确实简洁的地方。同样的逻辑在 JS 里需要 map + filter 链式调用,Python 一个中括号搞定且语义清晰。
1.2 tuple — 元组 元组是 有序、不可变 的序列,用 () 表示。创建后不能修改,适合存常量、配置、坐标等不会变的数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 point: tuple [int , int ] = (3 , 4 ) rgb: tuple [int , int , int ] = (255 , 128 , 0 ) x, y = point r, g, b = rgb a, b = b, a from typing import NamedTupleclass Student (NamedTuple ): name: str age: int grade: float s = Student(name="小明" , age=15 , grade=92.5 ) print (s.name, s.age)
对比 JS :Python 元组的不可变性类似 Object.freeze(),但语法层面就保证了,不用额外处理。
1.3 dict — 字典 字典是 键值对映射 ,用 {} 表示。相当于 JS 的 Object / Map,但访问方式更安全:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 student: dict [str , object ] = { "name" : "小明" , "age" : 15 , "scores" : [85 , 92 , 78 ], } student.get("phone" ) student.get("phone" , "未填写" ) for key, value in student.items(): print (f"{key} -> {value} " ) squares = {x: x ** 2 for x in range (1 , 6 )}
特别提醒 :student["phone"] 如果 key 不存在会直接抛 KeyError,养成用 get() 的习惯能少很多 bug。
1.4 set — 集合 集合是 无序、不重复 元素的集合。最适合做去重和集合运算:
1 2 3 4 5 6 7 8 9 10 11 12 13 numbers = {1 , 2 , 3 , 2 , 1 , 4 , 5 } print (numbers) math_club = {"小明" , "小红" , "小刚" , "小丽" } music_club = {"小红" , "小丽" , "小华" , "小雪" } math_club & music_club math_club | music_club math_club - music_club math_club ^ music_club "小明" in math_club
1.5 综合示例:班级成绩分析 四种类型一起用才真正感受到组合的力量:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 subjects: tuple [str , ...] = ("语文" , "数学" , "英语" ) class_scores: dict [str , list [int ]] = { "小明" : [88 , 92 , 85 ], "小红" : [75 , 60 , 78 ], "小刚" : [90 , 88 , 92 ], "小丽" : [55 , 70 , 65 ], "小华" : [80 , 45 , 72 ], } for name, scores in class_scores.items(): avg = sum (scores) / len (scores) print (f"{name} : 平均 {avg:.1 f} " ) failing = set () for name, scores in class_scores.items(): for score in scores: if score < 60 : failing.add(name) print (f"补考名单: {failing} " ) excellent = {name for name, scores in class_scores.items() if all (s >= 80 for s in scores)} print (f"全科优秀: {excellent} " )
二、循环与控制流 2.1 for 循环 — Python 的灵魂 Python 的 for 和 JS 不太一样:Python 的 for 总是遍历可迭代对象 ,而不是传统的三表达式循环:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 for fruit in ["苹果" , "香蕉" , "橘子" ]: print (fruit) for char in "Python" : print (char) range (5 ) range (2 , 7 ) range (1 , 10 , 2 ) for i, fruit in enumerate (fruits): print (f"[{i} ] {fruit} " ) names = ["小明" , "小红" , "小刚" ] scores = [85 , 92 , 78 ] for name, score in zip (names, scores): print (f"{name} : {score} 分" )
技巧 :忘记索引的存在,优先用 for item in iterable。需要索引时才用 enumerate()。
2.2 while 循环 1 2 3 4 5 countdown = 5 while countdown > 0 : print (f"{countdown} ..." ) countdown -= 1 print ("发射!" )
2.3 break 和 continue 1 2 3 4 5 6 7 8 9 10 11 for num in [3 , 7 , 2 , 9 , 5 ]: if num == 9 : print ("找到了!" ) break for num in range (1 , 11 ): if num % 2 == 0 : continue print (num)
2.4 for…else — Python 独有特性 这是其他主流语言没有的:else 跟在循环后面,循环正常结束(没被 break)才执行 :
1 2 3 4 5 6 7 8 9 def is_prime (n: int ) -> bool : for i in range (2 , int (n ** 0.5 ) + 1 ): if n % i == 0 : print (f"{n} 能被 {i} 整除" ) break else : print (f"{n} 是素数!" ) return True return False
这个特性完美解决了”在循环中查找某物,没找到时需要执行默认逻辑”的模式——不用额外设 flag 变量。
三、字符串操作 3.1 切片规则(和列表完全一致) 1 2 3 4 text = "Hello Python!" text[0 :5 ] text[6 :] text[::-1 ]
3.2 常用方法 1 2 3 4 5 6 " Hello World! " .strip() "apple banana apple" .count("apple" ) "apple banana" .replace("apple" , "grape" ) "hello" .upper() "Hello" .startswith("He" ) "123" .isdigit()
3.3 f-string — 最推荐的格式化方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 name, age = "小明" , 15 print (f"我叫{name} ,明年{age + 1 } 岁" ) score = 92.567 print (f"{score:.1 f} " ) print (f"{1234567 :,} " ) print (f"|{'左对齐' :<10 } |" )print (f"|{'居中' :^10 } |" )print (f"|{'右对齐' :>10 } |" )print (f"{name = } " )
3.4 split 和 join — 字符串 ↔ 列表互转 1 2 3 4 5 "小明,15,上海" .split("," ) "Python is fun" .split() ", " .join(["Python" , "is" , "awesome" ]) "" .join(["a" , "b" , "c" ])
四、文件读写 4.1 with open() — 永远这样用 with 语句会自动关闭文件,即使中途报错也不会漏:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 with open ("notes.txt" , "w" , encoding="utf-8" ) as f: f.write("Hello Python\n" ) f.writelines(["第一行\n" , "第二行\n" ]) with open ("notes.txt" , "r" , encoding="utf-8" ) as f: content = f.read() with open ("notes.txt" , "r" , encoding="utf-8" ) as f: lines = f.readlines() with open ("notes.txt" , "r" , encoding="utf-8" ) as f: for line in f: print (line.strip())
4.2 json 持久化 — 结构化数据的正确存储方式 这是我第一天学到的最重要的教训 。一开始我把 dict 拆成文本行写进文件,读回来只剩字符串,结构全丢了:
1 2 3 4 5 6 7 8 9 10 11 f.write(f"{note['date' ]} \n" ) f.write(f" {note['content' ]} \n" ) import jsonnotes = [{"date" : "2024-01-15" , "content" : "学了 Python" , "tags" : ["Python" ]}] json.dump(notes, f) notes = json.load(f)
核心认知 :结构化数据 → 用 json。手动拼文本看似简单,但数据读回来就废了。
五、实战练习 每个知识点后面都跟着一个练习,这里挑两个代表性强的展示:
5.1 猜数字游戏(循环 + 控制流) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import randomtarget = random.randint(1 , 10 ) max_attempts = 5 for attempt in range (1 , max_attempts + 1 ): guess = int (input (f"第{attempt} 次猜: " )) if guess < target: print ("太小了!" ) elif guess > target: print ("太大了!" ) else : print (f"猜中了!用了 {attempt} 次" ) break else : print (f"游戏结束,答案是 {target} " )
5.2 文本统计分析器(字符串 + 字典 + 集合) 1 2 3 4 5 6 7 8 9 10 article = "Python was created by Guido. Python is easy. Python is powerful." words = article.lower().replace("." , "" ).split() unique = set (words) word_count = {} for word in words: word_count[word] = word_count.get(word, 0 ) + 1 print (f"总词数: {len (words)} , 不重复: {len (unique)} " )
5.3 笔记管理器(文件 + json + 菜单交互) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 import jsonnotes = [] if Path("notes.json" ).exists(): with open ("notes.json" , "r" , encoding="utf-8" ) as f: notes = json.load(f) while True : print ("1.写笔记 2.查看 3.搜索 4.退出" ) choice = input ("请选择: " ) if choice == "1" : note = {"date" : input ("日期:" ), "content" : input ("内容:" ), "tags" : input ("标签(逗号分隔):" ).split("," )} notes.append(note) elif choice == "2" : for n in notes: print (f"[{n['date' ]} ] {n['content' ]} " ) elif choice == "3" : kw = input ("搜索:" ).lower() for n in notes: if kw in n["content" ].lower(): print (f"[{n['date' ]} ] {n['content' ]} " ) elif choice == "4" : with open ("notes.json" , "w" , encoding="utf-8" ) as f: json.dump(notes, f) break
学习心得 第一天总共写了 4 个教程文件 + 4 个练习文件 ,大约 300 行代码。几个比较深的感受:
1. Python 的简洁是真实的
列表推导式、元组解包、f-string 调试模式、for...else——这些语法糖不是花哨,而是确实减少了日常代码量。特别是列表推导式,工作半年后回头看估计会觉得 JS 的 filter().map() 很啰嗦。
2. 数据类型选对事半功倍
用 set 做去重和成员判断、用 dict.get() 替代 [] 安全访问、用 tuple 承载不可变配置——这些习惯从一开始建立比后期纠正容易得多。
3. 结构化数据就该用 json
第一版笔记管理器用纯文本存数据,读回来结构全丢,自己都感觉不对劲。换成 json.dump/load 后代码少了三分之一,数据保真度 100%。千万别为了”少学一个模块”而用错工具。
4. 写代码和写文章是两种收获
把学到的知识写成这篇博客,逼着我重新梳理每个知识点的”为什么”和”怎么用”,比单纯敲代码理解更深。以后每个阶段学完都值得写一篇。
项目地址:github.com/halely/python-study
下一篇预告:Python 函数进阶——*args / **kwargs、模块与包、异常处理。
学习时间:2026-06-01