pyhton 控制語句
程序在一般情況下是按順序執行的,編程語言提供了各種控制結構,允許復雜的執行路徑。循環語句允許我們執行一個語句或語句多次
if 語句
Python條件語句是通過一條或多條語句的執行結果(True或者False)來決定執行的代碼塊,執行語句可以是單個語句或語句塊。判斷條件可以是任何表達式,任何非零、或非空(null)的值均為true

if True: print("Hello World") print("Goog Bye") ------------------------ Hello World Goog Bye if False: print("Hello Python") print("Goog Bye") ------------------------ Goog Bye
if ... else 語句
if False: print("No executed") else: print("Executed") ------------------------ Executed
if..elif...else
age = int(input("Please your age>>:")) if 0 < age and age <= 20: print("teenager") elif 20 < age and age <= 40: print("Man") elif 40 < age and age <= 60: print("Old") else: print("Died") ------------------------ Please your age>>:30 Man
if 嵌套
age = int(input("Please your age>>:")) if age >= 0: if 0 < age and age <= 20: print("teenager") elif 20 < age and age <= 40: print("Man") elif 40 < age and age <= 60: print("Old") else: print("Died") else: print("Your age error") ------------------------- Please your age>>:48 Old
while 循環語句
Python 編程中 while 語句用於循環執行程序,即在某條件下,循環執行某段程序,以處理需要重復處理的相同任務。

Python中while語句的一般形式: while 判斷條件: 語句
sum = 0 count = 0 while count <= 100: sum += count count += 1 print(sum) ------------------------- 5050
while無限循環,可以使用 CTRL+C 來中斷循環 while True: print("無限循環")
for 語句
for循環可以遍歷任何序列的項目(一個列表或者一個字符串等)

for <variable> in <sequence>: <statements> else: <statements>
languages = ["C", "C++", "Perl", "Python"] for x in languages: print (x)
fruits = ['banana', 'apple', 'mango','tomato','pelar'] for fruit in range(len(fruits)): print('fruit: ',fruits[fruit]) ------------------------------------------------ fruit: banana fruit: apple fruit: mango fruit: tomato fruit: pelar
break 語句
break 語句可以跳出 for 和 while 的循環體。若遇到break而使得 for 或 while 循環中終止而且 else 塊將不執行
for megs in 'Hello Python': if megs == 'y': break print ('輸出的當前字母為 :',megs) -------------------------------- 輸出的當前字母為 : H 輸出的當前字母為 : e 輸出的當前字母為 : l 輸出的當前字母為 : l 輸出的當前字母為 : o 輸出的當前字母為 : 輸出的當前字母為 : P
counts = 0 while counts < 6: print("counts:" ,counts) if counts == 3: break counts += 1 --------------------------------- counts: 0 counts: 1 counts: 2 counts: 3
continue語句
continue語句被用來告訴Python跳過當前循環塊中的剩余語句,然后繼續進行下一輪循環
for megs in 'Hello Python': if megs == 'y': continue print ('輸出的當前字母為 :',megs) -------------------------------- 輸出的當前字母為 : H 輸出的當前字母為 : e 輸出的當前字母為 : l 輸出的當前字母為 : l 輸出的當前字母為 : o 輸出的當前字母為 : 輸出的當前字母為 : P 輸出的當前字母為 : t 輸出的當前字母為 : h 輸出的當前字母為 : o 輸出的當前字母為 : n
counts = 0 while counts < 6: counts += 1 if counts == 3: continue print("counts:", counts) -------------------------------- counts: 1 counts: 2 counts: 4 counts: 5 counts: 6
else子句
循環語句可以有 else 子句,它在窮盡列表(以for循環)或條件變為 false (以while循環)導致循環終止時被執行,但循環被break終止時不執行
for...else
for 循環中使用 break 語句,break 語句用於跳出當前循環體,且不執行else子句,否則執行else子句
sites = ["Baidu", "Google","UC","Taobao"] for site in sites: if site == "Baidu": print("李彥宏") break print("循環數據 " + site) else: print("沒有循環數據!") print("完成循環!") -------------------------------- 李彥宏 完成循環!
while...else語句
while … else 在條件語句為 false 時執行則else 的語句塊,若遇到break語句則不執行else子句
count = 0 while count < 10: print (count, " 小於 10") count = count + 1 else: print (count, " 大於或等於 10") ------------------------- 0 小於 10 1 小於 10 2 小於 10 3 小於 10 4 小於 10 5 小於 10 6 小於 10 7 小於 10 8 小於 10 9 小於 10 10 大於或等於 10
pass語句
Python pass是空語句,是為了保持程序結構的完整性。pass 不做任何事情,一般用做占位語句
for char in "Hello World": if char == 'W': pass print("執行pass語句") print("char: " ,char) ------------------------------ char: H char: e char: l char: l char: o char: 執行pass語句 char: W char: o char: r char: l char: d
簡單示例
#!/usr/bin/env python # -*- coding:utf-8 -*- ''' 用戶登錄、注銷、注冊 ''' import json user_dict = {} cmd_list = ['ls','move','cd','mkdir','rm','touch'] def login(): while True: user_name = input("please enter login user name >>: ") with open('data.json','r') as f: data = f.read() user_dict = json.loads(data) if user_name in user_dict: while True: user_pawd = input("please enter login user pawd >>: ") if user_pawd == user_dict[user_name]: while True: cmd = input("please enter command >>>: ") if cmd in cmd_list: print("Run command %s" %cmd) elif cmd == "quit": break else: print("please cmd error,please enter again!") break else: print("Enter login user pawd error,please enter again!") break else: print("Enter login user name error,please enter again!") def registered(): while True: with open('data.json','r') as f: data = f.read() user_dict = json.loads(data) user_name = input("please enter user name >>>: ") if user_name not in user_dict: user_pawd = input("please enter user pawd >>>: ") user_dict[user_name] = user_pawd data = json.dumps(user_dict) with open('data.json','w') as f: f.write(data) break else: print("Enter user name existed !")
def logout(): while True: user_name = input("please enter delete user name >>: ") with open('data.json','r') as f: data = f.read() user_dict = json.loads(data) if user_name in user_dict: del user_dict[user_name] data = json.dumps(user_dict) with open('data.json','w') as f: f.write(data) break else: print("user name not existed!")
if __name__ == '__main__': while True: choice = input(" login \n regist \n quit \n logout \n >>:") if choice == "login": login() elif choice == "regist": registered() elif choice == "logout": logout() elif choice == "quit": break else: print("choice error!")
