1、if单分支:
语法结构:
if 条件:
code...
code...
...
注:
1,条件是表达式,不需要用括号括起来
2,条件的结束要有冒号
3,语句块没有花括号,而是由统一的缩进来实现
eg1:
count=89
if count>80:-------------条件是表达式,不需要用括号括起来print 'larger then 80!'
eg2:
count=int(raw_input('plz inout your math record:'))--------字符串转换为×××才可以!
print countif count>80: print 'larger than 80!'print 'End!'-----------------两个print会分为两行输出,一个属于if分支,一个属于程序2、if双分支
语法结构:
if 条件:
code...
code...
...
else:
code...
code...
注:
1,条件和else后面都要有冒号
2,条件是表达式,不需要用左右圆括号
3,if、else下的语句块,多条语句不需要用花括号,而是由统一缩进来实现
eg1:
count=int(raw_input('plz inout your math record:'))
print countif count>80: print 'larger than 80!'else: print 'lower than 80!'print 'End!eg2:
sex=raw_input('plz input your sex:')--------raw_input不断读入什么,返回的都是string字符串型
if sex=='male':-------------判断字符串是否相等! print 'Gentleman'else: print 'Lady'3,if多分支
if、elif、else 类似于switch...case模型
if...else的嵌套(每个if条件和else后面都要加冒号)--------这种方式容易出错!
count=int(raw_input('plz input your math record:'))
if count>=90: print 'a'else: if count>=80: print 'b' else: if count>=70: print 'c' else: if count>=60: print 'd' else: print 'no pass'print 'End!使用if、多个elif、else的方式实现(每个if条件、elif条件和else后面都要加冒号):
count=int(raw_input('plz input your math record:'))
if count>=90: print 'a'elif count>=80: print 'b'elif count>=70: print 'c'elif count>=60: print 'd'else: print 'no pass'print 'End!'---------------不在缩进范围内的就不属于if或else分支的语句,而属于主程序语句