# Python 运算符示例代码
# 1. 算术运算符
a = 10
b = 3
print("a + b =", a + b) # 加法
print("a - b =", a - b) # 减法
print("a * b =", a * b) # 乘法
print("a / b =", a / b) # 除法
print("a // b =", a // b) # 整除
print("a % b =", a % b) # 取余
print("a ** b =", a ** b) # 幂运算
# 2. 比较运算符
print("a == b:", a == b) # 等于
print("a != b:", a != b) # 不等于
print("a > b:", a > b) # 大于
print("a < b:", a < b) # 小于
print("a >= b:", a >= b) # 大于等于
print("a <= b:", a <= b) # 小于等于
# 3. 赋值运算符
c = 5
c += 3 # 相当于 c = c + 3
print("c after c += 3:", c)
d = 8
d -= 2 # 相当于 d = d - 2
print("d after d -= 2:", d)
e = 4
e *= 2 # 相当于 e = e * 2
print("e after e *= 2:", e)
f = 9
f /= 3 # 相当于 f = f / 3
print("f after f /= 3:", f)
g = 7
g %= 2 # 相当于 g = g % 2
print("g after g %= 2:", g)
h = 6
h //= 2 # 相当于 h = h // 2
print("h after h //= 2:", h)
i = 2
i **= 3 # 相当于 i = i ** 3
print("i after i **= 3:", i)
# 4. 逻辑运算符
x = True
y = False
print("x and y is", x and y) # 逻辑与
print("x or y is", x or y) # 逻辑或
print("not x is", not x) # 逻辑非
# 5. 成员运算符
list1 = [1, 2, 3, 4, 5]
print("2 in list1:", 2 in list1) # 元素是否存在于列表中
print("6 not in list1:", 6 not in list1) # 元素是否不存在于列表中
# 6. 身份运算符
str1 = "hello"
str2 = "hello"
str3 = "world"
print("str1 is str2:", str1 is str2) # 判断两个对象是否是同一个对象
print("str1 is not str3:", str1 is not str3) # 判断两个对象是否不是同一个对象
# 7. 位运算符
p = 60 # 二进制: 0011 1100
q = 13 # 二进制: 0000 1101
print("p & q =", p & q) # 按位与
print("p | q =", p | q) # 按位或
print("p ^ q =", p ^ q) # 按位异或
print("~p =", ~p) # 按位取反
print("p << 2 =", p << 2) # 左移
print("p >> 2 =", p >> 2) # 右移
True 或 False)。+=, -=, *=, /=, etc.)。and, or, not。上一篇:python open
下一篇:python re
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站