Python Basics Notes — Variables, Data Types, Strings, and Booleans
Python 基础笔记 — 变量、数据类型、字符串与布尔值
1. Variables / 变量
- Python does NOT require declaring variable types. Variables are created when first assigned.
- Variable naming rules / 变量命名规则:
- Must start with a letter or underscore / 必须以字母或下划线开头。
- Cannot start with a number / 不能以数字开头。
- Can only contain letters, numbers, and underscores / 只能包含字母、数字和下划线。
- Case-sensitive (age, Age, AGE are different) / 区分大小写(age、Age、AGE是不同变量)。
- Cannot use Python keywords / 不能使用Python关键字。
- Multi-word variable naming / 多单词变量命名:
- Camel Case 驼峰式: myVariableName
- Pascal Case 帕斯卡式: MyVariableName
- Snake Case 蛇形: my_variable_name
- Multiple assignments / 多变量赋值:
x, y, z = 1, 2, 3
a=b=c=5
- Unpacking / 拆包:
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
2. Data Types / 数据类型
- Text type / 文本类型: str
- Numeric types / 数值类型: int (integer), float (floating point), complex (complex number)
- Sequence types / 序列类型: list (列表), tuple (元组), range (范围)
- Check data type / 查看变量类型:
print(type(x))
- Set data type / 设置数据类型:
x = str("Hello")
y = int(20)
z = float(20.5)
- Type conversion / 类型转换:
a = float(1) # int to float 整数转浮点数
b = int(2.8) # float to int 浮点数转整数(小数部分被截断)
c = complex(1) # int to complex 整数转复数
- Note / 注意: Complex numbers cannot be converted to other numeric types.
3. Random Numbers / 随机数
- Use the random module to generate random numbers / 使用 random 模块生成随机数:
import random
print([Link](1, 10)) # Generates random integer from 1 to 9
4. Strings / 字符串
- Strings can be enclosed in single or double quotes / 字符串可以用单引号或双引号括起:
'hello' and "hello" are the same
- Quotes inside strings / 字符串内可包含不同类型的引号:
print("It's OK")
print('He said "Hello"')
- Multi-line strings with triple quotes / 多行字符串用三重引号:
a = """Multi-line
string example"""
- Strings are arrays of characters; indexing starts at 0 / 字符串是字符数组,索引从0开始:
a = "Hello"
print(a[1]) # Outputs 'e'
- Loop through a string / 遍历字符串:
for c in "banana":
print(c)
- String length / 字符串长度:
print(len(a))
- Check if substring exists / 检查字符串是否包含某内容:
print("free" in txt) # True or False
print("expensive" not in txt)
- Use in if statement / 在 if 条件中使用:
if "free" in txt:
print("Yes, 'free' is present.")
- Slicing strings / 字符串切片:
Syntax: string[start:end], includes start, excludes end
例子 / Examples:
b = "Hello, World!"
print(b[2:5]) # Outputs 'llo'
print(b[:5]) # From start to index 4, outputs 'Hello'
print(b[2:]) # From index 2 to end, outputs 'llo, World!'
print(b[-5:-2]) # Negative indexing, outputs 'orl'
print(b[::2]) # Step 2, every other character
print(b[::-1]) # Reverse string
5. Booleans / 布尔值
- Booleans have two values: True or False / 布尔值只有两个值:True 或 False
- Obtained by comparison expressions / 通过比较表达式获得:
print(10 > 9) # True
print(10 == 9) # False
- Used in if statements / if 语句中使用:
if b > a:
print("b is greater than a")
else:
print("b is not greater than a")
- bool() function evaluates any value to a Boolean / bool() 函数评估任意值,返回布尔值:
print(bool("Hello")) # True, non-empty string
print(bool(0)) # False, zero