语法之流程运算
1、if:
主要是判断失误的对错 真假 是否可行;
编程的主要目的就是让机器帮助人类工作? 首先,机器需要判断能力
语法结构:
python是通过缩进来决定代码的归属
pep8:
缩进一定是四个空格
tab键
if 条件:
代码块
....
....
if gender == 'female' and 24 > age > 18 and is_beautiful:
print("小姐姐,给个微信")
if 条件:
代码块1
。。。
else:
代码块2
。。
if gender == 'female' and 24 > age > 18 and is_beautiful:
print("小姐姐,给个微信")
else: # 条件不成立将要执行的代码块
print('打扰了')
if 条件1:
代码块1
。。。
elif 条件2:
代码块2
elif 条件2:
代码块2
elif 条件2:
代码块2
elif 条件2:
代码块2
else:
代码块n
if gender == 'female' and 24 > age > 18 and is_beautiful:
print("小姐姐,给个微信")
# 在这个流程控制语句中可以加n多个elif
elif gender == 'female' and 30 > age > 18 and is_beautiful:
print("认识一下")
elif 30 > age > 18 and is_beautiful:
print("认识一下")
else: # 条件不成立将要执行的代码块
print('打扰了')
if ... elif ... else:
同一个代码结构里面只会执行一个
执行if就不会执行elif和else,
执行elif就不会执行if和else,执行else就不会执行if和elif
gender = input(" gender :")
age = input("age : ")
age = int(age)
is_beautiful = 0
is_success = input(":")
if gender == "female" and 30 > age > 14 and is_beautiful:
print("Pls ask for wechat !")
if is_success ==0:
print("bye")
else:
print("together")
else:
print("拜拜")
# for循环正常执行结束,就会执行else对应的代码块
# 非正常结束,例如break打断,就不会执行
for i in range(10):
if i == 5:
break
print(i)
else:
print('执行成功')
补充:
可以当做False来使用的:
0
None
""
[]
{}
2、while:
语法结构:
while 条件:
条件成立将要循环的代码块
continue:跳过本次循环,执行下一次循环 *****
# continue:跳过本次循环,执行下一次循环 *****
# continue下面不管有多少行代码,都不会执行
# break:结束本层循环,单纯指代当前while *****
# 只能结束一层循环
# 死循环
count = 0
while True:
print(count)
count+=1
while+嵌套:
from_db_password = '123'
count = 0
tag = True
while tag:
username = input("please input your username>>:")
password = input("please input your password>>:")
if username == from_db_username and password == from_db_password:
print('登录成功')
while tag:
cmd = input(">>>:")
if cmd == 'exit':
tag = ''
else:
print(f"执行{cmd}指令")
else:
print("登录失败")
count += 1
if count == 3:
print('锁定账户')
tag = 0
模拟认证功能: 1、接收用户的输入 2、判断用户的输入解果 3、返回数据
local_username = "david"
local_password = "123"
count = 0
while count < 5:
username = input("pls input your username:")
password = input("pls input your password : ")
print(password)
if username == local_username and password == local_password:# if的用法
print("登录成功")
break
else:
print("登录失败")
count += 1
if count == 3:
print("用户锁死")
count = 0
while count < 10:
count += 1
#print("第{}次打印".format(count))
if count == 7:# if elif 的用法
continue
elif count == 8:
break
print(count)
count = 0
while count < 10:
count += 1
print("第{}次打印".format(count))
print(f"第{count}次打印")# f" .format 的用法
if count == 7:
continue
elif count == 8:
break
print(count)
len()
1.在字符串中是字符串的长度
2.在列表中 是每个元素的个数
3.在字典中是有多少对键对值
s1 = "hello"
print(len(s1))#"str"
s2 = ["apple","football" ,123,["beer","tiger"]]
print(len(s2))#"list"
s3 = {"name":"david","age":18,"telephone":18755073689}
print(len(s3ict"
l1 =["apple", "liming", 123, [1232, "king"]]
count = 0
while count < len(l1):
print(l1[count])
count += 1
count = 0
while count < 10:
count += 1
if count == 5:
continue
print(count)
else:
print('youwenti ')
while 的嵌套
from_db_username = "sean"
from_db_password = "123"
count = 0
Tag = True
while Tag:
username = input("pls input your username : ")
password = input("pls input your password : ")
if username == from_db_username and password == from_db_password:
print("登录成功")
while Tag:###while的嵌套
cmd = input(": ")
if cmd == "exit":
Tag = ""
else:
print(f"执行{cmd}的操作")
else:
print("登录失败")
count += 1
if count == 3:
print("锁定账户")
Tag = 0
for:
for:给我们提供了一种不依赖于索引的取值方式
语法结构:
### for 变量 in 容器类型:
容器对象中有几个值,他就循环几次
字典对象,直接访问无法访问值value
for + continue
for + break
s1 = [123, "apple", 3456, [123,4567], "monkey" ]
for i in s1:# for是从容器中有几个值 取几个值
print(i)
s2= {"a":123, "b":"deak", "c":[123, "sea"]}
for i in s2:# 字典中可以取里面的值
print(s2[i])
for 循环的嵌入
for i in range(1,10):
for j in range(1,i+1):
print(f"{i}*{j}={i*j} ",end="")
print()
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81