1 流程控制 if 语句
if 条件:
代码1
代码2
代码3
表示 ;如果条件成立,就会做某件事情。
例如;
cls = 'human'
gender = 'female'
age = 24
if cls == 'human' and gender == 'female' and age > 28 and age < 28:
print('开始表白')
print('end...')
if 条件:
代码1
代码2
代码3
...
else:
代码1
代码2
代码3
表示 ;如果条件成立,就会做某件事情,如果不成立就会做另外一件事情
例如;
cls = 'human'
gender = 'female'
age = 38
if cls == 'human' and gender == 'female' and age > 16 and age < 22:
print('开始表白')
else:
print('阿姨好')
if 条件1:
代码1
代码2
代码3
...
elif 条件2:
代码1
代码2
代码3
...
elif 条件3:
代码1
代码2
代码3
...
...
else:
代码1
代码2
代码3
...
表示 ;如果条件1成立,就会做某件事情,如果条件2成立,就会做某件事情,.。。。。。。。。。。。。如果不成立就会做另外一件事情
例如;
cls = 'human'
gender = 'female'
age = 28
if cls == 'human' and gender == 'female' and age > 16 and age < 22:
print('开始表白')
elif cls == 'human' and gender == 'female' and age > 22 and age < 30:
print('考虑下')
else:
print('阿姨好')
代码的缩进(在Python中规定了是4个空格,同一缩进的代码是同级别的关系,自上而下的运行)
if 可以与 if嵌套使用
if 条件:
代码1
代码2
代码3
if 条件:
代码1
代码2
代码3
例如;
cls = 'human'
gender = 'female'
age = 18
is_success = False
if cls == 'human' and gender == 'female' and age > 16 and age < 22:
print('开始表白')
if is_success:
print('那我们一起走吧...')
else:
print('我逗你玩呢')
else:
print('阿姨好')
2 流程控制 while 语句
while 条件;
code 1
code 2
code 3
例如;
user_db = 'nick'
pwd_db = '123'
while True:
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
else:
print('username or password error')
break的意思是终止掉当前层的循环,执行其他代码。
while 条件:
print('1')
print('2')
break
print('3')
例如;
user_db = 'jason'
pwd_db = '123'
while True:
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
break
else:
print('username or password error')
continue的意思是终止本次循环,直接进入下一次循环
while 条件:
print('1')
print('2')
continue
print('3')
列如;
n = 1
while n < 10:
if n == 6:
n += 1 # 如果注释这一行,则会进入死循环
continue
print(n)
n += 1
ps:continue不能加在最后一步执行的代码,因为代码加上去毫无意义
while循环嵌套
退出单层循环
列如;
user_db = 'jason'
pwd_db = '123'
while True:
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
while True:
cmd = input('请输入你需要的命令:')
if cmd == 'q':
break
print('%s功能执行'%cmd)
else:
print('username or password error')
print('退出了while循环')
退出双层循环
列如;
user_db = 'jason'
pwd_db = '123'
while True:
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
while True:
cmd = input('请输入你需要的命令:')
if cmd == 'q':
break
print('%s功能执行'%cmd)
break
else:
print('username or password error')
print('退出了while循环')
上面的退出双层循环还有一种方法
列如;
user_db = 'jason'
pwd_db = '123'
flag = True
while flag:
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
while flag:
cmd = input('请输入你需要的命令:')
if cmd == 'q':
flag = False
break
print('%s功能执行'%cmd)
else:
print('username or password error')
print('退出了while循环')
while+else(了解)
while+else:else会在while没有被break时才会执行else中的代码。(正常提出)
n = 1
while n < 3:
if n == 2:break # 不会走else
print(n)
n += 1
else:
print('else会在while没有被break时才会执行else中的代码')
注意,while 不要将它写成死循环
3 流程控制语句 for
格式;
for 变量名 in 包含值:
比如;
info = {'name': 'jason', 'age': 19}
for item in info:
print(item) # 拿到字典所有的key
print(info[item])
range;
在Python3中,它是用户需要一个值就会生成一个值
>>> range(0,10000)
range(0, 10000)
>>>
在Python2中,是将所有的值都生成为一个列表
xrange 与Python3 中的range是一样的需要一个值,就会生成一个值
>>> range(0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
for 的break 和continue 和while 的用法是一样的
for可以不依赖于索引取指,是一种通用的循环取指方式
for的循环次数是由被循环对象包含值的个数决定的,而while的循环次数是由条件决定的
for 嵌套
1.打印99乘法口诀表
for i in range(1,10): #i=3
for j in range(1,i+1):
print('%s*%s=%s ' %(i,j,i*j),end='') #i=2 j=2
print()
本文深入讲解Python中的流程控制语句,包括if、while、for等关键语法,通过实例演示条件判断、循环操作及循环控制语句的使用方法,适合Python初学者及需要巩固流程控制概念的开发者。

被折叠的 条评论
为什么被折叠?



