欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解析Python中while true的使用

 更新時(shí)間:2015年10月13日 19:20:50   投稿:goldensun  
這篇文章主要介紹了解析Python中while true的使用,while true即用來(lái)制造一個(gè)無(wú)限循環(huán),需要的朋友可以參考下

無(wú)限循環(huán)
如果條件判斷語(yǔ)句永遠(yuǎn)為 true,循環(huán)將會(huì)無(wú)限的執(zhí)行下去,如下實(shí)例:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

var = 1
while var == 1 : # 該條件永遠(yuǎn)為true,循環(huán)將無(wú)限執(zhí)行下去
  num = raw_input("Enter a number :")
  print "You entered: ", num

print "Good bye!"


以上實(shí)例輸出結(jié)果:

Enter a number :20
You entered: 20
Enter a number :29
You entered: 29
Enter a number :3
You entered: 3
Enter a number between :Traceback (most recent call last):
 File "test.py", line 5, in <module>
  num = raw_input("Enter a number :")
KeyboardInterrupt

注意:以上的無(wú)限循環(huán)你可以使用 CTRL+C 來(lái)中斷循環(huán)。

python while 1 vs while True
Python 3.0之前,他們的執(zhí)行是不同的:
while 1,python會(huì)進(jìn)行優(yōu)化,每次循環(huán)是不會(huì)去檢查1的條件,因此性能會(huì)好
而while True,在python 3k前,True不是保留字,用戶可以True=0,所以,每次還要比較True的值

Python 3.0之后,True/False都變成了保留字,

>>> True = 10


會(huì)報(bào)錯(cuò)
因此,python 3后,while 1和while True效果一樣,都會(huì)被解釋器優(yōu)化

相關(guān)文章

最新評(píng)論