(一)條件語句
Python條件語句是經過一條或多條語句的執行結果(True或者False)來決定執行的代碼塊。
if 語句用於控制程序的執行,基本形式爲:python
if 判斷條件: 執行語句…… elif 判斷條件: 執行語句…… else: 執行語句……
注意:(1)elif語句能夠有0個或多個
(1)因爲 python 並不支持 switch 語句,因此多個條件判斷,只能用 elif 來實現,若是判斷須要多個條件需同時判斷時,能夠使用 or (或),表示兩個條件有一個成立時判斷條件成功;使用 and (與)時,表示只有兩個條件同時成立的狀況下,判斷條件才成功。
練習1:if語句練習-多個elif-判斷成績等級less
#encoding=utf-8 def print_grade_level(grades): if 90<=grades<=100: print u'優秀' elif 80<=grades<90: print u'良好' elif 60<=grades<80: print u'及格' else: print u'不及格' try: grades=float(raw_input('please input your grade:')) print_grade_level(grades) except Exception,e: print u"請輸入正確的分數!"
練習2:猜數dom
>>> import random >>> target_number=random.randint(1,5) >>> guess_number=int(raw_input("input your guess number:"),) input your guess number:4 >>> if guess_number ==target_number: ... print "you got it" ... elif guess_number>target_number: ... print "bigger" ... else: ... print "less" you got it
(二)循環語句
1while循環
在給定的判斷條件爲 true 時執行循環體,不然退出循環體。
語句格式爲:函數
while 判斷條件: 執行語句…… else: 執行語句……
注意:
執行語句能夠是單個語句或語句塊;
判斷條件能夠是任何表達式,任何非零、或非空(null)的值均爲True;
當判斷條件爲假即False時(參考bool中false的狀況),循環結束。
練習1:打印出count計算oop
>>> count=0 >>> while(count<9): ... print "the index is:",count ... count+=1
練習2:列表倒序打印code
>>> number=-1 >>> while number>=-len(lista): ... print lista[number] ... number-=1
練習3:猜數,循環猜數直到猜中爲止遞歸
#coding=utf-8 import random target_number=random.randint(1,5) print target_number guess_times=0 while 1: guess_number=int(raw_input("please input your guess number:")) guess_times+=1 if guess_number>target_number: print "bigger" elif guess_number<target_number: print "less" else: print "you got it" break print "guess times :%s" %guess_times
for循環
Python for循環能夠遍歷任何序列的項目,如一個列表或者一個字符串。
for iterating_var in sequence:
statements(s)
此處的sequence爲序列,能夠爲元組、列表、字符串、字典。
for … else結構下
1.for裏面的語句和普通的(沒有else的for語句)沒有區別;
2.else中的語句會在循環正常執行完後執行;
3.當for中語句經過break跳出而中斷時,不會執行else。索引
練習1:用於序列類型utf-8
for eachLetter in "names": ... print "current letter:",eachLetter
練習2:經過序列項迭代字符串
>>> nameList=['walter','nicole','steven','henry'] >>> for eachName in nameList: ... print eachName,"lim"
練習3:經過索引迭代
>>> nameList=['walter','nicole','steven','henry'] >>> for nameIdex in range(len(nameList)): ... print "liu",nameList[nameIdex]
練習4:
>>> nameList=["Donn","Shirley","Ben","Janice","David","Yen","Wendy"] >>> for i,eachLee in enumerate(nameList): ... print "%d %s Lee"%(i+1,eachLee)
練習5:判斷質數
>>>import math >>>ss=int(raw_input("enter:")) >>>for i in range(2,int(math.sqrt(ss))+1): >>> if ss%i==0: >>> print "%d is not prime"%ss >>> break >>>else: >>> print "%d is prime"%ss
練習6:遞規的用法
>>>def fact(n): >>> if n<=1: >>> return 1 >>> else: >>> return n*fact(n-1)
注:當遞歸函數有return時,在遞歸的地方也要return,否則永遠返回是None
練習6:嵌套輸出10-50個數是1-5的數
方法一:
>>> for i in range(1,5): ... for j in range(1,6): ... print str(i)+str(j)
方法二:
>> for i in range(10,50): .. if str(i)[1] in ["1","2","3","4","5"]: .. print i
方法三:
lis=[1,2,3,4,5] for i in range(10,51): j=i%10 if j in lis: print i
(三)break/continue/pass語句
(1)breakcontinue
break:跳出最近所在的循環(跳過整個循環語句)
continue:跳到最近所在循環的開頭處(來到循環的首行)
pass:什麼事也不作,只是空佔位語句
循環else塊:只有當循環正常離開時纔會執行(也就是沒有碰到break語句)
(3)直接退出多重循環三種方式
方法一:
class getoutofloop(Exception): pass try: for i in range(5): for j in range(5): for k in range(5): if i == j == k == 3: raise getoutofloop() else: print i, '----', j, '----', k except getoutofloop: Pass
方法二:
def test(): for i in range(5): for j in range(5): for k in range(5): if i == j == k == 3: return else: print i, '----', j, '----', k test()
方法三:
for i in range(5): for j in range(5): for k in range(5): if i == j == k == 3: break else: print i, '----', j, '----', k else: continue break else: continue Break