標籤:支援 ima 寫法 lock password 迴圈結構 round 這一 .com
1. if語句1.1 說明
if語句主要用來根據周圍環境條件(即expession)的變化做出不同的反應(即執行代碼)
1.2 文法1.2.1 單分支結構
if單分支單條件:
if expression: expr_true_suite注釋:expession為真執行代碼expr_true_suite
樣本:
name = ‘test‘input_name = input("input_name:")if name == input_name : print("right!")View Code
if單分支多條件:
if expression1 and expression2 :#條件兩邊可用and 或or expr_true_suite
注釋:expession為真執行代碼expr_true_suite
樣本:
name = ‘test‘age = 22input_name = input("input_name:")input_age = int(input("input_age:") )if name == input_name and age == input_age : print("name and is right!")View Code
if+else:
if expression: expr_true_suite else: expr_false_suite
樣本:
name = ‘test‘input_name = input("input_name:")if name == input_name: print("name is right!")else: print("name is error")View Code
1.2.2 多分支結構
if多分支結構:
if expession1: expr1_true_suiteelif expression2: expr2_true_suiteelif expession3: expr3_true_suiteelse: none_of_the_above_suite
樣本:
name = ‘test‘age = 22input_name = input("input_name:")input_age = int(input("input_age:") )if name !=input_name and age !=input_age : print("name and age all error!")elif name == input_name and age !=input_age : print("name right and age error")elif name != input_name and age ==input_age : print("age right and name error")else: print("all right")View Code
使用者登入:
#import getpass #密碼加密name = input("please input names:")#pwd = getpass.getpass("please input password:")pwd = input("please input password")if name == "chunwei" and pwd == "hello": print("welcome to here!")else: print("name or pwd error")
猜年齡:
self_age = 22age = int(input("age:"))if age == self_age: print ("you are right")elif self_age > age : print("try bigger")else: print("try small")
1.3 if語句小結
1)if 後運算式傳回值為True則執行其子代碼塊,然後此if語句到此終結,否則進入下一分支判斷,直到滿足其中一個分支,執行後終結if2)expression可以引入運算子:not,and,or,is,is not3)多重expression為加強可讀性最好用括弧包含4)if與else縮排層級一致表示是一對5)elif與else都是可選的6)一個if判斷最多隻有一個else但是可以有多個elif7)else代表if判斷的終結8)expession可以是傳回值為布爾值的運算式(例x>1,x is not None)的形式,也可是單個標準對象(例 x=1;if x:print(‘ok‘))9)所有標準對象均可用於布爾測試,同類型的對象之間可以比較大小。每個對象天生具有布 爾 True 或 False 值。Null 物件、值為零的任何數字或者 Null 對象 None 的布爾值都是 False。
2. while迴圈2.1 說明
while迴圈的本質就是讓電腦在滿足某一條件的前提下去重複做同一件事情(即while迴圈為條件迴圈,包含:1.條件計數迴圈,2條件無限迴圈)
這一條件指:條件運算式
同一件事指:while迴圈體包含的代碼塊
重複的事情例如:從1加到10000,求1-10000內所有奇數,服務等待串連
2.2 文法
while expression: suite_to_repeat註解:重複執行suite_to_repeat,直到expression不再為真
2.2.1 計數迴圈
count = 0while (count <5): print ("the loop is ",count) #print ("the loop is %s" %count) ##兩種寫法效果一樣## count+=1
2.2.2 無限迴圈(死迴圈)
count = 0while True: print ("the loop is" ,count) count+=1
2.2.3 while與break,continue,else連用
break跳出本層迴圈:
count=0while (count < 5): count+=1 if count == 3: print("跳出本層迴圈,即徹底終結這一個層while迴圈") break print(‘the loop is ‘ ,count)
continue跳出本次迴圈:
count=0while (count < 5): count+=1 if count == 3: #print(‘跳出本層迴圈,即徹底終結這一個/層while迴圈‘) #break print("跳出本次迴圈,即這一次迴圈continue之後的代碼不再執行,進入下一次迴圈") continue print(‘the loop is ‘ ,count)
else連用
count=0while (count < 6): count+=1 if count == 3: print(‘跳出本次迴圈,即這一次迴圈continue之後的代碼不再執行,進入下一次迴圈‘) continue print(‘the loop is %s‘ %count)else: print(‘迴圈不被break打斷,即正常結束,就會執行else後代碼塊‘)
猜年齡最佳化:
輸入三次不對則退出
count = 0self_age = 22while count <3: guess_age = int(input("guess_age:")) # if guess_age.isdigit(): # guess_age == int(guess_age) # else: # continue if guess_age == self_age: print("you are right!!!!") break elif guess_age < self_age: print("try bigger!!!") else: print("try small!!!!") count +=1else: print("try too many is error ! byebye....")
2.3 while小結
1)條件為真就重複執行代碼,直到條件不再為真,而if是條件為真,只執行一次代碼就結束了2)while有計數迴圈和無限迴圈兩種,無限迴圈可以用於某一服務的主程式一直處於等待被串連的狀態3)break代表跳出本層迴圈,continue代表跳出本次迴圈4)while迴圈在沒有被break打斷的情況下結束,會執行else後代碼
3. for迴圈
3.1 說明
for 迴圈提供了python中最強大的迴圈結構(for迴圈是一種迭代迴圈機制,而while迴圈是條件迴圈,迭代即重複相同的邏輯操作,每次操作都是基於上一次的結果,而進行的)
3.2 文法
3.2.1 基本文法
for iter_var in iterable: suite_to_repeat註解:每次迴圈, iter_var 迭代變數被設定為可迭代對象(序列, 迭代器, 或者是其他支援迭代的對 象)的當前元素, 提供給 suite_to_repeat 語句塊使用.
執行個體1:
for i in range(10): print("loop is :" ,i )
執行個體2:
for i in range(5): print("------",i ) for j in range(10): print(j ) if j > 3: break
Python基礎2