alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【320】Python 2.x 与 3.x 的区别

通过代码移植的报错进行梳理!

 

1. print 函数的区别

  Python 2.x 中可以加空格或者括号,但是 Python 3.x 只能是括号的

1
2
3
4
5
6
7
8
9
# Python 2.x
>>> print "processing..."
processing...
>>> print("processing...")
processing...
 
# Python 3.x
>>> print("processing...")
processing...

 

2. raw_input 与 input 函数

  Python 2.x 中 raw_input 与 Python 3.x 中 input 函数类似,而 Python 2.x 中的 input 函数接收数字或者带引号字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Python 2.x
>>> a = raw_input("Value: ")
Value: Alex
>>> a
'Alex'
>>> b = input("Value: ")
Value: "Alex"
>>> b
'Alex'
 
# Python 3.x
>>> a = input("Value: ")
Value: Alex
>>> a
'Alex'

参考:python2.x和python3.x中raw_input( )和input( )区别

 

3. Tkinter 模块

  Python 2.x 为 Tkinter,但是 Python 3.x 为 tkinter(小写)

1
2
3
4
5
# Python 2.x
>>> import Tkinter
 
# Python 3.x
>>> import tkinter

参考:Python GUI编程(Tkinter)

 

4. 除法运算符

  Python 2.x 中 /、// 均为整除,但是 Python 3.x 中 / 表示除以,// 表示整除

1
2
3
4
5
6
7
8
9
10
11
12
13
# Python 2.x
>>> 77/60
1
>>> 77//60
1
 
# Python 3.x
>>> 77/60
1.2833333333333334
>>> int(77/60)
1
>>> 77//60
1

 

5. cmd 默认 Python 版本

  需要通过修改环境变量来切换 Python 2.x 和 Python 3.x

参考:如何快速切换Python运行版本,如何选择Python版本

 

posted on   McDelfino  阅读(278)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示