if 条件判断:
语法1
if 条件:#为True执行代码123
代码1
代码2
代码3
1 cls='human' 2 sex='female' 3 age=18 4 5 if cls == 'human' and sex == 'female' and age > 16 and age < 22: 6 print('开始表白') 7 8 print('end....')
语法2
if 条件:
代码1
代码2
代码3
else:
代码1
代码2
代码3
1 cls='human' 2 sex='female' 3 age=38 4 5 if cls == 'human' and sex == 'female' and age > 16 and age < 22: 6 print('开始表白') 7 else: 8 print('阿姨好') 9 10 print('end....')
语法3(程序逐个执行,即当if不成立执行下一个elif 依次类推)
if 条件1:
代码1
代码2
代码3
elif 条件2:
代码1
代码2
代码3
elif 条件3:
代码1
代码2
代码3
else:
代码1
代码2
代码3
1 如果:成绩>=90,那么:优秀 2 3 如果成绩>=80且<90,那么:良好 4 5 如果成绩>=70且<80,那么:普通 6 7 其他情况:很差 8 9 ''' 10 11 score=input('your score: ') score='73' 12 score=int(score) score=73 13 if score >= 90: 14 print('优秀') 15 elif score >= 80: 16 print('良好') 17 elif score >= 70: 18 print('普通') 19 else: 20 print('很差')
if的嵌套:
if条件1:
if条件2:
else:
else:
1 cls='human' 2 sex='female' 3 age=18 4 is_success=False 5 6 if cls == 'human' and sex == 'female' and age > 16 and age < 22: 7 print('开始表白...') 8 if is_success: 9 print('在一起') 10 else: 11 print('我逗你玩呢....') 12 else: 13 print('阿姨好') 14 15 print('end....')
while语法,while循环又称为条件循环
(1)while 条件:
code1
code2
code3
1 user_db='egon' 2 pwd_db='123' 3 4 while True: 5 inp_user=input('username>>: ') 6 inp_pwd=input('password>>: ') 7 if inp_user == user_db and inp_pwd == pwd_db: 8 print('login successfull') 9 else: 10 print('user or password error')
(2)while+break:break的意思是终止掉当前层的循环,.执行其他代码
while True: inp_user=input('username>>: ') inp_pwd=input('password>>: ') if inp_user == user_db and inp_pwd == pwd_db: print('login successfull') break else: print('user or password error') print('其他代码')
(3) while+continue:continue的意思是终止掉本次循环,.直接进入下一次循环
ps:记住continue一定不要加到循环体最后一步执行的代码
1 while True: 2 if 条件1: 3 code1 4 code2 5 code3 6 continue 无意义 7 elif 条件1: 8 code1 9 continue 有意义 10 code2 11 code3 12 elif 条件1: 13 code1 14 code2 15 code3 16 continue 无意义 17 else: 18 code1 19 code2 20 code3 21 continue 无意义
(4)while循环嵌套
(1)条件一直为True
1 user_db='egon' 2 pwd_db='123' 3 4 while True: 5 inp_user=input('username>>: ') 6 inp_pwd=input('password>>: ') 7 if inp_user == user_db and inp_pwd == pwd_db: 8 print('login successfull') 9 while True: 10 cmd=input('请输入你要执行的命令: ') 11 if cmd == 'q': 12 break 13 print('%s 功能执行...' %cmd) 14 break 15 else: 16 print('user or password error') 17 18 19 print('end....')
(2)条件可以通过tag'标签赋值
example
1 user_db='egon' 2 pwd_db='123' 3 tag=True 4 while tag: 5 inp_user=input('username>>: ') 6 inp_pwd=input('password>>: ') 7 if inp_user == user_db and inp_pwd == pwd_db: 8 print('login successfull') 9 while tag: 10 cmd=input('请输入你要执行的命令: ') 11 if cmd == 'q': 12 tag=False 13 else: 14 print('%s 功能执行...' %cmd) 15 16 else: 17 print('user or password error') 18 19 20 print('end....')
for 循环
逐个选取字符串、列表中的索引,以及字典中的key 进行程序循环调用
1 for i in range(10): 默认的起始位置是0 2 print(i) 3 4 for i in range(1,10,2): 1 3 5 7 9 5 print(i)
(1)for循环嵌套
1 #1、for循环嵌套 2 for i in range(5): 3 print('========>第一层: %s<=========' %i) 4 for j in range(3): 5 print(' 第二层: %s' %j) 6 #2 for与break,与while与break组合使用原理一样 7 for+break 8 names=['asb','wsb','egon','lsb','csb'] 9 for n in names: 10 if n == 'egon': 11 break 12 print(n) 13 14 #3:continue 也是跳出本次循环 15 for+continue 16 names=['asb','wsb','egon','lsb','csb'] 17 for n in names: 18 if n == 'egon': 19 continue 20 print(n) 21 22 #4:当for执行结束后在else ,在没有break的前提下 23 for+else 24 names=['asb','wsb','egon','lsb','csb'] 25 for n in names: 26 if n == 'egon': 27 break 28 print(n) 29 else: 30 print('=====>')