if語句寫法: if expression: statement(s) 注:python使用縮進做爲其語句分組的方法,建議使用4個空格。
(1)條件爲真true (非空的量(string,tuple,list ,set,dictonary),全部非零的數):python
if 1: print 'hello world!' print 'True' if 'aaa': print 'hello world!' print 'True'
(2)條件爲假 faulse(0,None,空的量):express
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 ):code
if not 1>2: print 'hello world!' print 'True' if not 1>2 and 1 == 1: print 'hello world!' print 'True'
if else寫法:input
else語句: if expression: statement(s) else: statement(s) if 1 < 2: print 'hello world' else: print 'Oh,no,fourse!' print 'main'
elfi 語句:string
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!'
#!/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'
多個條件下判斷:it
轉換大小寫:io
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]"