python中while循环并列_Python中while循环的一个问题
匿名用户
1级
2013-08-27 回答
是因为你使用的编译器是python3.x的原因:
Python 3.3.1 (default, Apr 17 2013, 22:32:14)
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1
>>> b = 1
>>> while a > 0:
... b += 1
... a /= b
...
>>> print(a, b)
0.0 178如果使用python2.x的话,打印出来将是(0,2)
Python 2.7.4 (default, Apr 19 2013, 18:32:33)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1
>>> b = 1
>>> while a > 0:
... b += 1
... a /= b
...
>>> print (a,b)
(0, 2)
>>> c = (a, b)
>>> print c
(0, 2)
>>> print type(c)
可以比较明显的看到,使用python3.x输出的结果是一个浮点型(float),一个整型(int);而使用python2.x输出的结果是元组(tuple, 直观看就是带括号)。
这就是python3.x和python2.x的比较明显的一个区别---print
在python2.x中,print是个语句(statement),而在3.x中,print是个函数(function),最直接的例子:
在2.x中:
>>> print 'hello world'
hello world
在3.x中:
Python 3.3.1 (default, Apr 17 2013, 22:32:14)
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hello world'
File "", line 1
print 'hello world'
^
SyntaxError: invalid syntax
>>> print ('hello world')
hello world
>>>-------
还有一个比较明显的区别是在3.x中,最后打印的a为0.0(浮点型);而在2.x中,a为0(没有小数点,整型)。这就说明了为什么在python3.x中迭代了178次,因为:
第一次:b = 1 + 1 = 2; a = 1 / 1 + 1 = 0.5
第二次:b = 2 + 1 = 3; a = 0.5 / 2 + 1 = 0.167
....
需要迭代178次,才使得 a 终于
Python 3.3.1 (default, Apr 17 2013, 22:32:14)
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1
>>> b = 1
>>> while a > 0:
... b = b + 1
... a = a / b
... print ('a: ', a)
... print ('b: ', b)
...
a: 0.5
b: 2
a: 0.16666666666666666
b: 3
a: 0.041666666666666664
b: 4
a: 0.008333333333333333
b: 5
a: 0.001388888888888889
...
...
b: 175
a: 5.054e-321
b: 176
a: 3e-323
b: 177
a: 0.0
b: 178
>>> print(1/2)
0.5
这样,你比较容易知道原因了。
同样,我打印出了python2.x的结果:
Python 2.7.4 (default, Apr 19 2013, 18:32:33)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1
>>> b = 1
>>> while a > 0:
... b = b + 1
... a = a / b
... print 'a: ', a
... print 'b: ', b
...
a: 0
b: 2
>>> print 1/2
0
最后,打印是程序调试很有用的方法,通过打印的结果,你会知道每一步的运行结果。
python中while循环并列_Python中while循环的一个问题相关推荐
- python的for语句用法_python中list循环语句用法实例
本文实例讲述了python中list循环语句用法.分享给大家供大家参考.具体用法分析如下: Python 的强大特性之一就是其对 list 的解析,它提供一种紧凑的方法,可以通过对 list 中的每个 ...
- python中循环结构_python中的循环结构等相关知识
分支结构 1.单分支:一般用于只会发生一种情况的场景,if #90以上优秀 score=95 if score>90: print("优秀") 2.双分支:一般用于会发生两种 ...
- python中for循环流程图_Python中的迭代遍历 for in
遍历就是从头到尾依次从列表中获取数据,在循环体内部针对每一个元素,执行相同的操作. 在Python中为了提高列表的遍历效率,专门提供的迭代(iteration)遍历. 使用 for...in 就能够在 ...
- python嵌套列表怎么遍历_Python中遍历列表,循环嵌套
1.遍历列表中的元素:用for(操作元素)和while(操作索引)实现 #将列表中的所有元素打印出来 #for循环主要结合成员运算符使用,此时只能操作元素 list1= ["and" ...
- python回到本次循环开头_Python中,当一个while循环判断为false,结束这个循环的时候,怎么进入到下一个循环中?...
根据题主对问题的描述,题主需要的答案也许是关于 while 循环结构,Continue 和 Break 的详细解释.了解了循环的控制后,题主便能很清楚地明白自己需要如何控制循环来达到想要的答案了. 先 ...
- python中print输出格式汇总_python中print输出格式有哪些
python中print输出格式有:1.可用加号连接[print("I " + "love " + "you")]:2.逗号连接会自动在连接 ...
- python中关键字参数含义_python中接受任意关键字的参数
1.*args args是非关键字参数,可以理解为形参,为了方便记忆我理解它是arguments的缩写. 2.*kwargs kwargs是键值对参数,为了方便记忆我理解它是key word argu ...
- python中else什么意思_python中的else语句
python语言和其它语言一样在支持else语句,通常else语句和if语句合用,完成程序的分支选择功能. 例如如下打印学成成绩代码: score = int(input("请输入成绩:&q ...
- python表示语句块采用_python中什么是语句块?
在python中,语句块是在条件为真(条件语句)时执行或者执行多次(循环语句)的一组语句.在代码前放置空格来缩进语句即可创建语句块,语句块中的每行必须是同样的缩进量. Python的基本语法--语句块 ...
最新文章
- R语言head函数和tail函数获取dataframe、列表list、向量vector的头部和尾部数据:tail提取数据对象的尾部数据、head提取数据对象的头部数据、默认6条数据、自定义设置返回条数
- oracle10g ora 29702,ORA-29702,请帮助一下。
- 【Groovy】Groovy 扩展方法 ( Groovy 扩展方法引入 | 分析 Groovy 中 Thread 类的 start 扩展方法 )
- laravel大型项目系列教程(四)之显示文章列表和用户修改文章
- 跟我一起学.NetCore之EF Core 实战入门,一看就会
- (数学分析笔记)常用函数不定积分及其计算技巧
- Google Cloud Fundamentals简介
- 再次细分防火墙 WebRAY为何定义MAF新品类
- django 文档参考模型
- 光模块有什么用?什么是SFP光模块?
- python 怎么爬桌软件数据_python爬虫怎么从软件界面抓取数据?
- Logstash 中type 和 tags
- 这么用 if-else,小鹿差点被辞退!
- Making Pre-trained Language Models Better Few-Shot Learners
- Spire.XLS:一款Excel处理神器
- 一看就会!Matplotlib图像基础
- Jenkins邮箱配置中,使用SSL连接的问题
- UNIAPP 怎么引入字体
- Vue3中Vuex的使用
- python 如果文件夹不存在则新建
热门文章
- 经典书籍《宽客》阅读心得
- 解决删除谷歌浏览器chrome注册表残留问题
- C# 压缩PDF文档
- 微型计算机使用的crt信号,《微型计算机及其接口技术》课程学习方法篇(2)...
- Repulsion Loss: Detecting Pedestrians in a Crowd 详解(遮挡下的行人检测)
- prerender-spa-plugin linux运行报错问题
- 2021年起重机司机(限桥式起重机)考试题库及起重机司机(限桥式起重机)新版试题
- Bootstrap导航栏下拉菜单不生效的问题
- 网络爬虫在业务中的应用
- 阿里P8整理Mysql面试题答案,助你面试“脱颖而出”