python中的运算符
python中常见的运算符
以下是 python 中常见的运算符:
| 分类 | 运算符 | 描述 | 示例 |
|---|---|---|---|
| 算术运算 | + | 加法 | 2 + 3 = 5 |
| - | 减法 | 5 - 2 = 3 | |
| * | 乘法 | 2 * 3 = 6 | |
| / | 除法 | 7 / 2 = 3.5 | |
| // | 整除 | 7 // 2 = 3 | |
| % | 取余 | 7 % 2 = 1 | |
| ** | 幂运算 | 2 ** 3 = 8 | |
| 比较运算 | == | 相等 | 2 == 3 返回 False |
| != | 不相等 | 2 != 3 返回 True | |
| > | 大于 | 2 > 3 返回 False | |
| < | 小于 | 2 < 3 返回 True | |
| >= | 大于等于 | 2 >= 3 返回 False | |
| <= | 小于等于 | 2 <= 3 返回 True | |
| 逻辑运算 | and | 逻辑与 | True and False 返回 False |
| or | 逻辑或 | True or False 返回 True | |
| not | 逻辑非 | not True 返回 False | |
| 位运算 | & | 按位与 | 5 & 3 = 1 |
| | 按位或 | 5 | 3 = 7 | |
| ^ | 按位异或 | 5 ^ 3 = 6 | |
| ~ | 按位取反 | ~5 = -6 | |
| << | 左移 | 5 << 2 = 20 | |
| >> | 右移 | 5 >> 2 = 1 | |
| 赋值运算 | = | 赋值 | x = 5 |
| += | 加法赋值 | x += 2 等同于 x = x + 2 | |
| -= | 减法赋值 | x -= 2 等同于 x = x - 2 | |
*= | 乘法赋值 | x *= 2 等同于 x = x * 2 | |
| /= | 除法赋值 | x /= 2 等同于 x = x / 2 | |
| //= | 整除赋值 | x //= 2 等同于 x = x // 2 | |
| %= | 取余赋值 | x %= 2 等同于 x = x % 2 | |
**= | 幂运算赋值 | x **= 2 等同于 x = x ** 2 | |
| 位运算 | &= | 按位与赋值 | x &= 2 等同于 x = x & 2 |
|= | 按位或赋值 | x |= 2 等同于 x = x | 2 | |
| ^= | 按位异或赋值 | x ^= 2 等同于 x = x ^ 2 | |
| <<= | 左移赋值 | x <<= 2 等同于 x = x << 2 | |
| >>= | 右移赋值 | x >>= 2 等同于 x = x >> 2 |
math运算
| 函数名 | 函数的作用 | 示例 |
|---|---|---|
math.sqrt(x) | 返回给定数字的平方根 | math.sqrt(16) 返回 4.0 |
math.ceil(x) | 返回大于或等于给定数字的最小整数 | math.ceil(3.7) 返回 4 |
math.floor(x) | 返回小于或等于给定数字的最大整数 | math.floor(3.7) 返回 3 |
math.pow(x, y) | 返回x的y次幂 | math.pow(2, 3) 返回 8.0 |
math.sin(x) | 返回给定角度的正弦值 | math.sin(math.pi/2) 返回 1.0 |
math.cos(x) | 返回给定角度的余弦值 | math.cos(math.pi) 返回 -1.0 |
math.tan(x) | 返回给定角度的正切值 | math.tan(math.pi/4) 返回 1.0 |
math.log(x) | 返回给定数字的自然对数 | math.log(10) 返回 2.302585092994046 |
math.log10(x) | 返回给定数字的以10为底的对数 | math.log10(100) 返回 2.0 |
math.exp(x) | 返回给定数字的指数值 | math.exp(1) 返回 2.718281828459045 |
python中的字符串操作符
| 分类 | 名称 | 描述 | 示例 |
|---|---|---|---|
| 操作符 | + | 连接两个字符串 | “Hello” + “World” → “HelloWorld” |
| * | 重复一个字符串多次 | “Hello” * 3 → “HelloHelloHello” | |
| [] | 通过索引获取字符串中的字符 | “Hello”[0] → “H” | |
| [ : ] | 切片字符串,获取指定范围内的子串 | “Hello”[1:4] → “ell” | |
| in | 检查一个字符串是否包含在另一个字符串中 | “Hello” in “Hello World” → True | |
| not in | 检查一个字符串是否不包含在另一个字符串中 | “Python” not in “Hello World” → True | |
| 运算符 | + | 连接两个字符串 | “Hello” + “World” → “HelloWorld” |
| * | 重复一个字符串多次 | “Hello” * 3 → “HelloHelloHello” | |
| == | 检查两个字符串是否相等 | “Hello” == “World” → False | |
| != | 检查两个字符串是否不相等 | “Hello” != “World” → True | |
| < | 检查一个字符串是否小于另一个字符串(按字典顺序比较) | “Hello” < “World” → True | |
| > | 检查一个字符串是否大于另一个字符串(按字典顺序比较) | “Hello” > “World” → False | |
| <= | 检查一个字符串是否小于等于另一个字符串(按字典顺序比较) | “Hello” <= “World” → True | |
| >= | 检查一个字符串是否大于等于另一个字符串(按字典顺序比较) | “Hello” >= “World” → False | |
| in | 检查一个字符串是否包含在另一个字符串中 | “Hello” in “Hello World” → True | |
| not in | 检查一个字符串是否不包含在另一个字符串中 | “Python” not in “Hello World” → True | |
| 函数 | len() | 返回字符串的长度 | len(“Hello”) → 5 |
| str() | 将其他类型的数据转换为字符串 | str(123) → “123” | |
| lower() | 将字符串转换为小写字母 | “Hello”.lower() → “hello” | |
| upper() | 将字符串转换为大写字母 | “Hello”.upper() → “HELLO” | |
| strip() | 去除字符串两端的空格 | " Hello ".strip() → “Hello” | |
| split() | 将字符串按指定的分隔符分割成列表 | “Hello World”.split() → [“Hello”, “World”] | |
| join() | 将列表中的字符串按指定的连接符连接成一个字符串 | “-”.join([“Hello”, “World”]) → “Hello-World” | |
| format() | 格式化字符串,将占位符替换为指定的值 | “My name is {}.”.format(“Alice”) → “My name is Alice.” | |
| replace() | 将字符串中的指定子串替换为新的子串 | “Hello World”.replace(“World”, “Python”) → “Hello Python” |
字符串格式化 % 操作符的用法
| 分类 | 符号 | 描述 | 示例 |
|---|---|---|---|
| 字符串 | %s | 插入字符串 | name = "Alice"print("Hello, %s!" % name)输出: Hello, Alice! |
| 整数 | %d | 插入整数 | age = 25print("I am %d years old." % age)输出: I am 25 years old. |
| 浮点数 | %f | 插入浮点数 | pi = 3.14159print("The value of pi is %f." % pi)输出: The value of pi is 3.141590. |
| 科学计数法 | %e | 插入科学计数法表示的浮点数 | num = 1.23e+6print("The number is %e." % num)输出: The number is 1.230000e+06. |
| 十六进制 | %x | 插入十六进制表示的整数 | num = 255print("The number in hexadecimal is %x." % num)输出: The number in hexadecimal is ff. |
| 百分比 | %% | 插入百分比符号 | percentage = 0.75print("The percentage is %.2f%%." % (percentage * 100))输出: The percentage is 75.00%. |
以上是 % 操作符的一些常见用法。需要注意的是,% 操作符在Python 3.6及更高版本中已经过时,推荐使用更现代的字符串格式化方法,如使用 str.format() 方法或 f-strings。
字符串格式化 format() 方法
| 分类 | 格式 | 描述 | 示例 |
|---|---|---|---|
| 位置参数 | {} | 默认按顺序插入参数 | name = "Alice"age = 25print("My name is {} and I am {} years old.".format(name, age))输出: My name is Alice and I am 25 years old. |
| 关键字参数 | {key} | 按关键字指定参数 | name = "Alice"age = 25print("My name is {name} and I am {age} years old.".format(name=name, age=age))输出: My name is Alice and I am 25 years old. |
| 位置参数索引 | {index} | 按索引指定参数位置 | print("I am {1} years old and my name is {0}.".format(name, age))输出: I am 25 years old and my name is Alice. |
| 格式规范 | {:format} | 使用格式规范控制插入内容的显示 | pi = 3.14159print("The value of pi is {:.2f}.".format(pi))输出: The value of pi is 3.14. |
| 对齐和填充 | {:width} | 指定字段的宽度 | name = "Alice"`print(" |
| 浮点数精度 | {:width.precisionf} | 指定浮点数的精度 | pi = 3.14159print("The value of pi is {:.4f}.".format(pi))输出: The value of pi is 3.1416. |
| 二进制 | {:b} | 插入二进制表示的整数 | num = 10print("The number in binary is {:b}.".format(num))输出: The number in binary is 1010. |
本文详细介绍了Python中的基本运算符、数学运算函数如sqrt、ceil、floor等,以及字符串操作符和格式化方法,包括%操作符和format()函数的使用示例。
600

被折叠的 条评论
为什么被折叠?



