亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解析Python中while true的使用

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

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

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

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

print "Good bye!"


以上實例輸出結果:

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

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

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

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

>>> True = 10


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

相關文章

最新評論