格式:while 條件:python
循環體linux
print(1) while True: print("癢") print("雞你太美") print("卡路里") print("好運來") print("小三") print("小白臉") print("趁早") print("過火") print(2) falg = True while falg: print(1) print(2) # 正序的示範 count = 1 while count <= 5: print(count) count = count + 1 # 倒序的示範 count = 5 while count: print(count) count = count - 1 # 正序打印 25 - 57 count = 25 while count < 58: print(count) count = count + 1 # 倒序打印 57 - 25 count = 57 while count > 24: print(count) count = count - 1
數字中非零都是True 零是Falsewindows
# print(bool(0)) # 數字中非零的都是True
break:終止循環,break下面的代碼不會執行學習
while True: print(123) print(234) break # 終止當前循環,break下方的代碼不會進行執行 print(345) print(1111)
continue:結束本次循環,直接跳入下一次循環(假裝成循環體中的最後一行),下面的代碼不會執行編碼
while True: print(123) print(234) print(345) while True: print(123) print(234) continue #continue 假裝成循環體中的最後一行代碼(跳出當前循環繼續下次循環) #continue 下面的代碼不會執行 print(345) print(1111)
while else :是一個整體(循環),當條件成立的時候不執行,當條件不成立的時候執行code
while True: print(111) break else: print(222) while True: print(111) break print(222) # while else # 0.退出 # 1.開始登陸 # 若是用戶選擇序號0就提示用戶退出成功 # 若是用戶選擇序號1就讓用戶輸入用戶名和密碼而後進行判斷,正確就終止循環,錯誤就從新輸入 srr = """ 0.退出 1.開始登陸 """ print(srr) num = int(input("請輸入你要的數字:")) while num == 1: name_input = input("請輸入你的用戶名:") password_input = input("請輸入你的密碼:") name = "alex" password = "123" if name_input == name and password_input: print("登陸成功") break else: print("用戶名或者密碼錯誤,請從新輸入!") else: print("退出成功")
總結:utf-8
break -- 打破當前循環 (終止當前循環)ci
a = "------------- info -------------" b = "name:" c = "age:" d = "job:" e = "-------------- end -------------" name = input("name") age = input("age") job = input("job") print(a + "\n" + b + name + "\n" + c + age + "\n"+ d + job + "\n" +e)
%就是佔位unicode
%%(轉義) 轉義成普通的%字符串
%s 是佔的字符串類型的位置
%d 是佔的數字類型的位置
按照位置順序傳遞,佔位和補位必需要一一對應
s = """ ------------- info ------------- name:%s age:%s job:%s -------------- end ------------- """ name = input("name") age = int(input("age")) job = input("job") print(s%(name,age,job)) # num = input("學習進度:") # s = "大哥黑的學習進度爲:%s%%" # print(s%(num)) # num = input("學習進度:") # num1 = input("aaaa:") # s = "大哥黑的學習進度爲:%s%" # print(s%(num,num1)) # num = input("學習進度:") # num1 = input("aaaa:") # s = "大哥黑的學習進度爲:%s%s" # print(s % (num, num1))
擴展:f"{變量名}{字符串}"3.6版本及以上才能使用
# s = f"今天下雨了{input('>>>')}" # print(s) # s = f"大黑哥的學習進度爲{input('<<<')}" # # print(s) # s = f"{1}{2}{3}" # print(s)
and (與):全真則真、
# print(3 and 4) # print(0 and 4) # print(0 and False) # print(3 and 5 and 9 and 0 and False) # print(5 and False and 9 and 0) # print(1 and 2 and 5 and 9 and 6)
or(或):一真則真
# print(1 or 0) # print(1 or 2) # print(0 or False) # print(1 or 9 or 4 or 0 or 9)
not(非):對立的
# print(not False)
級別:()> not > and > or
順序:從左向右執行
# print(9 and 1 or not False and 8 or 0 and 7 and False)
# s = "alexdsb" # if "sb" not in s: # print(True) # else: # print(False)
# 今 0101 # 天 0110 # 晚 0010 # 上 0001 # 去 1001 # 便 1000 # 利 0100 # 店 1111 # # # 00000101 00000110 0010000110011001