Python程序語言指定任何非0和非空(null)值爲true,0 或者 null爲false。python
Python 編程中 if 語句用於控制程序的執行,基本形式爲:express
if 條件判斷: 執行語句 else: 執行語句
其中"判斷條件"成立時(非零),則執行後面的語句,而執行內容能夠多行,以縮進來區分表示同一範圍。編程
因爲 python 並不支持 switch 語句,因此多個條件判斷,只能用 elif 來實現,若是判斷須要多個條件需同時判斷時,可使用 or (或),表示兩個條件有一個成立時判斷條件成功;使用 and (與)時,表示只有兩個條件同時成立的狀況下,判斷條件才成功;else 爲可選語句,當須要在條件不成立時執行內容則能夠執行相關語句,當判斷條件爲多個值時,可使用如下形式。app
if 條件語句: 執行語句 elif 條件語句: 執行語句 elif 條件語句: 執行語句 ... else: 執行語句
當if有多個條件時可以使用括號來區分判斷的前後順序,括號中的判斷優先執行,此外 and 和 or 的優先級低於>(大於)、<(小於)等判斷符號,即大於和小於在沒有括號的狀況下會比與或要優先判斷。less
#!/user/bin/python # -*- coding: cp936 -*- h=1.75 w=80.5 a=80.5/(1.75*1.75) if a<18.5: print"太輕" elif 18.5<a and a<25: print "正常" elif 25<a and a<28: print "太重" elif 28<a and a<32: print "肥胖" else: print "嚴重肥胖" #輸出結果 >>> 太重
v=100 if (v==10):print "變量v的值爲:",v print "cood bye"
python 複合布爾表達式計算採用短路規則,即若是經過前面的部分已經計算出整個表達式的值,則後面的部分再也不計算。dom
a,b=0,1 if (a>0) and (b/a>2): print "youo got it" else: print "game over" a,b=0,1 if (a>0) or (b/a>2): print "you got it" else: print "game over" #一個簡單的條件循環語句實現漢諾塔問題 def my_print(args): print args def move(n,a,b,c): my_print((a,'--->',c)) if n== 1 else (move(n-1,a,c,b) or move(1,a,b,c) or move(n-1,b,a,c)) move (3,'a','b','c')
Python 編程中 while 語句用於循環執行程序,即在某條件下,循環執行某段程序,以處理須要重複處理的相同任務。其基本形式爲:oop
while 判斷條件: 執行語句……
執行語句能夠是單個語句或語句塊。判斷條件能夠是任何表達式,任何非零、或非空(null)的值均爲true;當判斷條件假false時,循環結束。ui
執行流程圖以下:spa
Python while 語句執行過程code
#!/user/bin/python # -*- coding: cp936 -*- list=[12,37,5,42,8,3] n1=[] n2=[] n=0 while n<len(list): if (list[n]%2==0): n1.append(list[n]) else: n2.append(list[n]) n=n+1 print "n1列表內容:%s;n2列表內容:%s"%(n1,n2) #輸出結果 >>> n1列表內容:[12, 42, 8];n2列表內容:[37, 5, 3]
break、continue的用法
break的做用是當符合必定的條件時,終止循環;continue的做用是,當知足必定條件,知足條件的這一項中止運行。
#打印偶數 i=1 while i<10: i+=1 if i%2>0: continue print i #打印出從1到10 i=1 while 1: print i i+=1 if i>10: break #輸出結果 >>> 2 4 6 8 10 1 2 3 4 5 6 7 8 9 10
循環使用 else 語句
count=0 while count<5: print count,"is less than 5" count=count+1 else: print count,"is not less than 5" #輸出的結果 0 is less than 5 1 is less than 5 2 is less than 5 3 is less than 5 4 is less than 5 5 is not less than 5 >>>
簡單語句組
i=input("please input number:") while i<0:print "game over" print i #輸出結果 >>> please input number:12 12
猜大小的遊戲
#!/usr/bin/python # -*- coding: UTF-8 -*- import random s = int(random.uniform(1,10)) #print(s) m = int(input('輸入整數:')) while m != s: if m > s: print('大了') m = int(input('輸入整數:')) if m < s: print('小了') m = int(input('輸入整數:')) if m == s: print('OK') break;
猜拳小遊戲
import random while 1: s=int(random.randint(1,3)) if s==1: ind='石頭' elif s==2: ind='剪子' elif s==3: ind='布' m=raw_input('輸入 石頭、剪子、布,輸入"end"結束遊戲:') blist=['石頭','剪子','布'] if (m not in blist) and (m=='end'): print "輸入錯誤,請重新輸入!" elif m==ind: print "電腦出了:"+ind+",平局!" elif (m=='石頭' and ind=='剪刀') or (m=='剪刀' and ind=='布') or (m=='布' and ind=='石頭'): print "你贏了" elif (m=='石頭' and ind=='布') or (m=='布' and ind=='剪刀') or (m=='剪刀' and ind=='石頭'): print '機器贏了' elif m=='end': break
十進制轉二進制
denum=input("輸入十進制數:") print denum,"(10)", binnum=[] #二進制數 while denum >0: binnum.append(str(denum%2)) denum //= 2 print '=' while len(binnum)>0: import sys sys.stdout.write(binnum.pop())
Python for循環能夠遍歷任何序列的項目,如一個列表或者一個字符串。
語法:
for循環的語法格式以下:
for iterating_var in sequence: statements(s)
經過序列索引迭代
fruits=['banana','apple','mango'] for index in range(len(fruits)): print '當前水果:',fruits[index] print "game over" for i,j in enumerate(fruits): print i,j
else 語句
在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區別,else 中的語句會在循環正常執行完(即 for 不是經過 break 跳出而中斷的)的狀況下執行,while … else 也是同樣。
for num in range(10,20): for i in range(2,num): if num%i==0: j=num print '%d 等於%d*%d'%(num,i,j) break else: print num,'是一個質數'
冒泡排序
arays=[1,8,2,6,3,9,4] for i in range(len(arays)): print len(arays) for j in range(i): if arays[i] <arays[j]: arays[i],arays[j]=arays[j],arays[i] print arays #輸出結果 >>> 7 7 7 7 7 7 7 [1, 2, 3, 4, 6, 8, 9]
Python for 循環嵌套語法:
for iterating_var in sequence: for iterating_var in sequence: statements(s) statements(s)
Python while 循環嵌套語法:
while expression: while expression: statement(s) statement(s)
100之內的質數:
num=[] i=2 for i in range(2,100): j=2 for j in range(2,i): if (i%j==0): break else: num.append(i) print num