if語句寫法: python
if expression: statement(s)
注:python使用縮進做爲其語句分組的方法,建議使用4個空格。express
(1)條件爲真true (非空的量(string,tuple,list ,set,dictonary),全部非零的數):bash
if 1: print 'hello world!' print 'True'
if 'aaa': print 'hello world!' print 'True'
(2)條件爲假 faulse(0,None,空的量): 函數
if 0: print 'hello world!' print 'True'
if None: print 'hello world!' print 'True'
if '': print 'hello world!' print 'True'
if 1>2: print 'hello world!' print 'True'
(3)組合條件及其餘(and /or ): ui
if not 1>2: print 'hello world!' print 'True'
if not 1>2 and 1 == 1: print 'hello world!' print 'True'
2.if else 舉例:spa
if else寫法:code
else語句:input
if expression: statement(s) else: statement(s)
if 1 < 2: print 'hello world' else: print 'Oh,no,fourse!' print 'main'
3.if elif else寫法:string
elfi 語句: it
if expression1: statement1(s) elif expression2: statement2(s) else: statement3(s)
if 1 < 2: print 'hello world' elif 'a': print 'aaaaa' else: print 'Oh,no,fourse!'
4.舉例1:
#!/usr/bin/env python score =int( raw_input(‘Please input a num:’)) if score >= 90: print 'A' print 'Very good' elif score >=80: print 'B' print 'good' elif score >=60: print 'C' print 'pass' else: print 'D' print 'END'
5.舉例2:and or 應用:
多個條件下判斷:
轉換大小寫:
a.lower()
a.upper()
#!/usr/bin/env python yn = raw_input("Please input [Yes/No]:") yn = yn.lower() if yn == 'y' or yn == 'yes': print "Programe is running..." elif yn == 'n' or yn == 'no': print "Programe is exit." else: print "Error,Please input [Yes/No]"
循環
循環是一個結構,致使程序要重複必定的次數。
條件下循環也是如此,固然條件變爲假,循環結束。
for循環:
在序列裏,使用for循環遍歷。
語法:
for iterating_var in sqquence: statement(s)
舉例:
(例1)for用法舉例
>>> a = "ABC" >>> for i in a: ... print(i) ... A B C
(例2)list的for循環
>>> list1 = [1,3,4,5] >>> for i in list1: ... print(i) ... 1 3 4 5
(例3)range()函數用法:
最在取到5
>>> for i in range(1,6): ... print(i) ... 1 2 3 4 5
步長爲3
>>> for i in range(1,11,3): ... print(i) ... 1 4 7 10
求1,10內的偶數:
>>> print ([i for i in range(1,11) if i%2==0]) [2, 4, 6, 8, 10]
求1到100全部數加到一塊兒的和:
#!/usr/bin/python sum = 0 for i in range(1,101): sum = sum + i print sum
運行結果:
[root@localhost python]# python for1.py 5050
流程控制-for循環(字典)
>>> dic = {'a': '100', 'b': '100', 'c': '100', 'd': '100', 'e': '100', 'f': '100'} >>> for i in dic.items():print(i) ... ('a', '100') ('b', '100') ('c', '100') ('d', '100') ('e', '100') ('f', '100') >>>for k in dic: print(k) a b c d e f
舉例乘法口訣:
#!/usr/bin/env python #python3中執行 for i in range(1,10): for j in range(1,i+1): print("%sx%s=%s" % (j,i,j*i),end=" ") print()
運行結果:
1x1=1 1x2=2 2x2=4 1x3=3 2x3=6 3x3=9 1x4=4 2x4=8 3x4=12 4x4=16 1x5=5 2x5=10 3x5=15 4x5=20 5x5=25 1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36 1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49 1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64 1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
while與for相對比:
for循環用在有次數的循環上。
while循環用在有條件的控制上。
while循環:
while循環,直到表達式變爲假,才退出。while循環,表達式是一個邏輯表達式,必須返回一個True或False
語法:
while expression: statement(s)
break :跳出整個循環
continue:跳出本次循環
練習腳本若是下:
腳本1:
#!/usr/bin/python n = 0 while 1: if n == 10: break print(str(n)+" hello") n += 1
結果:
0 hello 1 hello 2 hello 3 hello 4 hello 5 hello 6 hello 7 hello 8 hello 9 hello
腳本2:
#!/usr/bin/env python sth='' while sth != 'q': sth=input("Please input sth,q for exit:") if not sth: break if sth == 'quit': continue print ('continue') else: print ('GOOD BYE')