1:环境与工具链搭建
安装Python
首先,我们需要安装最新稳定版的Python。
- 访问Python官网:https://www.python.org/downloads/
- 下载对应的python版本
python --version如果显示了Python版本号,说明安装成功啦!
配置VS Code
VS Code是目前最受欢迎的代码编辑器之一:
- 下载安装VS Code:https://code.visualstudio.com/
- 打开VS Code,进入扩展市场(左侧栏第5个图标,或快捷键Ctrl+Shift+X)
搜索并安装以下扩展:
- Python(Microsoft官方扩展)
- Pylance(语法提示增强)
- IntelliCode(代码自动补全)
2:基础语法回顾
变量与数据类型
Python中变量无需声明类型,直接赋值即可使用:
# 数字类型
age = 25 # 整数
height = 175.5 # 浮点数
# 字符串
name = "小明"
message = '你好,世界!' # 单引号双引号都可以
# 布尔值
is_student = True
has_pet = False
# 打印变量
print(f"我叫{name},今年{age}岁,身高{height}cm")条件判断
使用<span leaf="">if-elif-else</span>结构:
score = 85
if score >= 90:
print("优秀!")
elif score >= 80:
print("良好!")
elif score >= 60:
print("及格")
else:
print("需要努力了")
# 输出:良好!循环结构
for循环适合遍历可迭代对象:
# 打印1到5
for i in range(1, 6):
print(i)
# 遍历列表
fruits = ["苹果", "香蕉", "橙子"]
for fruit in fruits:
print(f"我喜欢吃{fruit}")while循环适合不确定次数的循环:
# 猜数字游戏
import random
secret = random.randint(1, 10)
guess = 0
while guess != secret:
guess = int(input("猜一个1到10之间的数:"))
if guess > secret:
print("猜大了!")
elif guess < secret:
print("猜小了!")
print("恭喜,猜对了!")函数定义
函数是可重复使用的代码块:
# 定义函数
def say_hello(name):
"""打招呼函数"""
return f"你好,{name}!"
# 调用函数
message = say_hello("小红")
print(message) # 输出:你好,小红!
# 带默认参数的函数
def calculate_price(price, discount=0.9):
return price * discount
print(calculate_price(100)) # 输出:90.0
print(calculate_price(100, 0.8)) # 输出:80.03:列表与元组
列表基础操作
列表是Python中最常用的数据结构之一:
# 创建列表
students = ["小明", "小红", "小刚", "小丽"]
# 访问元素
print(students[0]) # 输出:小明
print(students[-1]) # 输出:小丽(负索引从末尾开始)
# 添加元素
students.append("小华") # 在末尾添加
students.insert(1, "小芳") # 在指定位置添加
# 删除元素
students.remove("小刚") # 删除指定元素
removed = students.pop() # 弹出最后一个元素并返回
# 获取列表长度
print(len(students))列表推导式
列表推导式是Python中非常强大的特性,可以用一行代码创建新列表:
# 传统方式:生成1到10的平方
squares = []
for i in range(1, 11):
squares.append(i ** 2)
print(squares) # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# 使用列表推导式
squares = [i ** 2 for i in range(1, 11)]
print(squares) # 同样输出:[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# 带条件的列表推导式:获取偶数的平方
even_squares = [i ** 2 for i in range(1, 11) if i % 2 == 0]
print(even_squares) # 输出:[4, 16, 36, 64, 100]排序
理解<span leaf="">sort()</span>和<span leaf="">sorted()</span>的区别:
numbers = [3, 1, 4, 1, 5, 9, 2]
# sorted()函数返回新的排序列表,原列表不变
sorted_numbers = sorted(numbers)
print(numbers) # [3, 1, 4, 1, 5, 9, 2]
print(sorted_numbers) # [1, 1, 2, 3, 4, 5, 9]
# sort()方法直接修改原列表
numbers.sort()
print(numbers) # [1, 1, 2, 3, 4, 5, 9]
# 降序排序
numbers.sort(reverse=True)
print(numbers) # [9, 5, 4, 3, 2, 1, 1]
# 按字符串长度排序
words = ["苹果", "香蕉", "葡萄", "西瓜", "草莓"]
words.sort(key=len)
print(words) # ['苹果', '西瓜', '香蕉', '草莓', '葡萄']元组
元组与列表类似,但创建后不能修改:
# 创建元组
coordinates = (10, 20)
person = ("张三", 25, "北京")
# 访问元素
name, age, city = person # 元组拆包
print(f"{name}今年{age}岁,住在{city}")
# 尝试修改会报错
# coordinates[0] = 15 # TypeError: 'tuple' object does not support item assignment元组应用场景:
- 作为字典的键(因为列表不可哈希)
- 函数返回多个值
- 数据不应被修改的场景
# 元组作为字典键
locations = {
(39.9, 116.4): "北京",
(31.2, 121.5): "上海",
(23.1, 113.3): "广州"
}
# 函数返回多个值
def get_user_info():
return "张三", 30, "工程师"
name, age, job = get_user_info()4:字典与集合
字典基础
字典是键-值对的集合,访问速度非常快:
# 创建字典
student = {
"name": "小明",
"age": 15,
"scores": {"语文": 95, "数学": 90, "英语": 85}
}
# 访问元素
print(student["name"]) # 输出:小明
print(student["scores"]["语文"]) # 输出:95
# 添加或修改元素
student["gender"] = "男"
student["age"] = 16
# 检查键是否存在
if "phone" in student:
print(student["phone"])
else:
print("没有电话信息")字典的高级用法
<span leaf="">get()</span>和<span leaf="">setdefault()</span>方法:
# get方法:安全地获取值,如果键不存在返回默认值
phone = student.get("phone", "未知")
print(phone) # 输出:未知
# setdefault:如果键不存在,设置默认值并返回
student.setdefault("address", "北京") # 添加新键值对
print(student["address"]) # 输出:北京
# 统计单词频率的实际应用
text = "苹果 香蕉 苹果 草莓 香蕉 苹果"
words = text.split()
word_count = {}
for word in words:
# 如果单词不存在,设为1;已存在则+1
word_count[word] = word_count.get(word, 0) + 1
print(word_count) # {'苹果': 3, '香蕉': 2, '草莓': 1}字典推导式
和列表推导式类似,可以用简洁的方式创建字典:
# 创建平方字典:{数字: 平方值}
squares = {x: x**2 for x in range(1, 6)}
print(squares) # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
# 反转字典的键值对
inventory = {"苹果": 10, "香蕉": 5, "橙子": 8}
reversed_dict = {count: fruit for fruit, count in inventory.items()}
print(reversed_dict) # {10: '苹果', 5: '香蕉', 8: '橙子'}
# 筛选字典
scores = {"小明": 95, "小红": 85, "小刚": 72, "小丽": 63}
high_scores = {name: score for name, score in scores.items() if score >= 80}
print(high_scores) # {'小明': 95, '小红': 85}集合
集合是不重复元素的无序集合,擅长去重和集合运算:
# 创建集合
fruits = {"苹果", "香蕉", "橙子", "苹果"} # 注意重复的"苹果"会被自动去除
print(fruits) # {'香蕉', '橙子', '苹果'}
# 集合运算
a = {1, 2, 3, 4, 5}
b = {4, 5, 6, 7, 8}
print(a | b) # 并集:{1, 2, 3, 4, 5, 6, 7, 8}
print(a & b) # 交集:{4, 5}
print(a - b) # 差集:{1, 2, 3}
print(a ^ b) # 对称差集:{1, 2, 3, 6, 7, 8}
# 去重应用
numbers = [1, 2, 2, 3, 3, 3, 4, 5, 5]
unique_numbers = list(set(numbers))
print(unique_numbers) # [1, 2, 3, 4, 5]集合的实际应用
# 比较两个文件中的共同单词
file1_words = {"Python", "编程", "学习", "基础", "语法"}
file2_words = {"Python", "函数", "类", "编程", "进阶"}
# 两个文件共有的单词
common_words = file1_words & file2_words
print(f"共有单词: {common_words}") # {'Python', '编程'}
# file1独有的单词
only_in_file1 = file1_words - file2_words
print(f"只在file1中: {only_in_file1}") # {'学习', '基础', '语法'}5:第一天复盘与练习
单词频率统计项目
这个项目将结合我们学过的知识,读取文本文件并统计单词出现频率:
def word_frequency_counter(filename):
"""
统计文本文件中单词出现的频率,并返回出现频率最高的10个单词
"""
# 初始化一个空字典来存储单词计数
word_count = {}
try:
# 打开并读取文件
with open(filename, 'r', encoding='utf-8') as file:
# 读取所有内容并转换为小写
text = file.read().lower()
# 去除标点符号(简化版,实际可能需要更多处理)
for char in ',.!?":;()[]{}':
text = text.replace(char, ' ')
# 按空格分割单词
words = text.split()
# 统计每个单词出现的次数
for word in words:
word_count[word] = word_count.get(word, 0) + 1
except FileNotFoundError:
print(f"错误:找不到文件 '{filename}'")
return {}
# 按频率排序并返回前10个单词
sorted_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
return dict(sorted_words[:10])
# 测试函数
if __name__ == "__main__":
# 假设我们有一个名为'sample.txt'的文件
result = word_frequency_counter('sample.txt')
print("出现频率最高的10个单词:")
for word, count in result.items():
print(f"'{word}': {count}次")如何创建测试文件
可以创建一个示例文本文件来测试上面的代码:
# 创建一个示例文本文件
with open('sample.txt', 'w', encoding='utf-8') as f:
f.write("""
Python是一种广泛使用的解释型、高级和通用的编程语言。Python支持多种编程范式,
包括面向对象、命令式、函数式和过程式编程。它拥有动态类型系统和垃圾回收功能,
能够自动管理内存使用,并且具有一个庞大而全面的标准库。Python由Guido van Rossum创建,
第一版发布于1991年。Python的设计哲学强调代码的可读性和简洁的语法,尤其是使用空格缩进来划分代码块。
Python编程语言经常被称为胶水语言,它可以把用其他语言制作的各种模块连接在一起。
""")项目优化与拓展
- 增加停用词过滤:忽略常见但无意义的词如"the"、"is"、"and"等
- 添加词干提取:将不同形式的单词(如"run"、"runs"、"running")归为同一个词
- 可视化结果:使用matplotlib绘制词频柱状图
# 添加停用词过滤功能
def word_frequency_counter_improved(filename, stopwords=None):
"""
增强版单词频率统计,支持停用词过滤
"""
if stopwords is None:
stopwords = {"的", "了", "和", "在", "是", "我", "有", "与"}
word_count = {}
try:
with open(filename, 'r', encoding='utf-8') as file:
text = file.read().lower()
for char in ',.!?":;()[]{}':
text = text.replace(char, ' ')
words = text.split()
# 过滤停用词
filtered_words = [word for word in words if word not in stopwords]
for word in filtered_words:
word_count[word] = word_count.get(word, 0) + 1
except FileNotFoundError:
print(f"错误:找不到文件 '{filename}'")
return {}
sorted_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
return dict(sorted_words[:10])学习经验总结
完成这一天的学习后,应该掌握了Python的基础知识。