python的基本流程控制

一:if判断语句
1.1 if判断语法之一
if条件:
子代码块

1.2 if判断语法之二
if条件:
子代码块
else:
子代码块

1.3 if判断语法之三
if条件:
if条件:
子代码块

1.4 if判断语法之四
if条件:
子代码块
elif条件:
子代码块
,,,
else条件:
子代码块
举个简单例子 如:
sex = 'male'
age = 19
is_beauty = True
success = True
if is_beauty and age < 20 and age > 16 and sex =='male':
if success:
print('收获爱情')
else:
print('我爱你不后悔 也尊重故事结尾')
else:
print('你很好,只是我们不合适')

二while循环
2.1while条件:while True:
print('hello world')
2.2结束while循环的两种方式
1条件改为false
conut=1
while count<2:
print('hello world')
count+=1
2while+break
while True:
print('hello world')
break
2.3while+continue
count=1
while count<6:
if count==4:
count+=1
continue
else:
print(count)
count+=1
2.4while+else
while 条件:
代码1
代码2
代码3
else:
在循环结束后,并且在循环没有被break打断过的情况下,才会执行else的代码

2.5while+while
tag = True
while tag:
name = input('请输入你的名字:')
pwd = input('请输入密码:')

if name == 'egon' and pwd =='123':
print('login is successful')
while tag:
print("""
0是退出
1是存款
2是查询
3是取款
""")
choice = input('请选择您的操作')

if choice =='0':
print('退出')
tag = False
elif choice =='1':
print('存款')
elif choice =='2':
print('查询')
elif choice == '3':
print('取款')
else:
print("""
0是退出
1是存款
2是查询
3是取款
""")
else:
print('username or password is error')

三for循环
3.1循环取值
列表与字典
l=[1,2,3,4,5]
for x in l:
print(x)
d={'name':'egon','age':'18','sex':'male'}
for x in d:
print(d,dic[x])
3.2for+break
nums=[11,22,33,44,55]
for x in nums:
if x==44:
break
print(x)
3.3for+continue
nums=[11,22,33,44,55]
for x in nums:
if x==44:
continue
print(x)
3.4 for+else
3.4 for+range
for i in range(5): #range顾头不顾尾 range(5)=range(0,5,1)
print(i)

3.5 for嵌套
for i in range(3):
for j in range(4):
print(i,j)

承蒙关照
原文地址:https://www.cnblogs.com/guanlei/p/10573729.html