概述
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
关于数字
# <> 运算符在三中被弃用, 比较大多用于数字, 所以就放在这里
# python3中range生成一个数字的生成器, 而在2中直接生成列表. 2中有xrange生成生成器
# 3中弃用了long长整型
# 2中/是整除, 3中则不是, 3中的整除是//
最后
以上就是昏睡蜜蜂为你收集整理的python3.6和2.7的区别_Python2.7与3.6的一些区别的全部内容,希望文章能够帮你解决python3.6和2.7的区别_Python2.7与3.6的一些区别所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复