if
# score=99 # if score>90: # print('优秀') # elif score<60: # print('不及格') # else: # print('良好')
if嵌套
# sex='female' # age=18 # is_success=False # if sex=='female' and age>16 and age<22: # if is_success: # print('开始表白') # else: # print('逗你玩的') # else: # print('阿姨好')
while+break循环 ,break意思是直接终止本层循环
# user_db='egon' # password_db='123' # while True: # inp_user=input('user>>>:') # inp_password=input('password>>>:') # if user_db==inp_user and password_db==inp_password: # print('login is success') # break # else: # print('user or password error') # print('while循环执行完毕才会执行其他代码')
while+continue循环 continue意思是结束本次循环,直接进入下一次循环
# n=1 # while n<10: # if n==8: # continue #无线循环,程序不会被结束 # print(n) # n+=1 # n=1 # while n<10: # if n==8: # n+=1 #跳过8不会出现死循环 # continue # print(n) # n+=1 #不要把continue加在循环体的最后,加了没有意义,因为continue是结束本次循环,直接进入下一次循环,所以一定不要加在最后执行的代码后
while的嵌套循环
# user_db='egon' # password_db='123' # while True: # inp_user=input('user>>>:') # inp_password=input('password>>>:') # if user_db==inp_user and password_db==inp_password: # print('login is success') # while True: # cmd=input('请输入你的命令:') # if cmd=='q': # break # break # else: # print('user or password error') # print('while循环执行完毕才会执行其他代码')
while+tag 使用变量来控制while循环的条件,可以整体控制while循环嵌套
# user_db='egon' # password_db='123' # tag=True # while tag: # inp_user=input('user>>>:') # inp_password=input('password>>>:') # if user_db==inp_user and password_db==inp_password: # print('login is success') # while tag: # cmd=input('请输入你的命令:') # if cmd=='q': # tag=False # else: # print('执行命令') # else: # print('user or password error') # print('while循环执行完毕才会执行其他代码')
while+else
# n=1 # while n<5: # if n==3: # break # else: # print(n) # n+=1 # while+else # n=1 # while n<5: # # if n==3: # # break # print(n) # n+=1 # else: # print('在整个循环结束之后,会进行判断,只有while循环没有被break掉的情况下才会自行else,即while循环被break掉后,就不会执行else') # print('while循环被break掉,同级别代码任然会被执行') #取值while循环效果不好,条件循环,一般用while # names=['egon','asb','lsb','wsb','csb'] # n=0 # while n<len(names): # print(names[n]) # n+=1 #for循环受限于元素的个数,循环取值一般用for循环 # names=['egon','asb','lsb','wsb','csb'] # for name in names: # print(name) # info={'name':'egon','age':18,'sex':'male'} # for k in info: #for循环取出的是字典的key值,所有通过Key值就可以value的值 # print(k,info[k]) # for i in range(10): #range默认从零开始 # print(i) # for i in range(1,10): # print(i) # # # for i in range(1,10,2): # print(i) # names=['egon','asb','lsb','wsb','csb'] # for i in range(len(names)): # print(names[i])
for 循环的嵌套
# for i in range(5): # print('-------------第一层----------%s'%i) # for j in range(3): # print('第二层----------%s'%j)
for+break
# for i in range(10): # print(i) # if i==5: # break # print('break直接结束本层循环,所以执行不到该步骤') # print('每循环一次该打印会被执行一次')
for+continue #结束本次循环,直接进入下一次循环
# for i in range(10): # if i==5: # print('条件成立执行该步骤打印') # continue #if条件成立会跳出本次循环,本次循环continue以下的代码不会被执行到,所以不会打印出5这个数字 # print(i)
for+else
# for i in range(10): # print(i) # if i==5: # print('条件成立执行该步骤') # # break # else: # print('只有当整个for循环没有被break掉的时候,才会执行到else这一步,否则永远执行不到')