Python基础教程
Python是一种高级、解释型、通用的编程语言,由Guido van Rossum于1991年首次发布。它以简洁易读的语法著称,支持多种编程范式,包括面向对象、命令式、函数式和过程式编程。
Python特点:
- 易读易学
- 跨平台
- 丰富的标准库
- 支持多种编程范式
- 动态类型系统
- 自动内存管理
- 庞大的社区支持
Python应用领域:
- Web开发
- 数据分析与科学计算
- 人工智能与机器学习
- 自动化脚本
- 游戏开发
- 网络爬虫
- 系统运维
安装与环境配置
安装Python
- 访问Python官网
- 下载适合你操作系统的版本
- 运行安装程序,勾选"Add Python to PATH"选项
- 完成安装
验证安装
打开终端/命令行,输入:
python --version
# 或
python3 --version
开发环境推荐
- IDLE (Python自带)
- PyCharm
- VS Code
- Jupyter Notebook
- Sublime Text
基础语法
Hello World
print("Hello, World!")
注释
# 这是单行注释
"""
这是多行注释
可以跨越多行
"""
'''
这也是多行注释
使用单引号
'''
变量与赋值
x = 10 # 整数
y = 3.14 # 浮点数
name = "Alice" # 字符串
is_active = True # 布尔值
# 多重赋值
a, b, c = 1, 2, 3
# 交换变量值
a, b = b, a
输入输出
# 输入
name = input("请输入你的名字: ")
# 输出
print("你好,", name)
print(f"你好, {name}") # f-string (Python 3.6+)
数据类型
基本数据类型
- 整数 (
int
) - 浮点数 (
float
) - 布尔值 (
bool
) - 字符串 (
str
) - 空值 (
NoneType
)
数字类型
# 整数
a = 10
b = -5
c = 0b1010 # 二进制
d = 0o12 # 八进制
e = 0xA # 十六进制
# 浮点数
f = 3.14
g = 2.0e3 # 2000.0
# 复数
h = 3 + 4j
# 类型转换
int_num = int(3.9) # 3 (截断小数部分)
float_num = float(5) # 5.0
字符串
s1 = '单引号字符串'
s2 = "双引号字符串"
s3 = '''多行
字符串'''
s4 = """这也是
多行字符串"""
# 字符串操作
name = "Alice"
greeting = "Hello, " + name # 拼接
length = len(name) # 长度
first_char = name[0] # 索引
last_char = name[-1] # 负索引
sub = name[1:4] # 切片 (lic)
# 字符串方法
"hello".upper() # "HELLO"
"HELLO".lower() # "hello"
" hello ".strip() # "hello"
"a,b,c".split(",") # ['a', 'b', 'c']
" ".join(["a", "b"]) # "a b"
"hello".find("ll") # 2
"hello".replace("l", "x") # "hexxo"
布尔类型
t = True
f = False
# 布尔运算
print(True and False) # False
print(True or False) # True
print(not True) # False
复合数据类型
- 列表 (
list
) - 元组 (
tuple
) - 集合 (
set
) - 字典 (
dict
)
列表 (List)
# 创建列表
numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'cherry']
mixed = [1, 'hello', 3.14, True]
# 列表操作
numbers[0] # 1 (索引)
numbers[-1] # 5 (最后一个元素)
numbers[1:3] # [2, 3] (切片)
len(numbers) # 5 (长度)
3 in numbers # True (成员检查)
# 列表方法
numbers.append(6) # 添加元素
numbers.insert(1, 1.5) # 在指定位置插入
numbers.remove(3) # 删除第一个匹配项
popped = numbers.pop() # 删除并返回最后一个元素
numbers.sort() # 排序
numbers.reverse() # 反转
numbers.copy() # 浅拷贝
numbers.clear() # 清空列表
# 列表推导式
squares = [x**2 for x in range(10)]
元组 (Tuple)
# 创建元组
point = (10, 20)
colors = ('red', 'green', 'blue')
single = (5,) # 单元素元组需要逗号
# 元组操作
point[0] # 10
x, y = point # 解包
len(colors) # 3
'red' in colors # True
# 元组是不可变的
# point[0] = 5 # 会报错
集合 (Set)
# 创建集合
primes = {2, 3, 5, 7}
evens = set([0, 2, 4, 6, 8])
# 集合操作
primes.add(11) # 添加元素
primes.remove(2) # 移除元素,不存在会报错
primes.discard(3) # 移除元素,不存在不会报错
len(primes) # 大小
# 集合运算
a = {1, 2, 3}
b = {2, 3, 4}
a | b # 并集 {1, 2, 3, 4}
a & b # 交集 {2, 3}
a - b # 差集 {1}
a ^ b # 对称差集 {1, 4}
# 集合推导式
squares = {x**2 for x in range(10)}
字典 (Dict)
# 创建字典
person = {'name': 'Alice', 'age': 25}
scores = dict(math=90, physics=85)
# 字典操作
person['name'] # 'Alice'
person['age'] = 26 # 修改值
person['city'] = 'NY' # 添加新键值对
'name' in person # True
len(person) # 3
# 字典方法
person.keys() # 所有键
person.values() # 所有值
person.items() # 所有键值对
person.get('name', 'Unknown') # 安全获取
person.pop('age') # 删除并返回
person.update({'age': 27, 'job': 'engineer'}) # 批量更新
person.clear() # 清空
# 字典推导式
squares = {x: x**2 for x in range(5)}
控制流程
条件语句
# if语句
age = 18
if age < 18:
print("未成年")
elif age == 18:
print("刚好成年")
else:
print("已成年")
# 三元表达式
status = "成年" if age >= 18 else "未成年"
循环语句
while循环
count = 0
while count < 5:
print(count)
count += 1
else:
print("循环结束")
for循环
# 遍历序列
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit)
# 遍历字典
person = {'name': 'Alice', 'age': 25}
for key, value in person.items():
print(f"{key}: {value}")
# range函数
for i in range(5): # 0到4
print(i)
for i in range(2, 10, 2): # 2到8,步长2
print(i)
# 带索引的遍历
for index, fruit in enumerate(fruits):
print(index, fruit)
循环控制
# break
for i in range(10):
if i == 5:
break
print(i)
# continue
for i in range(10):
if i % 2 == 0:
continue
print(i)
# pass (占位语句)
for i in range(5):
pass # 什么都不做
函数
定义函数
def greet(name):
"""这是一个问候函数"""
return f"Hello, {name}!"
# 调用函数
message = greet("Alice")
print(message)
参数与返回值
# 默认参数
def power(base, exponent=2):
return base ** exponent
# 关键字参数
power(exponent=3, base=2)
# 可变参数
def sum_all(*args):
return sum(args)
sum_all(1, 2, 3, 4)
# 关键字可变参数
def print_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info(name="Alice", age=25)
# 返回值可以是多个
def min_max(numbers):
return min(numbers), max(numbers)
smallest, largest = min_max([3, 1, 4, 1, 5, 9])
作用域
x = 10 # 全局变量
def foo():
y = 5 # 局部变量
global x
x = 20 # 修改全局变量
foo()
print(x) # 20
Lambda函数
square = lambda x: x ** 2
square(5) # 25
# 常用于排序等高阶函数
pairs = [(1, 'one'), (2, 'two'), (3, 'three')]
pairs.sort(key=lambda pair: pair[1])
装饰器
def my_decorator(func):
def wrapper():
print("装饰器: 函数调用前")
func()
print("装饰器: 函数调用后")
return wrapper
@my_decorator
def say_hello():
print("Hello!")
say_hello()
模块与包
模块
# 导入整个模块
import math
print(math.sqrt(16))
# 导入特定内容
from math import pi, cos
print(cos(pi))
# 别名
import numpy as np
from math import factorial as fac
# 导入所有内容 (不推荐)
from math import *
创建模块
- 创建一个.py文件,例如
mymodule.py
# mymodule.py
def greet(name):
return f"Hello, {name}!"
PI = 3.14159
- 在其他文件中使用
import mymodule
print(mymodule.greet("Alice"))
print(mymodule.PI)
包
包是一个包含多个模块的目录,必须包含__init__.py
文件。
目录结构:
mypackage/
__init__.py
module1.py
module2.py
subpackage/
__init__.py
module3.py
使用:
from mypackage import module1
from mypackage.subpackage import module3
面向对象编程
类与对象
class Dog:
# 类属性
species = "Canis familiaris"
# 初始化方法
def __init__(self, name, age):
self.name = name # 实例属性
self.age = age
# 实例方法
def description(self):
return f"{self.name} is {self.age} years old"
def speak(self, sound):
return f"{self.name} says {sound}"
# 创建实例
buddy = Dog("Buddy", 9)
print(buddy.description())
print(buddy.speak("Woof"))
继承
class Bulldog(Dog):
def run(self, speed):
return f"{self.name} runs at {speed} mph"
# 方法重写
class Dachshund(Dog):
def speak(self, sound="Yap"):
return super().speak(sound)
# 多重继承
class A:
pass
class B:
pass
class C(A, B):
pass
特殊方法
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __str__(self):
return f"Vector({self.x}, {self.y})"
v1 = Vector(2, 4)
v2 = Vector(3, 1)
print(v1 + v2) # Vector(5, 5)
属性装饰器
class Circle:
def __init__(self, radius):
self._radius = radius
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
if value < 0:
raise ValueError("Radius cannot be negative")
self._radius = value
@property
def area(self):
return 3.14 * self._radius ** 2
c = Circle(5)
print(c.radius) # 5
print(c.area) # 78.5
c.radius = 10
异常处理
基本异常处理
try:
result = 10 / 0
except ZeroDivisionError:
print("不能除以零")
except (TypeError, ValueError) as e:
print(f"类型或值错误: {e}")
except Exception as e:
print(f"未知错误: {e}")
else:
print("没有发生异常")
finally:
print("无论是否异常都会执行")
抛出异常
def validate_age(age):
if age < 0:
raise ValueError("年龄不能为负数")
return age
try:
validate_age(-5)
except ValueError as e:
print(e)
自定义异常
class MyError(Exception):
def __init__(self, message):
self.message = message
super().__init__(message)
try:
raise MyError("自定义错误")
except MyError as e:
print(e.message)
文件操作
文件读写
# 写入文件
with open('example.txt', 'w', encoding='utf-8') as f:
f.write("Hello, World!\n")
f.write("这是第二行\n")
# 读取文件
with open('example.txt', 'r', encoding='utf-8') as f:
content = f.read() # 读取全部内容
# 或者逐行读取
for line in f:
print(line.strip())
# 追加模式
with open('example.txt', 'a') as f:
f.write("追加的内容\n")
文件操作常用方法
import os
# 检查文件是否存在
os.path.exists('example.txt')
# 重命名文件
os.rename('old.txt', 'new.txt')
# 删除文件
os.remove('file_to_delete.txt')
# 目录操作
os.mkdir('new_dir')
os.listdir('.') # 列出目录内容
os.rmdir('empty_dir') # 删除空目录
标准库介绍
os模块
import os
# 系统信息
os.name # 操作系统名称
os.environ # 环境变量
os.getcwd() # 当前工作目录
os.chdir('/path/to/dir') # 改变工作目录
# 路径操作
os.path.join('dir', 'file.txt') # 跨平台路径拼接
os.path.abspath('file.txt') # 绝对路径
os.path.dirname('/path/to/file') # 目录名
os.path.basename('/path/to/file') # 文件名
os.path.splitext('file.txt') # 分割扩展名
sys模块
import sys
sys.argv # 命令行参数
sys.exit(1) # 退出程序
sys.version # Python版本
sys.path # 模块搜索路径
sys.stdin # 标准输入
sys.stdout # 标准输出
sys.stderr # 标准错误
datetime模块
from datetime import datetime, date, time, timedelta
# 当前时间
now = datetime.now()
today = date.today()
# 创建特定时间
dt = datetime(2023, 5, 15, 14, 30)
d = date(2023, 5, 15)
t = time(14, 30)
# 时间运算
tomorrow = today + timedelta(days=1)
last_week = today - timedelta(weeks=1)
# 格式化
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
parsed = datetime.strptime("2023-05-15", "%Y-%m-%d")
json模块
import json
# Python对象转JSON字符串
data = {'name': 'Alice', 'age': 25}
json_str = json.dumps(data)
# JSON字符串转Python对象
parsed_data = json.loads(json_str)
# 文件操作
with open('data.json', 'w') as f:
json.dump(data, f)
with open('data.json', 'r') as f:
loaded_data = json.load(f)
re模块 (正则表达式)
import re
# 匹配
match = re.search(r'\d+', 'abc123def')
if match:
print(match.group()) # 123
# 查找所有
numbers = re.findall(r'\d+', 'a1b22c333') # ['1', '22', '333']
# 替换
text = re.sub(r'\s+', ' ', 'too many spaces') # 'too many spaces'
# 分割
parts = re.split(r'[,;]', 'a,b;c') # ['a', 'b', 'c']
# 编译正则表达式
pattern = re.compile(r'\d{3}-\d{4}')
match = pattern.search('电话: 123-4567')
常用第三方库
requests (HTTP请求)
import requests
# GET请求
response = requests.get('https://api.github.com')
print(response.status_code)
print(response.json())
# POST请求
payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('https://httpbin.org/post', data=payload)
# 带headers和参数
headers = {'User-Agent': 'my-app'}
params = {'q': 'python'}
response = requests.get('https://www.google.com/search',
headers=headers,
params=params)
numpy (数值计算)
import numpy as np
# 创建数组
a = np.array([1, 2, 3])
b = np.zeros((3, 3)) # 3x3零矩阵
c = np.arange(10) # 0到9的数组
# 数组运算
a + 1 # 每个元素加1
a * 2 # 每个元素乘2
np.dot(a, a) # 点积
# 矩阵运算
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
A @ B # 矩阵乘法
# 统计
np.mean(a) # 平均值
np.max(a) # 最大值
np.std(a) # 标准差
pandas (数据分析)
import pandas as pd
# 创建DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['NY', 'LA', 'Chicago']}
df = pd.DataFrame(data)
# 数据操作
df.head() # 查看前几行
df.describe() # 统计摘要
df['Age'].mean() # 计算平均年龄
df[df['Age'] > 30] # 过滤
# 读写CSV
df.to_csv('people.csv', index=False)
df = pd.read_csv('people.csv')
matplotlib (数据可视化)
import matplotlib.pyplot as plt
# 折线图
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Simple Plot')
plt.show()
# 散点图
plt.scatter(x, y)
# 柱状图
names = ['A', 'B', 'C']
values = [10, 20, 30]
plt.bar(names, values)
# 多图
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot(x, y)
ax2.scatter(x, y)
flask (Web框架)
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/')
def home():
return "Welcome to my Flask app!"
@app.route('/greet', methods=['GET'])
def greet():
name = request.args.get('name', 'World')
return f"Hello, {name}!"
@app.route('/api/data', methods=['POST'])
def receive_data():
data = request.json
return jsonify({"status": "success", "data": data})
if __name__ == '__main__':
app.run(debug=True)
Python进阶
生成器
def count_up_to(max):
count = 1
while count <= max:
yield count
count += 1
counter = count_up_to(5)
for num in counter:
print(num)
# 生成器表达式
squares = (x**2 for x in range(10))
迭代器
class CountDown:
def __init__(self, start):
self.current = start
self.end = 0
def __iter__(self):
return self
def __next__(self):
if self.current <= self.end:
raise StopIteration
else:
self.current -= 1
return self.current + 1
for num in CountDown(5):
print(num)
装饰器高级用法
from functools import wraps
def log_activity(func):
@wraps(func)
def wrapper(*args, **kwargs):
print(f"开始执行: {func.__name__}")
result = func(*args, **kwargs)
print(f"完成执行: {func.__name__}")
return result
return wrapper
def validate_input(min_val, max_val):
def decorator(func):
@wraps(func)
def wrapper(num):
if not (min_val <= num <= max_val):
raise ValueError(f"输入必须在 {min_val} 和 {max_val} 之间")
return func(num)
return wrapper
return decorator
@log_activity
@validate_input(1, 100)
def square(num):
return num ** 2
上下文管理器
# 基于类的上下文管理器
class FileManager:
def __init__(self, filename, mode):
self.filename = filename
self.mode = mode
self.file = None
def __enter__(self):
self.file = open(self.filename, self.mode)
return self.file
def __exit__(self, exc_type, exc_val, exc_tb):
if self.file:
self.file.close()
with FileManager('test.txt', 'w') as f:
f.write('Hello')
# 基于生成器的上下文管理器
from contextlib import contextmanager
@contextmanager
def file_manager(filename, mode):
try:
f = open(filename, mode)
yield f
finally:
f.close()
with file_manager('test.txt', 'w') as f:
f.write('Hello again')
多线程与多进程
# 多线程
import threading
def print_numbers():
for i in range(5):
print(i)
thread = threading.Thread(target=print_numbers)
thread.start()
thread.join()
# 线程池
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=3) as executor:
executor.submit(print_numbers)
executor.submit(print_numbers)
# 多进程
import multiprocessing
def worker(num):
print(f'Worker: {num}')
processes = []
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
processes.append(p)
p.start()
for p in processes:
p.join()
异步编程 (asyncio)
import asyncio
async def fetch_data():
print("开始获取数据")
await asyncio.sleep(2) # 模拟IO操作
print("数据获取完成")
return {"data": 123}
async def main():
task1 = asyncio.create_task(fetch_data())
task2 = asyncio.create_task(fetch_data())
await task1
await task2
asyncio.run(main())
最佳实践
代码风格 (PEP 8)
- 使用4个空格缩进
- 每行不超过79个字符
- 导入应该分组,标准库、第三方库、本地应用/库,各组之间用空行分隔
- 函数和类定义之间用两个空行分隔
- 类内方法定义之间用一个空行分隔
- 使用小写字母和下划线命名变量和函数
- 使用驼峰命名法命名类名
- 常量使用全大写字母和下划线
文档字符串
def calculate_area(width, height):
"""计算矩形面积
参数:
width (float): 矩形的宽度
height (float): 矩形的高度
返回:
float: 矩形的面积
"""
return width * height
单元测试
import unittest
def add(a, b):
return a + b
class TestAdd(unittest.TestCase):
def test_add_positive(self):
self.assertEqual(add(2, 3), 5)
def test_add_negative(self):
self.assertEqual(add(-1, -1), -2)
def test_add_zero(self):
self.assertEqual(add(0, 0), 0)
if __name__ == '__main__':
unittest.main()
虚拟环境
# 创建虚拟环境
python -m venv myenv
# 激活 (Windows)
myenv\Scripts\activate
# 激活 (macOS/Linux)
source myenv/bin/activate
# 停用
deactivate
项目结构
my_project/
│
├── my_package/ # 主包
│ ├── __init__.py
│ ├── module1.py
│ └── module2.py
│
├── tests/ # 测试代码
│ ├── __init__.py
│ ├── test_module1.py
│ └── test_module2.py
│
├── docs/ # 文档
│ └── documentation.md
│
├── requirements.txt # 依赖列表
├── setup.py # 安装脚本
└── README.md # 项目说明
性能优化技巧
- 使用列表推导式代替循环
- 使用生成器处理大数据集
- 使用
join()
拼接字符串 - 使用局部变量比全局变量快
- 使用内置函数和库
- 避免不必要的对象创建
- 使用
if x in y
检查成员关系 - 使用
functools.lru_cache
缓存函数结果
常见陷阱与避免方法
- 可变默认参数
# 错误
def append_to(element, lst=[]):
lst.append(element)
return lst
# 正确
def append_to(element, lst=None):
if lst is None:
lst = []
lst.append(element)
return lst
- 在循环中修改迭代对象
# 错误
lst = [1, 2, 3, 4]
for item in lst:
if item % 2 == 0:
lst.remove(item)
# 正确
lst = [x for x in lst if x % 2 != 0]
- 变量作用域混淆
# 错误
x = 10
def foo():
print(x)
x = 20
# 正确
x = 10
def foo():
nonlocal x # 或 global x
print(x)
x = 20
- 浅拷贝与深拷贝
import copy
# 浅拷贝
lst1 = [1, [2, 3]]
lst2 = lst1.copy() # 或 list(lst1) 或 lst1[:]
lst2[1][0] = 99 # 会影响lst1
# 深拷贝
lst3 = copy.deepcopy(lst1)
lst3[1][0] = 100 # 不会影响lst1
==
与is
的区别
a = [1, 2, 3]
b = a
c = [1, 2, 3]
a == b # True
a is b # True
a == c # True
a is c # False
这份教程涵盖了Python编程的各个方面,从基础语法到高级特性。要成为Python专家,建议:
- 多写代码,实践是最好的学习方式
- 阅读优秀的开源代码
- 参与开源项目
- 学习Python标准库
- 关注Python社区和最新发展
祝你Python编程愉快!