1.条件判断语句
if;condition
#一、单一情况 #1.if后面填的不是True就是False is_drink = True cond = is_drik is True print(cond) if cond: print("喝酒不开车") #2.and;两端条件成立则整体成立 account = "zhangsan" psw = "123" cond1 = account == "zhangsan" cond2 = psw =="123" cond3 = cond1 and cond2 print(cond1,cond2,cond3) if conds: print("登录成功") #3.or;两端有一个则整体成立 account ="zhangsan is_super_user = true cond1 = account == "zhangsan" cond2 = is_supeer_user is True cond3 = cond1 or cond2 print(cond1,cond2,cond3) if cond3: print("没问题") #4.not;不是/否 条件不成立,则整体成立 is_super_user = True cond1 = is_super_user is True cond2 = not cond1 print(cond1,cond2) if cond2: print("权限不足") #二、二选一 ticket > 4 if ticket >3: print("购买成功") else: print("购买失败") #多选一 alcohol =25 if 0<= alcohol <20: print("可以开车") elif 20<= alcohol <80: print("饮酒驾驶") else: print("醉酒驾驶") ticket = 4 if ticket>3 print("购买成功") else: print("购买失败") #三、多选一 alcohol = 25 if 0<= alcohol <20:
2.判断变量是否为空
null_str = "" if not null_str: print("空字符串") null_dict ={} #空字典 null_list =[] #空列表 null_tuple=() #空元组
3.三目运算符特点
#1.如果逻辑中只有一句话,则可以考虑,三目运算
#2.逻辑超过一句话,会很麻烦
除/ 整除// 余数%
4.range 的三个用法
(start=0,end,step=!)
5.for和while应用场景
1.已知重复次数 用for 已知总次数
2.未知重复次数 用while未知总页数
6.for,while循环基本格式(in可迭代对象)(while do)
for a in 'abcd'
print(a)
total = 0
num=1
while num <=11:
total += num
pringt(total)
可迭代对象=字符串+列表+字典+元组+文件句柄
7.随机数
from dandom import randint
sys_num = randint(50,51)
print(sys_num)