好程序员分享Python自动化运维开发实战 6、流程控制python
Python条件语句是经过一条或多条语句的执行结果(True或者False)来决定执行的代码块。程序员
Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false。express
if 语句用于控制程序的执行,基本形式为:app
if 判断条件: 执行语句…… else: 执行语句……
其中"判断条件"成立时(非零),则执行后面的语句,而执行内容能够多行,以缩进来区分表示同一范围。less
else 为可选语句,当须要在条件不成立时执行内容则能够执行相关语句运维
if 语句的判断条件能够用>、<、==、>=、<=来表示其关系。函数
当判断条件为多个值时,可使用如下形式:ui
if 判断条件1: 执行语句1…… elif 判断条件2: 执行语句2…… elif 判断条件3: 执行语句3…… else: 执行语句4……
因为 python 并不支持 switch 语句,因此多个条件判断,只能用 elif 来实现。spa
若是判断须要多个条件需同时判断时,可使用 or (或),表示两个条件有一个成立时判断条件成功;code
使用 and (与)时,表示只有两个条件同时成立的状况下,判断条件才成功。
#!/usr/bin/python num = 9 if num >= 0 and num <= 10: print 'hello' num = 10 if num < 0 or num > 10: print 'hello' else: print 'undefine' num = 8 if (num >= 0 and num <= 5) or (num >= 10 and num <= 15): print 'hello' else: print 'undefine'
你也能够在同一行的位置上使用if条件判断语句,以下实例:
var = 100 if ( var == 100 ) : print "变量 var 的值为100" print "Good bye!"
循环语句容许咱们执行一个语句或语句组屡次
Python提供了for循环和while循环(在Python中没有do..while循环):
while 循环 在给定的判断条件为 true 时执行循环体,不然退出循环体。 for 循环 重复执行语句 嵌套循环 你能够在while循环体中嵌套for循环
循环控制语句能够更改语句执行的顺序。Python支持如下循环控制语句:
break 语句 在语句块执行过程当中终止循环,而且跳出整个循环 continue 语句 在语句块执行过程当中终止当前循环,跳出该次循环,执行下一次循环。 pass 语句 pass是空语句,是为了保持程序结构的完整性。
基本形式为:
while 判断条件: 执行语句……
执行语句能够是单个语句或语句块。判断条件能够是任何表达式,任何非零、或非空(null)的值均为true。
当判断条件假false时,循环结束。
实例: #!/usr/bin/python count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!"
continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还能够是个常值,表示循环一定成立,具体用法以下:
# continue 和 break 用法 i = 1 while i < 10: i += 1 if i%2 > 0: # 非双数时跳过输出 continue print i # 输出双数二、四、六、八、10 i = 1 while 1: # 循环条件为1一定成立 print i # 输出1~10 i += 1 if i > 10: # 当i大于10时跳出循环 break
无限循环
若是条件判断语句永远为 true,循环将会无限的执行下去,以下实例:
#!/usr/bin/python # -*- coding: UTF-8 -*- var = 1 while var == 1 : # 该条件永远为true,循环将无限执行下去 num = raw_input("Enter a number :") print "You entered: ", num print "Good bye!"
循环使用 else 语句
在 python 循环里,else 中的语句会在循环正常执行完,即循环不是经过 break 跳出而中断的状况下执行
#!/usr/bin/python count = 0 while count < 5: print count, " is less than 5" count = count + 1 else: print count, " is not less than 5"
简单while语句组
相似if语句的语法,若是你的while循环体中只有一条语句,你能够将该语句与while写在同一行中, 以下所示:
flag = 1 while (flag): print 'Given flag is really true!'
Python for循环能够遍历任何序列的项目,如一个列表或者一个字符串。
for循环的语法格式以下:
for iterating_var in sequence: statements(s)
实例:
#!/usr/bin/python # -*- coding: UTF-8 -*- for letter in 'Python': print '当前字母 :', letter fruits = ['banana', 'apple', 'mango'] for fruit in fruits: print '当前字母 :', fruit 以上实例输出结果: 当前字母 : P 当前字母 : y 当前字母 : t 当前字母 : h 当前字母 : o 当前字母 : n 当前字母 : banana 当前字母 : apple 当前字母 : mango
经过序列索引迭代
另一种执行循环的遍历方式是经过索引,以下实例:
#!/usr/bin/python # -*- coding: UTF-8 -*- fruits = ['banana', 'apple', 'mango'] for index in range(len(fruits)): print '当前水果 :', fruits[index]
以上实例输出结果:
当前水果 : banana 当前水果 : apple 当前水果 : mango
以上实例咱们使用了内置函数 len() 和 range(),函数 len() 返回列表的长度,即元素的个数。 range返回一个序列的数。
循环使用 else 语句
以下实例:
#!/usr/bin/python # -*- coding: UTF-8 -*- for num in range(10,20): # 迭代 10 到 20 之间的数字 for i in range(2,num): # 根据因子迭代 if num%i == 0: # 肯定第一个因子 j=num/i # 计算第二个因子 print '%d 等于 %d * %d' % (num,i,j) break # 跳出当前循环 else: # 循环的 else 部分 print num, '是一个质数'
以上实例输出结果:
10 等于 2 * 5 11 是一个质数 12 等于 2 * 6 13 是一个质数 14 等于 2 * 7 15 等于 3 * 5 16 等于 2 * 8 17 是一个质数 18 等于 2 * 9 19 是一个质数
Python 语言容许在一个循环体里面嵌入另外一个循环。
for iterating_var in sequence: for iterating_var in sequence: statements(s) statements(s)
while expression: while expression: statement(s) statement(s)
continue 语句跳出本次循环,而break跳出整个循环。
continue 语句用来告诉Python跳过当前循环的剩余语句,而后继续进行下一轮循环。
Python pass是空语句,是为了保持程序结构的完整性。
pass 不作任何事情,通常用作占位语句。
Python 语言 pass 语句语法格式以下:
pass
实例:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 输出 Python 的每一个字母 for letter in 'Python': if letter == 'h': pass print '这是 pass 块' print '当前字母 :', letter print "Good bye!"
以上实例执行结果:
当前字母 : P 当前字母 : y 当前字母 : t 这是 pass 块 当前字母 : h 当前字母 : o 当前字母 : n