python学习旅程笔记3-控制流

来源:互联网 发布:龙珠超 知乎 编辑:程序博客网 时间:2024/05/20 18:17

Python的基本运算表达式

(1)判断语句

关于if语句的规则我不再介绍,只在这里提出Python下if语句的用法,以及特点。

[python] view plaincopy
  1. #!/usr/bin/python  
  2. # Filename: if.py   
  3.   
  4. number = 23  
  5. guess = int(raw_input('Enter an integer : '))  
  6.   
  7. if guess == number:  
  8.     print 'Congratulations, you guessed it.' # New block starts here  
  9.     print "(but you do not win any prizes!)" # New block ends here  
  10. elif guess < number:  
  11.     print 'No, it is a little higher than that' # Another block  
  12.     # You can do whatever you want in a block ...  
  13. else:  
  14.     print 'No, it is a little lower than that'   
  15.     # you must have guess > number to reach here  
  16.   
  17. print 'Done'  
  18. # This last statement is always executed, after the if statement is executed   

 

以下为输出

 

[python] view plaincopy
  1. $ python if.py  
  2. Enter an integer : 50  
  3. No, it is a little lower than that  
  4. Done  
  5. $ python if.py  
  6. Enter an integer : 22  
  7. No, it is a little higher than that  
  8. Done  
  9. $ python if.py  
  10. Enter an integer : 23  
  11. Congratulations, you guessed it.  
  12. (but you do not win any prizes!)  
  13. Done   

在这里,我们使用了一个函数raw_input(),用以获取用户的输入。

我们为内建的raw_input函数提供一个字符串,这个字符串被打印在屏幕上,然后等待用户的输入。一旦我们输入一些东西,然后按回车键之后,函数返回输入。对于raw_input函数来说是一个字符串。我们通过int把这个字符串转换为整数,并把它存储在变量guess中。事实上,int是一个类,不过你对它所需了解的只是它把一个字符串转换为一个整数。

 

注意:我们使用了缩进层次来告诉Python每个语句分别属于哪一个块。这就是为什么缩进在Python如此重要的原因。

         在Python中没有switch语句。不过你可以使用if..elif..else语句来完成同样的工作。

 

 

(2)循环语句

 

1.while语句

Python的while语句,与其他语言的while语句区别不大。

[python] view plaincopy
  1. #!/usr/bin/python  
  2. # Filename: while.py  
  3.   
  4. number = 23  
  5. running = True  
  6.   
  7. while running:  
  8.     guess = int(raw_input('Enter an integer : '))  
  9.   
  10.     if guess == number:  
  11.         print 'Congratulations, you guessed it.'   
  12.         running = False # this causes the while loop to stop  
  13.     elif guess < number:  
  14.         print 'No, it is a little higher than that'   
  15.     else:  
  16.         print 'No, it is a little lower than that'   
  17. else:  
  18.     print 'The while loop is over.'   
  19.     # Do anything else you want to do here  
  20.   
  21. print 'Done'   

 

输出为:

 

[python] view plaincopy
  1. $ python while.py  
  2. Enter an integer : 50  
  3. No, it is a little lower than that.  
  4. Enter an integer : 22  
  5. No, it is a little higher than that.  
  6. Enter an integer : 23  
  7. Congratulations, you guessed it.  
  8. The while loop is over.  
  9. Done   

 

注意:在Python中,你可以在while循环中使用一个else从句。

 

2.for语句

for语句,在这里,我通过一个例子在作出说明

[python] view plaincopy
  1. #!/usr/bin/python  
  2. # Filename: for.py  
  3.   
  4. for i in range(15):  
  5.     print i  
  6. else:  
  7.     print 'The for loop is over'   

 

输出为:

[python] view plaincopy
  1. $ python for.py  
  2. 1  
  3. 2  
  4. 3  
  5. 4  
  6. The for loop is over   

 

注意:else部分是可选的。如果包含else,它总是在for循环结束后执行一次,除非遇到break语句。

与C/C++语言相比,Python的if语句,无疑简单了许多。

 

(3)其他语句

1.break语句

break语句在for循环和while循环中使用。

[python] view plaincopy
  1. while True:  
  2.     s = raw_input('Enter something : ')  
  3.     if s == 'quit':  
  4.         break  
  5.     print 'Length of the string is', len(s)  
  6. print 'Done'   

 

输出为

[python] view plaincopy
  1. $ python break.py  
  2. Enter something : Programming is fun  
  3. Length of the string is 18  
  4. Enter something : When the work is done  
  5. Length of the string is 21  
  6. Enter something : if you wanna make your work also fun:  
  7. Length of the string is 37  
  8. Enter something :       use Python!  
  9. Length of the string is 12  
  10. Enter something : quit  
  11. Done   

 

 

2.continue语句

在这里,我只通过一个例子来说明。

[python] view plaincopy
  1. #!/usr/bin/python  
  2. # Filename: continue.py  
  3.   
  4. while True:  
  5.     s = raw_input('Enter something : ')  
  6.     if s == 'quit':  
  7.         break  
  8.     if len(s) < 3:  
  9.         continue  
  10.     print 'Input is of sufficient length'  
  11.     # Do other kinds of processing here...   

 

输出为:

[python] view plaincopy
  1. $ python continue.py  
  2. Enter something : a  
  3. Enter something : 12  
  4. Enter something : abc  
  5. Input is of sufficient length  
  6. Enter something : quit   

 

0 0
原创粉丝点击