解析Python中while true的使用,pythonwhile
解析Python中while true的使用,pythonwhile
无限循环
如果条件判断语句永远为 true,循环将会无限的执行下去,如下实例:
#!/usr/bin/python # -*- coding: UTF-8 -*- var = 1 while var == 1 : # 该条件永远为true,循环将无限执行下去 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
注意:以上的无限循环你可以使用 CTRL+C 来中断循环。
python while 1 vs while True
Python 3.0之前,他们的执行是不同的:
while 1,python会进行优化,每次循环是不会去检查1的条件,因此性能会好
而while True,在python 3k前,True不是保留字,用户可以True=0,所以,每次还要比较True的值
Python 3.0之后,True/False都变成了保留字,
>>> True = 10
会报错
因此,python 3后,while 1和while True效果一样,都会被解释器优化
相关内容
- 使用python检测主机存活端口及检查存活主机,python存活
- 深入解析Python中的变量和赋值运算符,python运算符
- Python中数字以及算数运算符的相关使用,python运算符
- 各个系统下的Python解释器相关安装方法,python解释器
- Python入门学习之字符串与比较运算符,python运算符
- 初步认识Python中的列表与位运算符,python列表运算符
- Python语法快速入门指南,python语法快速入门
- Python监控主机是否存活并以邮件报警,python监控主机存
- 分享一下Python 开发者节省时间的10个方法,python节省时
- Python二分查找详解,python二分详解
评论关闭