举例说明python2.7.X和python3.X以上版本的区别记录学习

本文详细介绍了从Python 2迁移到Python 3的过程中需要注意的关键差异,包括整数除法、Unicode处理、打印函数的变化以及xrange等函数的使用效率对比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

future 模块

python 3.x引进了一些python2.7.x不兼容的关键字和功能,所以我们可以通过在python 2中导入future模型进行使用python3的特性.

如果我们想在python 2中使用python 3的整除,那么就可以导入下列:
from __future__ import division

future模块的另外一些功能

python 2
print 'Python', python_version()
print 'Hello, World!'
print('Hello, World!')
print "text", ; print 'print more text on the same line'
Python 2.7.6
Hello, World!
Hello, World!
text print more text on the same line
python 3
print('Python', python_version())
print('Hello, World!')

print("some text,", end="")  ##notice:不换行的方式.
print(' print more text on the same line')
Python 3.4.1
Hello, World!
some text, print more text on the same line

整数除法

特别注意的地方是在将python2代码移植到python 3的时候,整数除法是不会引起任何报错信息的,但它却会改变数值的精度. 所以在python 2使用除法时,建议使用future模块的division.

python 2
print 'Python', python_version()
print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '3 / 2.0 =', 3 / 2.0
print '3 // 2.0 =', 3 // 2.0
Python 2.7.6
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0
python 3
print('Python', python_version())
print('3 / 2 =', 3 / 2)
print('3 // 2 =', 3 // 2)
print('3 / 2.0 =', 3 / 2.0)
print('3 // 2.0 =', 3 // 2.0)
Python 3.4.1
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

Unicode

python 2 没有byte类型,而python 3中,我们有Unicode(utf-8) str, 和两种byte类型:byte和bytearray.

python 2
print type(unicode('this is like a python3 str type'))
print type(b'byte type does not exist')
print 'they are really' + b' the same'
print type(bytearray(b'bytearray oddly does exist though'))
<type 'unicode'>
<type 'str'>
they are really the same
<type 'bytearray'>
python 3
print('Python', python_version())
print('strings are now utf-8 \u03BCnico\u0394é!')
print('Python', python_version())
print('strings are now utf-8 \u03BCnico\u0394é!')
print('and Python', python_version(), end="")
print(' also has', type(bytearray(b'bytearrays')))
'note that we cannot add a string' + b'bytes for data'
Python 3.4.1
strings are now utf-8 μnicoΔé!

Python 3.4.1 has <class 'bytes'>

and Python 3.4.1 also has <class 'bytearray'>

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

<ipython-input-13-d3e8942ccf81> in <module>()
----> 1 'note that we cannot add a string' + b'bytes for data'


TypeError: Can't convert 'bytes' object to str implicitly

xrange

使用%timeit + func 的形式来测试该函数测运行效率.注意最好在ipython环境下使用%timeit.

python 2
In [3]: def test_range(n):
   ...:     for i in range(n):
   ...:         pass
   ...:     


In [4]: def test_xrange(n):
   ...:     for i in xrange(n):
   ...:         pass
   ...:     
In [6]: %timeit test_range(1000)
100000 loops, best of 3: 15.4 µs per loop

In [7]: %timeit test_xrange(1000)
The slowest run took 10.07 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 10.7 µs per loop
python 3

而python 3只有range(),并没有xrange()

print('Python', python_version())

print('\ntiming range()')
%timeit test_range(n)

print(xrange(10))
Python 3.4.1

timing range()
1000 loops, best of 3: 520 µs per loop

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)

<ipython-input-5-5d8f9b79ea70> in <module>()
----> 1 print(xrange(10))


NameError: name 'xrange' is not defined

contains method for range objects in python 3

Raising exceptions

python 2
python 3

Handling exceptions

python 2
python 3

next() 和.next()

python 2
python 3

For-loop 和global namespace leak

python 2
python 3

比较无顺序的类型

python 2
python 3

inputs 和input()

python 2
python 3

返回迭代对象

python 2
python 3

round

参考链接:

<think>好的,用户想在VSCode中切换Python 2Python 3的配置。首先,我需要回忆VSCode的相关设置。记得VSCode通过Python扩展提供解释器选择功能。用户可能需要在状态栏选择解释器,或者修改工作区设置。 根据引用[1][2],切换Python解释器可以通过状态栏的Python Interpreter选择器。用户点击状态栏的Python版本部分,会弹出可用解释器列表,包括Python 2Python 3的环境。这里需要指导用户如何找到这个选项,可能需要截图或详细步骤,但文本回复的话,要明确说明点击位置。 另外,配置工作区设置也是一个方法。用户可以在.vscode/settings.json中指定python.defaultInterpreterPath,指向具体的Python路径。比如,Windows、macOSLinux的路径可能不同,需要分别举例说明。例如,Windows可能是"C:/Python27/python.exe",而Linux可能是"/usr/bin/python2.7"。 还需要考虑虚拟环境的情况,如果用户使用virtualenv或conda,解释器路径可能在不同位置。这时候,Python扩展应该能自动检测这些环境,并在选择器中显示出来。可能需要提醒用户确保已经安装了Python扩展,并且解释器已经正确配置在系统路径中。 另外,用户可能遇到问题,比如选择器中没有显示想要的Python版本。这时候需要检查Python扩展是否正确安装,或者手动添加解释器路径到设置中。同时,不同项目可能需要不同的解释器,所以工作区设置是推荐的方式,避免全局影响。 最后,是否需要重启VSCode或重新加载窗口?可能在某些情况下需要,但通常选择解释器后会自动生效。不过,如果配置更改后没有立即生效,建议用户重启或使用“重新加载窗口”命令。 总结步骤:1. 使用状态栏选择器切换;2. 配置工作区设置;3. 处理不同操作系统的路径差异;4. 检查扩展环境配置。确保回答清晰,分步骤,并引用提供的参考资料。</think>在VSCode中切换Python 2Python 3的配置可以通过以下步骤实现: ### 一、通过状态栏快速切换 1. 打开任意Python文件 2. 查看VSCode底部状态栏右侧的Python版本标识(如显示`Python 3.10.4`) 3. 点击该标识,将弹出可用Python解释器列表 4. 从列表中选择需要的Python 2Python 3解释器[^1][^2] ### 二、配置工作区设置 1. 创建/打开项目文件夹 2. 按`Ctrl+,`打开设置界面 3. 搜索`python.defaultInterpreterPath` 4. 输入对应路径: - Windows示例: ```json "python.defaultInterpreterPath": "C:/Python27/python.exe" ``` - macOS/Linux示例: ```json "python.defaultInterpreterPath": "/usr/bin/python3" ``` ### 三、多版本共存配置技巧 1. 为不同项目创建独立配置: - 在项目根目录创建`.vscode/settings.json` - 添加指定解释器路径 2. 使用虚拟环境管理: ```bash # Python2虚拟环境 virtualenv -p python2.7 venv_py2 # Python3虚拟环境 python3 -m venv venv_py3 ``` 生成的虚拟环境会自动出现在解释器选择列表中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值