目录
python运算符有算术运算符、赋值运算符、比较运算符、逻辑运算符、位运算符、成员运算符以及身份运算符。
一、算术运算符
# 算术运算符 + - * / ** //
a = 10
b = 20
result = a + b
print(result)
result = a - b
print(result)
result = a * b
print(result)
# 除法,结果是浮点数
result = b / a
print(result)
# 求幂
result = a ** b
print(result)
# 除法取整
result = b // a
print(result)
# 求余
result = a % b
print(result)
注意:1.任意一个数余2,要么是0(偶数),要么是1(奇数),用于判定奇偶。
2.m % n有n种情况,其中m % n == 0,m是n的倍数
二、赋值运算符
在 Python 中,赋值运算符用于将值分配给变量。
# 赋值运算符
a = 10
a = a + 1
print(a)
a += 1
print(a)
a -= 1
print(a)
a *= 2
print(a)
a /= 1
print(a)
a //= 2
print(int(a))
a **= 2
print(a)
a = 5
a %= 2
print(a)
三、比较运算符
比较运算符有>、>=、<、<=、!=、==。
# 比较运算符,结果有True或False
print(10 > 5)
print(10 >= 5)
print(5 < 10)
print(5 <= 10)
print(5 != 3)
print(5 == 5)
print(5 == 1)
print(3 != 3)
print(10 == "10", 10 != "10")
# python 推荐使用连续比较
print(10 > 8 > 5)
四、逻辑运算符
1.and(并)(左侧为真,返回右侧;左侧为假,返回左侧),左右均为真,结果才为真。
# 逻辑运算符(and or 的返回值是左侧或右侧)
# 左右均为真,结果才为真
# and(并)(左侧为真,返回右侧;左侧为假,返回左侧)
print(100 and 0)
print(100 and 20)
print(0 and "")
print("" and 0)# 返回空值
2.or(或) (左侧为真,返回左侧;左侧为假,返回右侧),左右均为假,结果才为假。
# 左右均为假,结果才为假
# or(或) (左侧为真,返回左侧;左侧为假,返回右侧)
print(100 or 1)
print(100 or 0)
print(0 or 100)
print("" or 0)
print(0 or None)
print(0 or "该起床了")
3.not(非)
# not(非)
print(not 1)
print(not 1 or 0)
print(not False)
五、位运算符
在 Python 中,位运算符用于对整数的二进制位进行操作。这些运算符直接处理二进制数据,因此在处理底层系统编程、数据加密、图形处理等场景中非常有用。
# 与&
a = 10 # 0b01010
b = 20 # 0b10100
print(a & b)
# 或|
print(a | b)
# 异或^
print(a ^ b)
六、成员运算符
在 Python 中,成员运算符in用于判断一个值是否属于某个容器类型(如列表、元组、字符串、字典等)的成员。
# 成员运算符
print("he" in "hello hi",100 in [100, 200, 300],"name" in {"name":"nihao"})
七、身份运算符
1.基本用法
在 Python 中,身份运算符is用于比较两个对象的内存地址(即是否为同一个对象实例),而非比较对象的值。这与比较值的相等运算符(==)有本质区别。
# 身份运算符
e = [100,200,300]
f = [100,200,300]
g = e
print(e == f, e is f, e is not f)
print(e is g)
2.常见应用
2.1 检查变量是否为 None
value = None
if value is None:
print("值不存在")
if value is not None:
print("值存在")
2.2 检查布尔值
flag = True
if flag is True:
print("标志为真")
2.3 单例模式检查
class Singleton:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
八、运算符的实际运用
1.比较三个数的大小
first_value = int(input("输入第一个数:"))
second_value = int(input("输入第二个数:"))
third_value = int(input("输入第三个数:"))
if first_value > second_value:
if third_value > first_value:
print(third_value - first_value, third_value, first_value,second_value)
else:
if second_value > third_value:
print(first_value - third_value, "排序:", first_value, second_value, third_value)
else:
print(first_value - second_value, "排序:", first_value, third_value, second_value)
else:
if third_value > second_value:
print(third_value - first_value, "排序:", third_value, second_value, first_value)
else:
if third_value < first_value:
print(second_value - third_value, "排序:", second_value, first_value, third_value)
else:
print(second_value - first_value, "排序:", second_value, third_value, first_value)
2.判断是否是闰年
year = int(input("请输入年份:"))
# 判断该年份是否为闰年。闰年的条件是:能被4 整除但不能被100整除,或者能被400整除
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("该年份是为闰年")
else:
print("该年份不是为闰年")
3.判断是否是等边、等腰三角形
first_side = int(input("输入三角形的第一个边长:"))
second_side = int(input("输入三角形的第二个边长:"))
third_side = int(input("输入三角形的第三个边长:"))
# 判断能否组成三角形
if (first_side + second_side > third_side) and (first_side + third_side > second_side) and (second_side + third_side > first_side ):
print("这三个边长能组成三角形 ")
# 如果能组成三角形进一步判定是否是等边三角形,等腰三角形,普通三角形
if (first_side == second_side) or (first_side == third_side) or (second_side == third_side) or (first_side == second_side == third_side):
print("这是等腰三角形")
else:
print("这是普通三角形")
if first_side == second_side == third_side:
print("这是等边三角形")
else:
print("不能组成三角形")
总结
Python 运算符是操作数据的基本工具,按功能可分为七类。算术运算符(如 +、-、*、/)用于数值计算;赋值运算符(如 =、+=)用于变量赋值;比较运算符(如 ==、>)返回布尔值比较结果;逻辑运算符(and、or、not)用于逻辑判断;位运算符(&、|、^)直接操作二进制位;成员运算符(in、not in)检查元素是否属于容器;身份运算符(is、is not)比较对象内存地址。需注意:比较值用 ==,比较身份用 is;位运算高效但可读性低;赋值操作可能因对象可变 / 不可变产生不同效果。合理运用运算符是编写高效 Python 代码的基础。
2632

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



