目录
数学函数
函数 | 描述 | 示例 |
---|---|---|
abs(x) | 计算x的绝对值 | abs(-7) → 7 |
round(x, [n]) | 四舍五入x到n位小数(默认整数) | round(3.14159) → 3, round(3.14159, 2) → 3.14 |
math.sqrt(x) | 计算x的平方根 | math.sqrt(16) → 4.0 |
math.factorial(x) | 计算x的阶乘 | math.factorial(5) → 120 |
math.pow(x, y) | 计算x的y次幂 | math.pow(2, 3) → 8.0 |
math.log(x) | 计算自然对数ln(x) | math.log(2.718) → 1.0 |
math.exp(x) | 计算e的x次方 | math.exp(1) → 2.718 |
math.sin(x) | 计算x的正弦值(x为弧度) | math.sin(math.pi / 2) → 1.0 |
math.cos(x) | 计算x的余弦值(x为弧度) | math.cos(0) → 1.0 |
示例:
银行家舍入法: 当 number
位于两个数之间且距离相等时,round()
函数将 number
四舍五入到最接近的偶数。
import math
# 绝对值
print(abs(-7)) # 输出:7
# 四舍五入
print(round(3.14159)) # 输出:3
print(round(3.14159, 2)) # 输出:3.14
# 四舍五入到 2 位小数
print(round(2.675, 2)) # 输出:2.68
# 四舍五入到 -1 位(取整到最近的 10)
print(round(1234.5678, -1)) # 输出:1230
# 银行家舍入法:
# 四舍五入到最接近的整数
print(round(4.5)) # 输出:4
print(round(5.5)) # 输出:6
# 其他数学函数
print(math.sqrt(16)) # 输出:4.0
print(math.factorial(5)) # 输出:120
print(math.pow(2, 3)) # 输出:8.0
print(math.log(2.718)) # 输出:1.0
print(math.exp(1)) # 输出:2.718
print(math.sin(math.pi / 2)) # 输出:1.0
print(math.cos(0)) # 输出:1.0
随机函数
函数 | 描述 | 示例 |
---|---|---|
random.random() | 生成一个 [0.0, 1.0) 之间的浮点数 | random.random() → 0.37444887175646646 |
random.randint(a, b) | 生成一个 [a, b] 之间的随机整数 | random.randint(1, 10) → 7 |
random.choice(seq) | 从序列 seq 中随机选择一个元素 | random.choice([‘apple’, ‘banana’]) → ‘banana’ |
random.shuffle(x) | 打乱列表 x 中的元素 | random.shuffle([1, 2, 3]) → [3, 1, 2] |
random.uniform(a, b) | 生成一个 [a, b] 之间的随机浮点数 | random.uniform(1.5, 2.5) → 1.9230775691733008 |
示例:
import random
# 生成随机浮点数
print(random.random()) # 输出:0.37444887175646646
# 生成随机整数
print(random.randint(1, 10)) # 输出:7
# 随机选择
print(random.choice(['apple', 'banana', 'cherry'])) # 输出:'banana'
# 打乱列表
list_to_shuffle = [1, 2, 3]
random.shuffle(list_to_shuffle)
print(list_to_shuffle) # 输出:[3, 1, 2](顺序可能不同)
# 生成随机浮点数
print(random.uniform(1.5, 2.5)) # 输出:1.9230775691733008
字符串函数
函数 | 描述 | 示例 |
---|---|---|
str.upper() | 将字符串转换为大写 | “hello”.upper() → “HELLO” |
str.lower() | 将字符串转换为小写 | “HELLO”.lower() → “hello” |
str.capitalize() | 将字符串的首字母大写,其余字母小写 | “hello world”.capitalize() → “Hello world” |
str.title() | 将每个单词的首字母大写,其余字母小写 | “hello world”.title() → “Hello World” |
str.strip() | 去除字符串两端的空白字符 | " hello ".strip() → “hello” |
str.lstrip() | 去除字符串左端的空白字符 | " hello ".lstrip() → "hello " |
str.rstrip() | 去除字符串右端的空白字符 | " hello “.rstrip() → " hello” |
str.split(sep) | 根据分隔符 sep 将字符串分割成列表 | “a,b,c”.split(“,”) → [“a”, “b”, “c”] |
str.rsplit(sep) | 从右侧开始根据分隔符 sep 分割字符串 | “a,b,c,d”.rsplit(“,”, 2) → [“a,b”, “c”, “d”] |
str.join(iterable) | 将可迭代对象中的元素连接成一个字符串 | “,”.join([“a”, “b”, “c”]) → “a,b,c” |
str.replace(old, new) | 替换字符串中的子串 old 为 new | “hello world”.replace(“world”, “Python”) → “hello Python” |
str.find(sub) | 查找子串 sub 在字符串中的位置 | “hello”.find(“e”) → 1 |
str.rfind(sub) | 从右侧开始查找子串 sub 在字符串中的位置 | “hello”.rfind(“l”) → 3 |
str.index(sub) | 查找子串 sub 在字符串中的位置(如果未找到则引发 ValueError) | “hello”.index(“l”) → 2 |
str.rindex(sub) | 从右侧开始查找子串 sub 在字符串中的位置(如果未找到则引发 ValueError) | “hello”.rindex(“l”) → 3 |
str.startswith(prefix) | 检查字符串是否以 prefix 开头 | “hello”.startswith(“he”) → True |
str.endswith(suffix) | 检查字符串是否以 suffix 结尾 | “hello”.endswith(“lo”) → True |
str.zfill(width) | 用零填充字符串,使其长度达到 width | “42”.zfill(5) → “00042” |
str.isdigit() | 检查字符串是否仅包含数字字符 | “123”.isdigit() → True |
str.isalpha() | 检查字符串是否仅包含字母字符 | “abc”.isalpha() → True |
str.isspace() | 检查字符串是否仅包含空白字符 | " ".isspace() → True |
str.islower() | 检查字符串是否所有字母都是小写 | “hello”.islower() → True |
str.isupper() | 检查字符串是否所有字母都是大写 | “HELLO”.isupper() → True |
示例:
# 字符串转换
text = "hello world"
print(text.upper()) # 输出: "HELLO WORLD"
print(text.lower()) # 输出: "hello world"
print(text.capitalize()) # 输出: "Hello world"
print(text.title()) # 输出: "Hello World"
# 去除空白字符
text_with_spaces = " hello "
print(text_with_spaces.strip()) # 输出: "hello"
print(text_with_spaces.lstrip()) # 输出: "hello "
print(text_with_spaces.rstrip()) # 输出: " hello"
# 分割和连接
csv_string = "a,b,c"
print(csv_string.split(",")) # 输出: ["a", "b", "c"]
print(",".join(["a", "b", "c"])) # 输出: "a,b,c"
# 替换子串
text = "hello world"
print(text.replace("world", "Python")) # 输出: "hello Python"
# 查找子串
print(text.find("e")) # 输出: 1
print(text.rfind("l")) # 输出: 3
print(text.index("l")) # 输出: 2
print(text.rindex("l")) # 输出: 3
# 判断字符串
print(text.startswith("he")) # 输出: True
print(text.endswith("lo")) # 输出: True
print("42".zfill(5)) # 输出: "00042"
print("123".isdigit()) # 输出: True
print("abc".isalpha()) # 输出: True
print(" ".isspace()) # 输出: True
print("hello".islower()) # 输出: True
print("HELLO".isupper()) # 输出: True