Python 运算符、判断结构
算术运算符
算术运算符是完成基本的算术运算 (arithmetic operators) 符号,就是用来处理四则运算的符号。以下假设变量a为10,变量b为21:
a=10
b=3
print(a+b)
print(a-b)
print(a*b)
print(a/b)
print(round(a/b,3))
print(a//b)
print(a**b)
print(a%b)
13
7
30
3.3333333333333335
3.333
3
1000
1
比较(关系)运算符
a=10
b=3
print(a==b)
print(a!=b)
print(a>b)
print(a<b)
print(a>=b)
print(a<=b)
False
True
True
False
True
False
赋值运算符
a+=b (等同于a=a+b)
a-=b (等同于a=a-b)
a*=b (等同于a=a*b)
a/=b (等同于a=a/b)
a%=b (等同于a=a%b)
逻辑运算符
位运算符
# 5 0101
# 10 1010
a=5
b=10
print(a & b)
print(a | b)
print(a ^ b)
print(~b)
print(b<<2)
print(b>>2)
0
15
15
-11
40
2
成员运算符
身份运算符
a=30
b=30
c=10
print(a is b)
print(a is c)
print(a is not b)
print(a is not c)
True
False
False
True
运算符优先级
if语句
if–else语句
if–elif–else语句
# 单循环,不提倡
result=int(input('请输入成绩:'))
if result>=80:
print('A')
if result>=60 and result<=79:
print('B')
if result<60:
print('C')
result=int(input('请输入成绩:'))
if result>=80:
print('A')
elif result>=60 and result<80:
print('B')
else:
print("C")
result=int(input('请输入成绩:'))
if result>=0 and result<=100:
if result>=80:
print('A')
elif result>=60 and result<80:
print('B')
else:
print('C')
else:
print('输入有误')