基础语法与数据类型
变量与赋值
Python 是动态类型语言,无需声明类型:
1 2 3 4
| x = 10 x = "hello" name = "Alice" age = 25
|
多重赋值:
1 2
| a, b, c = 1, 2, 3 x, y = y, x
|
基本数据类型
| 类型 |
示例 |
说明 |
int |
42, -100 |
任意精度整数 |
float |
3.14, 1e-5 |
双精度浮点 |
bool |
True, False |
布尔值 |
str |
"hello", 'world' |
字符串 |
None |
None |
空值,类似 null |
类型检查与转换:
1 2 3 4 5 6 7 8
| type(42) isinstance(x, int)
int("42") float("3.14") str(100) bool(0) bool("")
|
运算符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 10 + 3 10 // 3 10 % 3 2 ** 10
a == b a != b a < b
a and b a or b not a
"x" in "hello" 3 in [1, 2, 3]
a is b a is not b
|
注意:== 比较值,is 比较是否为同一对象。小整数缓存导致 a is b 有时为 True,不要用 is 比较字符串内容。
字符串基础
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| s = "Hello" s = 'World' s = """多行 字符串"""
name = "Alice" msg = f"Hello, {name}!"
"hello".upper() " trim ".strip() "a,b,c".split(",") "-".join(["a", "b"])
|
输入输出
1 2 3
| name = input("请输入姓名: ") print("Hello,", name) print(f"年龄: {age}", end="\n")
|
注释与文档
1 2 3 4 5 6 7 8 9 10 11
|
""" 多行字符串,也可作模块/函数文档 """
def add(a: int, b: int) -> int: """返回两数之和。""" return a + b
print(add.__doc__)
|
类型注解(Type Hints)
Python 3.5+ 支持类型提示,运行时不强制检查,供 IDE 和 mypy 使用:
1 2 3 4 5
| def greet(name: str, times: int = 1) -> str: return (f"Hello, {name}! " * times).strip()
scores: list[int] = [90, 85, 92] user: dict[str, int] = {"age": 25}
|
常量约定
Python 无真正常量,约定全大写表示不应修改:
1 2
| MAX_SIZE = 1024 DEFAULT_TIMEOUT = 30
|
小结
- 动态类型,变量无需声明
- 基本类型:int、float、bool、str、None
- f-string 是字符串格式化的首选方式
- 类型注解提升可读性和工具支持,但不影响运行时行为