10 基本运算符
基本运算符
运算符:运算符好
- 算术运算符
x = 10
y = 20
print(x + y) 加 # 结果为:30
print(x - y) 减 # 结果为:
print(x * y) 乘 # 结果为:
print(x / y) 除 # 结果为:
print(x % y) 求余 # 结果为:
print(x // y) 求整 # 结果为:
print(x ** y) 幂 # 结果为:
- 比较运算符
x = 10
y = 20
print(x > y) # False
print(x < y) # True
print(x >= y) # False
print(x <= y) # True
print(x == y) # False
print(x != y) # True
- 赋值运算符
x = 10 # (一元赋值运算符)
y = 20
# x = x+y # x += y
x += y # (二元赋值运算符)
x += 10
print(x)
x -= y
print(x)
x *= y
print(x)
x /= y
print(x)
x **= y # x = x**y
print(x)
y //= x # x = x//y
print(y)
x %= y # x = x%y
print(x)
- 逻辑运算符
- and(和), 两个条件都为真就为True(真),否则都为False
print(1>1 and 2>3) # False
print(10>1 and False) # False
- or (或),只要有一个为真就为True(真),否则都为False
age = 18
# inp_age= input('age:')
# print(age==inp_age or True) # True
print(True or False) # True
print(False or False) # False
- not (不是)
print(not True) # False
print(not False) # True
- 身份运算符, 通过比较两者的id返回布尔值
x=1000
y=1000
# print(id(x))
# print(id(y))
print(x is y) # False
print(x is not y) # True
print(not x is y)
# 值相同的id不一定相同,id相同的值一定相同
补充(仅做了解)
- 运算符优先级(如果你想让他有先算,加括号就行了,没必要记优先运算符)
x = 100
y = 100
print(not x is y) # x is y = True # not True = False
print(not x is y ** 2) # y ** 2 = 10000 # x is y = False # not False = True
print(not ((x is y) ** 2)) # x is y = True # True ** 2 = 1 = True # not True = False
# True为1,False为0
print(True>0) # True
print(False >0) # False
x = 10
y = 10
print(x is y) # True
x = x ** 5 # x = 100000
print(x is y) # False
print(x is y ** 5) # y=100000 # False
x = 100000
y = 100000
print(x is y) # True
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY