基本运算

基本运算

一、算术运算符

 x = 10
 y = 20
 print(x + y)  #30
 print(x - y)  #-10
 print(x * y)  #200
 print(x / y)  #0.5
 print(x % y)  #10
 print(x // y) #0
 print(x ** y) #100000000000000000000

二、比较运算符

​ 返回的都是布尔值

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)

四、逻辑运算符

​ 返回True或False(二元运算符)

## and(和),两个条件都为真就为真,否则都为False
print(1>1 and 2>3)  # False
print(10>1 and False)  # False

## or(或),只要有一个为真就为真,否则都为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

五、身份运算符

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
posted @ 2023-10-20 16:02  Rubbish雀  阅读(19)  评论(0)    收藏  举报