[转载] Python条件语句和循环语句
文章参考:http://www.runoob.com/python
一、条件语句
1. 布尔变量
False,None,0,"",(),[],{}值在作为布尔表达式时,会被解释器看作假。其他都为真。
布尔值True(1)为真,False(0)为假。
2. Python条件语句
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
Python 编程中 if 语句用于控制程序的执行,基本形式为:
if 判断条件: 执行语句…… else: 执行语句……
其中"判断条件"成立时(非零),则执行后面的语句,而执行内容可以多行,以缩进来区分表示同一范围。
else 为可选语句,当需要在条件不成立时执行内容则可以执行相关语句,具体例子如下:
flag = False name = 'luren' if name == 'python': flag = True print 'welcome boss' else: print name
输出结果为:luren
if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)来表示其关系。
当判断条件为多个值时,可以使用以下形式:
if 判断条件1: 执行语句1…… elif 判断条件2: 执行语句2…… elif 判断条件3: 执行语句3…… else: 执行语句4……
示例如下:
num=2 if num==1: print "num is 1" elif num==5: print "num is 5" elif num==8: print "num is 8" else: print "num is another number"
由于 python 并不支持 switch 语句,所以多个条件判断,只能用 elif 来实现,如果多个条件需同时判断时,可以使用 or (或),表示两个条件有一个成立时判断条件成功;使用 and (与)时,表示只有两个条件同时成立的情况下,判断条件才成功。
当if有多个条件时可使用括号来区分判断的先后顺序,括号中的判断优先执行,此外 and 和 or 的优先级低于>(大于)、<(小于)等判断符号。
二、循环语句
循环语句允许我们执行一个语句或语句组多次。
Python提供了for循环和while循环(在Python中没有do..while循环):
循环控制语句
循环控制语句可以更改语句执行的顺序。Python支持以下循环控制语句:
1. while循环
while语句用于循环执行语句,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务,其基本形式如下:
while 判断条件:
执行语句......
执行流程图如下:
示例1:while循环简单示例
num=1 while (num<5): print "the current num is: ",num num=num+1 print "OK"
打印结果如下:
the current num is: 1 the current num is: 2 the current num is: 3 the current num is: 4 OK
while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立,具体用法如下:
num=1 while (num<10): num=num+1 if num%2>0: //非双数时跳出输出 continue print "the num is:",num //输出2,4,6,8,10 i=1 while 1: //循环条件为1必定成立 print i; i=i+1 if i>10: //当i大于10时跳出循环 break
示例2:猜大小游戏
#!/usr/bin/python import random s = int(random.uniform(1,10)) #print(s) m = int(input('please input a intnum:')) while m != s: if m > s: print ('your input is big') m = int (input('please input a intnum:')) if m < s: print ('your input is small') m = int (input('please input a intnum:')) if m == s: print('Congratulations,you are right!') break;
执行结果如下:
$ python game.py please input a intnum:9 your input is big please input a intnum:5 your input is big please input a intnum:3 your input is big please input a intnum:2 Congratulations,you are right!
2. for循环
for循环可以遍历任何序列的项目,如一个列表或者字符串
for循环语法格式如下:
for iterating_var in sequence: statements(s)
for循环流程图:
示例1:循环遍历字符串字母或者列表
#!/usr/bin/python for letter in 'Python': print 'the current letter :', letter fruits = ['banana', 'apple', 'mango'] for fruit in fruits: print 'the current fruit :', fruit print "Good bye!"
执行结果如下:
the current letter : P
the current letter : y
the current letter : t
the current letter : h
the current letter : o
the current letter : n
the current fruit : banana
the current fruit : apple
the current fruit : mango
Good bye!
示例2:通过序列索引迭代遍历
#!/usr/bin/python fruits = ['banana', 'apple', 'mango'] for index in range(len(fruits)): print 'the current fruit :', fruits[index] print "Good bye!"
执行结果如下:
the current fruit : banana
the current fruit : apple
the current fruit : mango
Good bye!
for循环中使用else
在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
补充:无论是java、C++等循环语句学习过程中,总会有例如打印菱形、三角形等,下面使用Python来进行实现类似功能,链接http://www.cnblogs.com/secdata/p/7362080.html