新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python中無限循環(huán)有什么條件
循環(huán)

我們提供的服務(wù)有:成都網(wǎng)站制作、成都做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、衡陽縣ssl等。為1000多家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的衡陽縣網(wǎng)站制作公司
如果條件判斷語句永遠(yuǎn)為 true,循環(huán)將會的執(zhí)行下去。
如下實例
#!/usr/bin/python
# -*- coding: UTF-8 -*-
var = 1
while var == 1 : # 該條件永遠(yuǎn)為true,循環(huán)將執(zhí)行下去
num = raw_input("Enter a number :")
print "You entered: ", num
print "Good bye!"
以上實例輸出結(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, innum = 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會進(jìn)行優(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)化
網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:Python中無限循環(huán)有什么條件
文章網(wǎng)址:http://m.fisionsoft.com.cn/article/djpiogo.html


咨詢
建站咨詢
