《Python期末备考全攻略:高分秘籍与实用技巧大合集!》

《Python期末备考全攻略:高分秘籍与实用技巧大合集!》

  • 1 Python 基础语法
    • 1.1 变量与数据类型
    • 1.2 条件语句
    • 1.3 循环语句
  • 2. 常见数据结构
    • 2.1 列表
    • 2.2 元组
    • 2.3 字典
    • 2.4 集合
  • 3. 函数与模块
    • 3.1 自定义函数
    • 3.2 匿名函数(lambda)
    • 3.3 标准库与第三方库
  • 4. 文件操作
    • 4.1 文件读写操作
  • 5. 面向对象编程
    • 5.1 类与对象
    • 5.2 继承与多态
  • 6. 综合练习题与答案

《Python期末备考全攻略:高分秘籍与实用技巧大合集!》_第1张图片

1 Python 基础语法

1.1 变量与数据类型

数据类型 示例 特点
整数(int) x = 10 不带小数点的数字
浮点数(float) y = 3.14 带小数点的数字
字符串(str) s = “hello” 用引号括起的字符序列
布尔值(bool) flag = True 值为 True 或 False
列表(list) lst = [1, 2, 3] 可变的有序集合
字典(dict) d = {‘a’: 1} 键值对构成的无序集合

代码示例:

# 检查变量类型
x = 10
y = 3.14
s = "hello"
flag = True
print(type(x))  # 输出:
print(type(y))  # 输出:
print(type(s))  # 输出:
print(type(flag))  # 输出:

1.2 条件语句

if-elif-else 结构:

score = 85
if score >= 90:
    print("优秀")
elif 75 <= score < 90:
    print("良好")
else:
    print("及格或不及格")

注意:
条件后要加冒号 :。
缩进表示代码块的从属关系。

1.3 循环语句

for 循环:

# 遍历一个列表
for item in [1, 2, 3]:
    print(item)

# 使用 range()
for i in range(1, 6):
    print(i)  # 输出 1 到 5

while 循环:

count = 3
while count > 0:
    print(f"倒计时:{count}")
    count -= 1

2. 常见数据结构

2.1 列表

操作总结:

操作 示例 结果
创建列表 lst = [1, 2, 3] [1, 2, 3]
索引访问 lst[0] 1
切片 lst[1:] [2, 3]
添加元素 lst.append(4) [1, 2, 3, 4]
删除元素 lst.remove(2) [1, 3, 4]

代码示例:

lst = [1, 2, 3]
lst.append(4)  # 添加元素
print(lst)  # 输出:[1, 2, 3, 4]
lst.remove(2)  # 删除元素
print(lst)  # 输出:[1, 3, 4]

2.2 元组

元组与列表类似,但元组是不可变的。

# 创建元组
tup = (1, 2, 3)
print(tup[0])  # 输出:1

2.3 字典

字典是键值对的集合,常用来存储结构化数据。

操作 示例 结果
创建字典 d = {‘a’: 1, ‘b’: 2} {‘a’: 1, ‘b’: 2}
访问值 d[‘a’] 1
添加键值对 d[‘c’] = 3 {‘a’: 1, ‘b’: 2, ‘c’: 3}

代码示例:

d = {'name': 'Alice', 'age': 25}
print(d['name'])  # 输出:Alice
d['city'] = 'Beijing'  # 添加键值对
print(d)  # 输出:{'name': 'Alice', 'age': 25, 'city': 'Beijing'}

2.4 集合

集合用于存储不重复的元素。

s = {1, 2, 3, 3}
s.add(4)
print(s)  # 输出:{1, 2, 3, 4}

3. 函数与模块

3.1 自定义函数

def greet(name):
    """简单的问候函数"""
    return f"Hello, {name}!"

print(greet("Alice"))  # 输出:Hello, Alice!

3.2 匿名函数(lambda)

# 计算两个数的乘积
multiply = lambda a, b: a * b
print(multiply(3, 4))  # 输出:12

3.3 标准库与第三方库

功能 示例
math 数学运算 math.sqrt(16) 输出:4.0
random 随机数生成 random.randint(1, 10) 输出随机整数

4. 文件操作

4.1 文件读写操作

# 写入文件
with open('example.txt', 'w') as f:
    f.write("Python 是很棒的!")
    # 读取文件
with open('example.txt', 'r') as f:
    content = f.read()
    print(content)  # 输出:Python 是很棒的!

5. 面向对象编程

5.1 类与对象

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def introduce(self):
        print(f"我叫{self.name},今年{self.age}岁。")

p = Person("张三", 20)
p.introduce()  # 输出:我叫张三,今年20岁。

5.2 继承与多态

class Animal:
    def speak(self):
        print("动物发出声音")

class Dog(Animal):
    def speak(self):
        print("汪汪汪")

d = Dog()
d.speak()  # 输出:汪汪汪

6. 综合练习题与答案

练习题:

  • 问题1:写一个函数,返回输入字符串的逆序字符串。

  • 问题2:实现一个类,支持简单的加减运算。

答案:

# 练习 1
def reverse_string(s):
    return s[::-1]

print(reverse_string("hello"))  # 输出:olleh
# 练习 2
class Calculator:
    def add(self, a, b):
        return a + b

    def subtract(self, a, b):
        return a - b

calc = Calculator()
print(calc.add(10, 5))  # 输出:15
print(calc.subtract(10, 5))  # 输出:5

你可能感兴趣的:(python,windows,开发语言,数据结构)