# 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) # 幂运算,a 的 b 次方
# 2. 比较运算符
x = 5
y = 8
print("x > y is", x > y) # 大于
print("x < y is", x < y) # 小于
print("x == y is", x == y) # 等于
print("x != y is", x != y) # 不等于
print("x >= y is", x >= y) # 大于等于
print("x <= y is", x <= y) # 小于等于
# 3. 赋值运算符
c = 5
c += 3 # 相当于 c = c + 3
print("c =", c)
d = 10
d -= 2 # 相当于 d = d - 2
print("d =", d)
e = 2
e *= 4 # 相当于 e = e * 4
print("e =", e)
f = 20
f /= 4 # 相当于 f = f / 4
print("f =", f)
g = 7
g %= 3 # 相当于 g = g % 3
print("g =", g)
h = 2
h **= 3 # 相当于 h = h ** 3
print("h =", h)
# 4. 逻辑运算符
p = True
q = False
print("p and q is", p and q) # 逻辑与
print("p or q is", p or q) # 逻辑或
print("not p is", not p) # 逻辑非
# 5. 成员运算符
list1 = [1, 2, 3, 4, 5]
print("2 in list1 is", 2 in list1) # 检查元素是否在列表中
print("6 not in list1 is", 6 not in list1) # 检查元素是否不在列表中
# 6. 身份运算符
str1 = "hello"
str2 = "hello"
str3 = "world"
print("str1 is str2 is", str1 is str2) # 检查两个对象是否是同一个对象
print("str1 is not str3 is", str1 is not str3) # 检查两个对象是否不是同一个对象
# 7. 位运算符
i = 6 # 二进制: 0110
j = 2 # 二进制: 0010
print("i & j =", i & j) # 按位与
print("i | j =", i | j) # 按位或
print("i ^ j =", i ^ j) # 按位异或
print("~i =", ~i) # 按位取反
print("i << 2 =", i << 2) # 左移
print("i >> 2 =", i >> 2) # 右移
True 或 False)。+=, -=, *=, /= 等)。and、or 和 not。上一篇:python读取二进制文件
下一篇:python 文件读写
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站