python中的运算符

本文详细介绍了Python中的基本运算符、数学运算函数如sqrt、ceil、floor等,以及字符串操作符和格式化方法,包括%操作符和format()函数的使用示例。

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 = 25
print("I am %d years old." % age)
输出:I am 25 years old.
浮点数%f插入浮点数pi = 3.14159
print("The value of pi is %f." % pi)
输出:The value of pi is 3.141590.
科学计数法%e插入科学计数法表示的浮点数num = 1.23e+6
print("The number is %e." % num)
输出:The number is 1.230000e+06.
十六进制%x插入十六进制表示的整数num = 255
print("The number in hexadecimal is %x." % num)
输出:The number in hexadecimal is ff.
百分比%%插入百分比符号percentage = 0.75
print("The percentage is %.2f%%." % (percentage * 100))
输出:The percentage is 75.00%.

以上是 % 操作符的一些常见用法。需要注意的是,% 操作符在Python 3.6及更高版本中已经过时,推荐使用更现代的字符串格式化方法,如使用 str.format() 方法或 f-strings。

字符串格式化 format() 方法

分类格式描述示例
位置参数{}默认按顺序插入参数name = "Alice"
age = 25
print("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 = 25
print("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.14159
print("The value of pi is {:.2f}.".format(pi))
输出:The value of pi is 3.14.
对齐和填充{:width}指定字段的宽度name = "Alice"
`print("
浮点数精度{:width.precisionf}指定浮点数的精度pi = 3.14159
print("The value of pi is {:.4f}.".format(pi))
输出:The value of pi is 3.1416.
二进制{:b}插入二进制表示的整数num = 10
print("The number in binary is {:b}.".format(num))
输出:The number in binary is 1010.
Python 包含三个核心逻辑运算符,分别为 `and`(逻辑与)、`or`(逻辑或)、`not`(逻辑非),它们可以在布尔值之间执行逻辑 AND、OR 和 NOT 运算 [^1][^2]。 ### 各运算符描述及示例 | 运算符 | 描述 | 示例 | 结果 | | --- | --- | --- | --- | | and | 逻辑与,若两个操作数都为 True,则返回 True;否则返回 False | `True and True` | `True` | | or | 逻辑或,若两个操作数至少有一个为 True,则返回 True;只有当两个操作数都为 False 时,才返回 False | `False or True` | `True` | | not | 逻辑非,对操作数的布尔值取反 | `not False` | `True` | [^2] ### 真值表与运算规则 #### `and` 运算符 | 左操作数 | 右操作数 | 结果 | | --- | --- | --- | | True | True | True | | True | False | False | | False | True | False | | False | False | False | #### `or` 运算符 | 左操作数 | 右操作数 | 结果 | | --- | --- | --- | | True | True | True | | True | False | True | | False | True | True | | False | False | False | #### `not` 运算符 | 操作数 | 结果 | | --- | --- | | True | False | | False | True | [^2] ### 短路求值特性 #### `and` 的短路行为 在 `and` 运算中,如果第一个操作数为 `False`,则不会计算第二个操作数,因为无论第二个操作数是什么,整个表达式的结果都是 `False`。 ```python def check(): print("执行检查") return True print(False and check()) # 输出 False,不执行check() ``` #### `or` 的短路行为 在 `or` 运算中,如果第一个操作数为 `True`,则不会计算第二个操作数,因为无论第二个操作数是什么,整个表达式的结果都是 `True`。 ```python def load_data(): print("加载数据") return [] print(True or load_data()) # 输出 True,不执行load_data() ``` #### 实际应用:避免除零错误 利用 `and` 运算符的短路特性,可以避免危险的计算,如除零错误。 ```python x = 0 if x != 0 and (10 / x > 2): # 安全判断 print("条件成立") else: print("跳过危险计算") ``` [^2] ### 运算符优先级 逻辑运算符Python 中有其特定的优先级,在复杂的逻辑表达式中,需要注意运算符的优先级以确保表达式按预期计算。不过具体优先级顺序在引用中未详细提及,一般可通过查阅 Python 官方文档获取准确信息。 ### 示例代码 ```python a = 10 b = 10 c = -10 if a > 0 and b > 0: print("The numbers are greater than 0") if a > 0 and b > 0 and c > 0: print("The numbers are greater than 0") else: print("Atleast one number is not greater than 0") ``` 这段代码展示了 `and` 运算符的使用,第一个 `if` 语句中 `a > 0` 和 `b > 0` 都为 `True`,所以会输出相应信息;第二个 `if` 语句中由于 `c > 0` 为 `False`,所以整个 `and` 表达式为 `False`,会执行 `else` 语句的内容 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值