# Python基础

# 语言概述

Python 是解释型、动态类型、支持多范式的语言,强调可读性和简洁性。广泛应用于 Web、数据分析、自动化、AI 等。

# 环境与运行

python3 --version
python3 script.py
python3 -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate
pip install package
pip freeze > requirements.txt
1
2
3
4
5
6

# 变量与基本类型

x = 10
f = 3.14
s = "hello"
b = True
n = None

type(x)
isinstance(x, int)
1
2
3
4
5
6
7
8

整数无上限(受内存限制);除法 / 得浮点,// 整除,% 取余,** 幂。

# 字符串

s = "hello"
s = 'hello'
s = """多行
字符串"""
s = r"\n 原始字符串"

s[0]
s[-1]
s[1:4]
s[:3]
s[2:]
s[::-1]

len(s)
s + " world"
s * 2
"he" in s
s.upper()
s.lower()
s.strip()
s.split(',')
','.join(['a', 'b'])
s.replace('l', 'L')
s.format(name='Tom')
f"{s} world"
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

# 列表

lst = [1, 2, 3]
lst = list(range(5))
lst[0]
lst[-1]
lst[1:3]
lst.append(4)
lst.extend([5, 6])
lst.insert(0, 0)
lst.remove(2)
lst.pop()
lst.pop(0)
lst.index(3)
lst.count(2)
lst.sort()
lst.reverse()
lst.clear()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

列表推导式:[x*2 for x in lst][x for x in lst if x > 0]

# 元组

t = (1, 2, 3)
t = 1, 2, 3
t[0]
a, b, c = t
1
2
3
4

不可变,可哈希时可作字典键;常用于返回多值、保护数据不被修改。

# 字典

d = {'a': 1, 'b': 2}
d = dict(a=1, b=2)
d['a']
d.get('c', 0)
d['c'] = 3
del d['c']
'a' in d
d.keys()
d.values()
d.items()

{k: v*2 for k, v in d.items()}
1
2
3
4
5
6
7
8
9
10
11
12

# 集合

s = {1, 2, 3}
s = set([1, 2, 2, 3])
s.add(4)
s.remove(1)
s.discard(1)
1 in s
s1 | s2
s1 & s2
s1 - s2
1
2
3
4
5
6
7
8
9

无序、不重复,用于去重和集合运算。

# 控制流

if x > 0:
    pass
elif x == 0:
    pass
else:
    pass

for i in range(10):
    print(i)
for k, v in d.items():
    print(k, v)

while condition:
    break
    continue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 函数

def greet(name, greeting='Hello'):
    return f"{greeting}, {name}"

def f(*args, **kwargs):
    pass

lambda x: x * 2
1
2
3
4
5
6
7

*args 为元组,**kwargs 为字典。参数顺序:位置参数、*args、默认参数、**kwargs。

# 文件操作

with open('file.txt', 'r', encoding='utf-8') as f:
    content = f.read()
    lines = f.readlines()
    for line in f:
        print(line.strip())

with open('out.txt', 'w', encoding='utf-8') as f:
    f.write('hello')
1
2
3
4
5
6
7
8

模式:r、w、a、r+、rb、wb。推荐 with 自动关闭。

# 异常处理

try:
    risky()
except ValueError as e:
    print(e)
except (TypeError, KeyError):
    pass
except Exception as e:
    raise
finally:
    cleanup()
1
2
3
4
5
6
7
8
9
10

# 类与对象

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

    def greet(self):
        return f"Hi, {self.name}"

    @classmethod
    def from_birth(cls, name, year):
        return cls(name)

    @staticmethod
    def is_valid_name(name):
        return len(name) > 0

class Student(Person):
    def __init__(self, name, grade):
        super().__init__(name)
        self.grade = grade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 模块与包

# mymodule.py
def fn():
    pass

# main.py
import mymodule
from mymodule import fn
import mymodule as m
1
2
3
4
5
6
7
8

包:含 init.py 的目录;可 from package import module

# 常用标准库

  • os / pathlib:路径与文件系统
  • sys:系统参数、stdin/stdout
  • json:JSON 读写
  • re:正则
  • datetime:日期时间
  • collections:defaultdict、Counter、deque
  • itertools:迭代工具
  • argparse:命令行参数