• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 解析Python中while true的使用

解析Python中while true的使用

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了python中while true,python while true,python3 while true,while true,while true 什么意思等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

无限循环
如果条件判断语句永远为 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!"
</div>


以上实例输出结果:

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
</div>

注意:以上的无限循环你可以使用 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
</div>


会报错
因此,python 3后,while 1和while True效果一样,都会被解释器优化

</div>

您可能想查找下面的文章:

  • 解析Python中while true的使用
  • 解析Python中while true的使用

相关文章

  • Python内置数据结构与操作符的练习题集锦
  • NumPy统计函数的实现方法
  • Windows下Python2与Python3两个版本共存的方法详解
  • python比较两个列表大小的方法
  • Python+Opencv识别两张相似图片
  • python 图片验证码代码分享
  • 在Python中使用lambda高效操作列表的教程
  • Python 错误和异常小结
  • Python实现把回车符\r\n转换成\n
  • 在漏洞利用Python代码真的很爽

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 零基础学Python(一)Python环境安装
    • Python中zip()函数用法实例教程
    • Python中的True,False条件判断实例分析
    • 详解在Python程序中使用Cookie的教程
    • python操作MySQL数据库的方法分享
    • python中的列表推导浅析
    • Python获取系统默认字符编码的方法
    • python算法学习之基数排序实例
    • 为python设置socket代理的方法
    • python使用正则搜索字符串或文件中的浮点数代码实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有