标签:python学习笔记3—流程控制if、for、while
流程控制if
if 语句
if expression:
statement(s)
else
else语句:
if 语句,else语句
if expression:
statement(s)
else:
statement(s)
elif语句:
if expression1:
statement1(s)
elif expression2(s):
statements2(s)
else:
statement2(s)
注:Python使用缩进作为其语法分组的方法,建议使用4个空格
逻辑值(bool)包含了两个值:
True:表示非空的量(比如:string,tuple,list,set,dictonary)所有非零数。
false:表示0,None,空的量等,包括空的元组、空的列表、空的字典。
[root@localhost ~]# vim 1.py #!/usr/bin/python score = int(raw_input("Please input you score")) if score >= 90: print ‘A‘ print ‘very good‘ elif score >= 80: print ‘B‘ print ‘good‘ elif score >= 70: print ‘C‘ print ‘pass‘ else : print ‘D fail‘ print ‘End‘ [root@localhost ~]# python 1.py Please input you score90 A very good End [root@localhost ~]# python 1.py Please input you score80 B good End [root@localhost ~]# python 1.py Please input you score70 C pass End [root@localhost ~]# python 1.py Please input you score10 D fail End
[root@localhost ~]# vim 2.py #!/usr/bin/python yn = raw_input("Please input [Yes/No]") yn = yn.lower() if yn == ‘y‘ or yn == ‘yes‘: print "program is running" elif yn == ‘n‘ or yn == ‘no‘: print "program is exit" else: print "please input [Yes/No]" [root@localhost ~]# python 2.py Please input [Yes/No]yes program is running [root@localhost ~]# python 2.py Please input [Yes/No]no program is exit
本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1773603
标签:python学习笔记3—流程控制if、for、while
原文地址:http://daixuan.blog.51cto.com/5426657/1773603