1.if 用法举例:
if语句写法:
if expression:
statement(s)
注:python使用缩进作为其语句分组的方法,建议使用4个空格。
(1)条件为真true (非空的量(string,tuple,list ,set,dictonary),所有非零的数):
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 ):
if not 1>2:
print 'hello world!'
print 'True'
if not 1>2 and 1 == 1:
print 'hello world!'
print 'True'
2.if else 举例:
if else写法:
else语句:
if expression:
statement(s)
else:
statement(s)
if 1 < 2:
print 'hello world'
else:
print 'Oh,no,fourse!'
print 'main'
3.if elif else写法:
elfi 语句:
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]"