Python2.7与3.6的一些区别

Posted 瓜田月夜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python2.7与3.6的一些区别相关的知识,希望对你有一定的参考价值。

2.7实现了一部分3的功能, 更早版本可能会稍稍涉及一点

首先是关键字的差别

python3.6

import  keyword
print(keyword.kwlist)
[‘False‘, ‘None‘, ‘True‘, ‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, 
‘except‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘nonlocal‘, ‘not‘, ‘or‘, ‘pass‘, 
‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]

python2.7

import keyword
keyword.kwlist
[‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘, 
‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, 
‘with‘, ‘yield‘]

python2.4

[‘and‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, 
‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘,‘lambda‘, ‘not‘, 
‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘yield‘]

这么多看不过来了把, 哈哈

python36 = {‘False‘, ‘None‘, ‘True‘, ‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘,
            ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘,
            ‘in‘, ‘is‘, ‘lambda‘, ‘nonlocal‘, ‘not‘, ‘or‘, ‘pass‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘,
            ‘with‘, ‘yield‘}

python27 = {‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘,
            ‘exec‘, ‘finally‘, ‘for‘,‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘,
            ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘,‘with‘, ‘yield‘}

python24={‘and‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘,
            ‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘,‘lambda‘, ‘not‘,
            ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘yield‘}


print(python36-python27) # {‘nonlocal‘, ‘False‘, ‘True‘, ‘None‘}
print(python36-python24) # {‘False‘, ‘nonlocal‘, ‘None‘, ‘as‘, ‘with‘, ‘True‘}

最主要的就是这个nonlocal了, python2中存在False, True, None但是不属于关键字

语法差异

异常:

# python3
try:
    raise IndexError("傻傻")
except IndexError as e:
    print(e)

# python2
try:
    raise IndexError("傻傻")
except IndexError , e:
    print(e)

其他

yield from # 好像是3.4
async和await  # 3.5

  

数据类型

在python3中str是Unicode编码的字节, bytes是其他字节

在python2中str默认是ascii编码的字节, Unicode是另一种类型

# 在pycharm中点击bytes
bytes = str

bytes: 就是八个二进制的字节, 我们存储和在网络传输中实际上都是字节

Unicode, GBK, ASCII, UTF8都是将字节翻译成有用的信息, Unicode可以存储所有文字, 可以用他来做一个中间编码来转换其他的编码

python3

# 转成字节
a = "爸爸"
byte = a.encode("utf8")
print(byte, type(byte))

byte = bytes(a, encoding="utf8")
print(byte, type(byte))

# 转成字符串
st = byte.decode("utf8")
print(st, type(st))

st = str(byte, encoding="utf8")
print(st, type(st))

python2

# _*_ coding:utf8 _*_

# 转成Unicode, 这里用utf8转是因为上面写的
nui = "我是你爸爸".decode("utf8")
print nui, type(nui)

# 转成字符串
st = nui.encode("utf8")
print st, type(st)

其他

输入

# python3
input() -> str

# python2
raw_input() -> str
python2中也有input不过他只能接收数字

输出

# python3
print() 

# python2
print

关于数字

# <> 运算符在三中被弃用, 比较大多用于数字, 所以就放在这里

# python3中range生成一个数字的生成器, 而在2中直接生成列表. 2中有xrange生成生成器

# 3中弃用了long长整型

# 2中/是整除, 3中则不是, 3中的整除是//

  

以上是关于Python2.7与3.6的一些区别的主要内容,如果未能解决你的问题,请参考以下文章

记录python2.7迁移到python3.6过程中的一些代码差异

mac python2.7.10 升级到 3.6

怎么同时安装python2.7和3.6

Cent OS 7.0离线升级python2.7至3.6

如何解决 Python 3.6 中的 UnicodeDecodeError?

ubuntu16.04LTS服务器,python2.7升级到3.6,且同时升级pip