之前写给朋友的python二级知识点全解,朋友考试通过后支持我分享出来,就放在这了,我敢保证是全网最全的考点汇总
推荐题库用:python2ji.com
全国二级Python考试考点全收录
1. Python 语言基本语法元素
1.1 程序的基本语法元素
1.1.1 程序的框架与缩进
Python 依赖缩进来表示代码块,而不像 C 或 Java 这类语言使用 {}
。
if True:
print("Hello, Python!") # 缩进 4 个空格
1.1.2 注释
# 这是单行注释
"""
这是多行注释
"""
1.1.3 变量、命名和保留字
变量命名需要遵循规则,不能使用 Python 的 33 个保留字,如 if
、while
、return
等。
import keyword
print(keyword.kwlist) # 输出所有保留字
1.1.4 数据类型与赋值
Python 有 6 种基本数据类型:
- 整数(int)
- 浮点数(float)
- 复数(complex)
- 字符串(str)
- 布尔值(bool)
- 空值(None)
x = 10 # int
y = 3.14 # float
z = 3 + 4j # complex
name = "Python" # str
flag = True # bool
1.1.5 库引用
import math # 导入整个 math 库
from math import sqrt # 导入 math 库中的 sqrt 函数
from math import * # 导入 math 库的所有函数
import math as m # 给 math 库起别名 m
1.2 基本输入输出函数
name = input("请输入你的名字:") # 输入
age = eval(input("请输入你的年龄:")) # 使用 eval 解析输入
print(f"你好,{
name},你的年龄是 {
age}") # 输出
1.3 Python 之禅
Python 之禅是 Python 设计哲学的总结,运行 import this
可查看。
import this
1.4 Python 语言特点
- 通用性:可用于 Web 开发、数据分析、人工智能等领域。
- 简洁性:语法简洁,代码易读。
- 高效性:代码量少,开发速度快。
2. 基本数据类型
2.1 数字类型
Python 支持整数、浮点数和复数。
x = 10 # 整数
y = 3.14 # 浮点数
z = 2 + 3j # 复数
2.2 数字类型的运算
Python 提供丰富的数值运算操作符。
print(10 + 5) # 加法
print(10 - 3) # 减法
print(10 * 3) # 乘法
print(10 / 2) # 除法(浮点数)
print(10 // 3) # 整除
print(10 % 3) # 取模
print(2 ** 3) # 指数(幂)
常用数学函数:
print(abs(-5)) # 绝对值
print(pow(2, 3)) # 幂运算
print(round(3.14159, 2)) # 四舍五入
2.3 字符串操作
s = "Hello, Python!"
print(s[0]) # 索引
print(s[0:5]) # 切片
print(s.lower()) # 转小写
print(s.upper()) # 转大写
print(s.replace("Python", "World")) # 替换
print(s.split(",")) # 分割
2.4 类型转换
print(int("10")) # 字符串转整数
print(float("3.14")) # 字符串转浮点数
print(str(100)) # 数字转字符串
3. 程序的控制结构
3.1 顺序结构
代码按照顺序执行。
print("第一步")
print("第二步")
print("第三步")
3.2 分支结构
age = int(input("请输入年龄:"))
if age >= 18:
print("你已成年")
else:
print("你未成年")
3.3 循环结构
# for 循环
for i in range(5):
print(i)
# while 循环
x = 5
while x > 0:
print(x)
x -= 1
3.4 异常处理
try:
num = int(input("请输入整数:"))
print(10 / num)
except ZeroDivisionError:
print("不能除以 0!")
except ValueError:
print("请输入正确的整数!")
4. 组合数据类型
4.1 列表
ls = [1, 2, 3, 4]
print(ls[0]) # 访问元素
ls.append(5) # 添加元素
ls.insert(2, 10) # 指定位置插入
print(ls)
4.2 字典
d = {
"name": "Alice", "age": 25}
print(d["name"]) # 访问键值
print(d.get("age")) # 使用 get 方法
4.3 文件操作
with open("test.txt", "w") as f:
f.write("Hello, file!")
5. 文件操作
Python 提供了强大的文件操作功能,包括文件的打开、读取、写入以及关闭等基本操作。
5.1 打开和关闭文件
Python 使用 open()
函数打开文件,close()
关闭文件。
# 以只读模式 ('r') 打开文件
f = open("example.txt", "r")
content = f.read()
print(content)
f.close()
open()
方法的常见模式:
'r'
只读模式(默认)'w'
写入模式(清空原内容)'a'
追加模式(保留原内容)'x'
创建新文件(文件已存在则报错)'b'
以二进制模式打开't'
以文本模式打开(默认)
使用 with
语句可以自动管理文件资源,避免忘记 close()
。
with open("example.txt", "r") as f:
content = f.read()
print(content) # 文件关闭前读取内容
5.2 读取文件内容
Python 提供了多种读取文件的方法:
with open("example.txt", "r") as f:
print(f.read()) # 读取整个文件
print(f.readline()) # 读取一行
print(f.readlines()) # 读取所有行,返回列表
5.3 写入文件
使用 'w'
或 'a'
模式可以向文件写入内容。
with open("example.txt", "w") as f:
f.write("Hello, Python!\n") # 覆盖原内容
with open("example.txt", "a") as f:
f.write("Appending new line.\n") # 追加内容
5.4 读取和写入 CSV 文件
CSV 文件是一种常见的数据存储格式,可用 csv
库操作。
import csv
# 写入 CSV 文件
with open("data.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["Name", "Age", "City"])
writer.writerow(["Alice", 25, "New York"])
writer.writerow(["Bob", 30, "Los Angeles"])
# 读取 CSV 文件
with open("data.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
print(row)
6. Python 计算生态
Python 计算生态丰富,涵盖了标准库和第三方库,极大地扩展了 Python 的能力。
6.1 标准库
Python 内置了大量标准库,无需额外安装,直接 import
即可使用。
6.1.1 turtle
库(绘图)
turtle
是 Python 内置的绘图库,可以用来绘制简单图形。
import turtle
turtle.pensize(3)
turtle.pencolor("blue")
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.done()
6.1.2 random
库(随机数)
random
可用于生成随机数。
import random
print(random.randint(1, 100)) # 生成 1-100 之间的随机整数
print(random.choice(["apple", "banana", "cherry"])) # 随机选择一个元素
6.1.3 time
库(时间操作)
time
主要用于时间处理和延时操作。
import time
print(time.ctime()) # 获取当前时间
time.sleep(2) # 暂停 2 秒
print("Hello after 2 seconds!")
6.2 第三方库
第三方库可以通过 pip install
进行安装,扩展 Python 功能。
pip install <库名>
6.2.1 requests
(网络请求)
用于发送 HTTP 请求。
import requests
response = requests.get("https://www.python.org")
print(response.status_code)
print(response.text[:500]) # 仅打印前 500 个字符
6.2.2 jieba
(中文分词)
jieba
是一个优秀的中文分词库。
import jieba
text = "我爱编程和Python"
words = jieba.lcut(text)
print(words)
6.2.3 matplotlib
(数据可视化)
matplotlib
用于绘制图表。
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]
plt.plot(x, y)
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.title("Simple Line Chart")
plt.show()
7. 常见第三方库
Python 有大量的第三方库,以下是一些常见库:
领域 | 代表库 |
---|---|
爬虫 | requests 、scrapy 、pyspider |
数据分析 | numpy 、pandas 、scipy |
文本处理 | pdfminer 、python-docx 、beautifulsoup4 |
数据可视化 | matplotlib 、seaborn 、mayavi |
机器学习 | scikit-learn 、TensorFlow 、mxnet |
Web 开发 | Django 、Flask |
游戏开发 | pygame 、Panda3D |
8. 结语
最后发一下我和朋友一致好评的题库:python2ji.com
该题库最大的优势是:不需要下载任何东西,点开就直接写,也支持手机浏览器
完成后可以直接提交,查看得分
如果回答错误,还会告知原因
按照考试的评分机制进行判定,甚至能支持turtle库的在线展示