首页 > 代码库 > Python学习笔记四:过程控制

Python学习笔记四:过程控制

条件语句:

#以缩进来区分表示同一范围。

 1 # coding=utf8 2 # 例1:if 基本用法 3  4 flag = False 5 name = luren 6 if name == python:         # 判断变量否为‘python‘ 7     flag = True          # 条件成立时设置标志为真 8     print welcome boss    # 并输出欢迎信息 9 else:10     print name              # 条件不成立时输出变量名称

多重判断:

 1 # coding=utf8 2 # 例2:elif用法 3  4 num = 5      5 if num == 3:            # 判断num的值 6     print boss         7 elif num == 2: 8     print user 9 elif num == 1:10     print worker11 elif num < 0:           # 值小于零时输出12     print error13 else:14     print roadman     # 条件均不成立时输出

python 并不支持 switch 语句

 1 # coding=utf8 2 # 例3:if语句多个条件 3  4 num = 9 5 if num >= 0 and num <= 10:    # 判断值是否在0~10之间 6     print hello 7 >>> hello        # 输出结果 8  9 num = 1010 if num < 0 or num > 10:    # 判断值是否在小于0或大于1011     print hello12 else:13     print undefine14 >>> undefine        # 输出结果15 16 num = 817 # 判断值是否在0~5或者10~15之间18 if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):    19     print hello20 else:21     print undefine22 >>> undefine        # 输出结果

同一行,与IDL类似

1 #!/usr/bin/python 2  3 var = 100 4  5 if ( var  == 100 ) : print "Value of expression is 100" 6  7 print "Good bye!" 

循环语句:

while

1 #!/usr/bin/python2 3 count = 04 while (count < 9):5    print The count is:, count6    count = count + 17 8 print "Good bye!"

while... else

1 #!/usr/bin/python2 3 count = 04 while (count < 9):5    print The count is:, count6    count = count + 17 8 print "Good bye!"
1 #!/usr/bin/python2 3 flag = 14 5 while (flag): print Given flag is really true!6 7 print "Good bye!"

while循环嵌套

 1 #coding=utf-8 2 #!/usr/bin/python 3  4 i = 2 5 while(i < 100): 6    j = 2 7    while(j <= (i/j)): 8       if not(i%j): break 9       j = j + 110    if (j > i/j) : print i, " 是素数"11    i = i + 112 13 print "Good bye!"

for

遍历任何序列的项目,列表或字符串

for i in sequence

     statements(s)

 1 #!/usr/bin/python 2  3 for letter in Python:     # First Example 4    print Current Letter :, letter 5  6 fruits = [banana, apple,  mango] 7 for fruit in fruits:        # Second Example 8    print Current fruit :, fruit 9 10 print "Good bye!"

>
Current Letter : PCurrent Letter : yCurrent Letter : tCurrent Letter : hCurrent Letter : oCurrent Letter : nCurrent fruit : bananaCurrent fruit : appleCurrent fruit : mangoGood bye!

索引

1 #!/usr/bin/python2 3 fruits = [banana, apple,  mango]4 for index in range(len(fruits)):5    print Current fruit :, fruits[index]6 7 print "Good bye!"

内置函数 len() 和 range(),函数 len() 返回列表的长度,即元素的个数。 range返回一个序列的数。

for.... else...(for循环嵌套)

 1 #!/usr/bin/python 2  3 for num in range(10,20):  #to iterate between 10 to 20 4    for i in range(2,num): #to iterate on the factors of the number 5       if num%i == 0:      #to determine the first factor 6          j=num/i          #to calculate the second factor 7          print %d equals %d * %d % (num,i,j) 8          break #to move to the next number, the #first FOR 9    else:                  # else part of the loop10       print num, is a prime number

break跳出整个循环  continue跳出本次循环  pass空语句,是为了保持程序结构的完整性。

1 #!/usr/bin/python2 3 for letter in Python: 4    if letter == h:5       pass6       print This is pass block7    print Current Letter :, letter8 9 print "Good bye!"

 

Python学习笔记四:过程控制