目录
基本运算符
算数运算符
+ - * / % // **
返回一个数值
比较运算符
> >= < <= == !=
返回一个布尔值
逻辑运算符
and or not
叠加多个条件
身份运算符
is
比较的是内存地址(id)是否相同
成员运算符
判断元素是否在容器类元素里面
in / not in
赋值运算符
= += -= *= /=
运算符优先级
需要优先就加括号
流程控制之if判断
单分支结构
age = 18
age_inp = int(input('pls enter your age: '))
if age_inp == age:
print('young blood')
双分支结构
age = 18
age_inp = int(input('pls enter your age: '))
if age_inp >= age:
print('adult')
else:
print('just grow up')
多分支结构
age_inp = int(input('pls enter your age: '))
if age_inp >= 18:
print('adult')
elif age_inp >= 13:
print('teenager')
else:
print('kid')
流程控制之while循环
while循环
while 条件: # 条件成立运行代码, 不成立结束whlie循环
代码 # 代码执行结束后会进入下一次循环(再一次判断条件)
while + break
break会终止循环
# 打印1到99
count = 0
while True:
if count == 100:
break
count = count + 1
print(count)
print('done')
while + continue
continue会跳过本次循环, 不执行下面的代码
# 打印1到99, 不打印50
count = 0
while True:
if count == 100:
break
count = count + 1
if count == 50:
continue
print(count)
print('done')