if 条件:
# 条件成立时执行的子代码块
代码1
代码2
代码3数组
示例:spa
sex='female' age=18 is_beautiful=True if sex == 'female' and age > 16 and age < 20 and is_beautiful: print('开始表白。。。') print('other code1...') print('other code2...') print('other code3...')
if 条件:
# 条件成立时执行的子代码块
代码1
代码2
代码3
else:
# 条件不成立时执行的子代码块
代码1
代码2
代码3code
示例:blog
sex='female' age=38 is_beautiful=True if sex == 'female' and age > 16 and age < 20 and is_beautiful: print('开始表白。。。') else: print('阿姨好。。。') print('other code1...') print('other code2...') print('other code3...')
if 条件1:
if 条件2:
代码1
代码2
代码3input
sex='female' age=18 is_beautiful=True is_successful=True height=1.70 if sex == 'female' and age > 16 and age < 20 and is_beautiful \ and height > 1.60 and height < 1.80: print('开始表白。。。') if is_successful: print('在一块儿。。。') else: print('什么爱情不爱情的,爱nmlgb的爱情,爱nmlg啊.') else: print('阿姨好。。。') print('other code1...') print('other code2...') print('other code3...')
if 条件1:
代码1
代码2
代码3
elif 条件2:
代码1
代码2
代码3
elif 条件3:
代码1
代码2
代码3for循环
……class
else:循环
代码1语法
代码2im
代码3
示例:
若是成绩 >= 90,那么:优秀
若是成绩 >= 80且 < 90, 那么:良好
若是成绩 >= 70且 < 80, 那么:普通
其余状况:不好
score = input('please input your score: ') # score='100' score = int(score) if score >= 90: print('优秀') elif score >= 80: print('良好') elif score >= 70: print('普通') else: print('不好')
while 条件:
代码1
代码2
代码3
while True: name=input('please input your name: ') pwd=input('please input your password: ') if name == 'egon' and pwd == '123': print('login successful') else: print('username or password error')
while 条件1:
while 条件2:
代码1
代码2
代码3
示范一:
while True: name=input('please input your name: ') pwd=input('please input your password: ') if name == 'egon' and pwd == '123': print('login successful') while True: print(""" 0 退出 1 取款 2 转帐 3 查询 """) choice=input('请输入您要执行的操做:') #choice='1' if choice == '0': break elif choice == '1': print('取款。。。') elif choice == '2': print('转帐。。。') elif choice == '3': print('查询') else: print('输入指令错误,请从新输入') break else: print('username or password error')
示范二:
tag=True while tag: name=input('please input your name: ') pwd=input('please input your password: ') if name == 'egon' and pwd == '123': print('login successful') while tag: print(""" 0 退出 1 取款 2 转帐 3 查询 """) choice=input('请输入您要执行的操做:') #choice='1' if choice == '0': tag=False elif choice == '1': print('取款。。。') elif choice == '2': print('转帐。。。') elif choice == '3': print('查询') else: print('输入指令错误,请从新输入') else: print('username or password error')
在条件改成False时不会当即结束掉循环,而是要等到下一次循环判断条件时才会生效
tag=True while tag: name=input('please input your name: ') pwd=input('please input your password: ') if name == 'egon' and pwd == '123': print('login successful') tag=False else: print('username or password error') print('===>')
break必定要放在循环体内,一旦循环体执行到break就会当即结束本层循环
while True: name=input('please input your name: ') pwd=input('please input your password: ') if name == 'egon' and pwd == '123': print('login successful') break else: print('username or password error') print('===>>>>>') print('===>>>>>')
while+continue:结束本次循环,直接进入下一次循环
示例一:
count=1 while count < 6: #count=6 if count == 4: count += 1 continue print(count) count+=1
示例二:
while True: name=input('please input your name: ') pwd=input('please input your password: ') if name == 'egon' and pwd == '123': print('login successful') break else: print('username or password error') # continue # 此处加continue无用
while + else:
while 条件:
代码1
代码2
代码3
else:
在循环结束后,而且在循环没有被break打断过的状况下,才会执行else的代码
tag=True while tag: print(1) print(2) print(3) # tag=False break else: print('else的代码')
l=['a','b','c','d','e']
#while 取数组代码:
i=0
while i < len(l):
print(l[i])
i+=1
#for 取数组代码:
for x in l: # x='b'
print(x)
#for 取字典代码:
dic={'name':'egon','age':18,'gender':'male'}
for x in dic:
print(x,dic[x])
for + break
nums=[11,22,33,44,55] for x in nums: if x == 44: break print(x)
for + continue
nums=[11,22,33,44,55] for x in nums: if x == 22 or x == 44: continue print(x)
for + else
names=['egon','kevin1111_dsb','alex_dsb','mac_dsb'] for name in names: if name == 'kevin_dsb': break print(name) else: print('======>')
for+ range()
# range的用法 >>> range(1,5) [1, 2, 3, 4] >>> for i in range(1,5): ... print(i) ... 1 2 3 4 >>> range(1,5,1) [1, 2, 3, 4] >>> range(1,5,2) # 1 3 [1, 3] for i in range(5): # 0 1 2 3 4 print(i)