基本运算符
算术运算符
+ - * / % // **
比较运算符
> >= < <= == != #返回一个布尔值
赋值运算符
= += -= *= /= //= %= **=
逻辑运算符
and左右两个条件都为True,则为True,否则为False
or左右两个条件只要有一个满足则为True,否则为False
not 如果条件为True,则非False。如果条件为False,则非True。
身份运算符
用于比较两个对象的存储单元
is和is not
x=257
y=x
z=257
print(x is y)#is比较的是内存地址
print(x is not y)#is not判断是否不相等
位运算符
把数字看作二进制进行计算的
& | ^ - << >>
成员运算符
判断元素是否在容器类元素里面(字符串)
in 和not in
class_student_lt = ['s1','s2','s3']
print('s1' in class_student_lt) # True
print('s1' not in class_student_lt) # False
print('s4' in class_student_lt) # False
python运算符优先级
需要优先,就加括号,括号优先级最高
流程控制之if判断
单分支结构
if 条件:
代码块
双分支结构
if 条件:
代码块1(条件成立)
else:
代码块2(条件不成立)
多分支结构
if 条件1:
代码块1#条件1成立
elif 条件2:
代码块2#条件1不成立条件2成立
elif 条件3:
代码块3#条件1 2不成立条件3成立
elif可以有无限个
else:
代码块4#所有条件不成立
流程控制之while循环
while 条件:
代码块
while+break
while True:
print(1)
break#终止掉当前层的循环,执行其他代码
print(2)
while+continue
while n<10:
if n==8:
continue#终止本次循环,直接进入下一次循环
print(n)
n+=1
tag控制循环
tag=True
while tag:
代码块
while+else
n=1
while n<3:
print(n)
n+=1
else:
print('else会在while,没有被break时才会执行else中的代码')