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的一些区别的更多相关文章

  1. python2.* 版本 与 3.* 版本中的区别

    目录 Unicode编码 print函数 raw_input() 和 input( ) 不等运算符 数据类型 除法 map 和 filter Unicode编码 python2.x 解释器默认编码格式 ...

  2. 版本python2和版本3.X的一个区别之一

    print函数 虽然print语法是Python 3中一个很小的改动,且应该已经广为人知,但依然值得提一下:Python 2中的print语句被Python 3中的print()函数取代,这意味着在P ...

  3. Python2和Python3关于reload()用法的区别

    Python2 中可以直接使用reload(module)重载模块. Pyhton3中需要使用如下两种方式: 方式(1) >>> from imp >>> imp. ...

  4. python2与python3的input函数的区别

    Python3.x 中 input() 函数接受一个标准输入数据,返回为 string 类型. Python2.x 中 input() 相等于 eval(raw_input(prompt)) ,用来获 ...

  5. python2与3实际中遇到的区别

    1.type(1/2) python2是向下取整,0,为int:python3是正常除法,0.5,为float 2.

  6. python2和python3中TestSuite().addTest的区别

    Python2中unittest.TestSuite().addTest()的参数是这样的:unittest.TestSuite().addTest(TestFun("test_nam&qu ...

  7. __getattr__在python2.x与python3.x中的区别及其对属性截取与代理类的影响

    python2.x中的新类型类(New-style class)与python3.x的类一致,均继承object类,而不继承object的类称为经典类(classic class),而对于这两种类,一 ...

  8. python2.X和3.X的一些区别【整理中】

    1.性能 Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可  以取得很好的优化结果.  Py3.1性能比P ...

  9. urllib库在python2和python3环境下的使用区别

    好东西啊!!! Python 2 name Python 3 name urllib.urlretrieve() urllib.request.urlretrieve() urllib.urlclea ...

随机推荐

  1. 实现Kubernetes跨集群服务应用的高可用

    在Kubernetes 1.3版本,我们希望降低跨集群跨地区服务部署相关的管理和运营难度.本文介绍如何实现此目标. 注意:虽然本文示例使用谷歌容器引擎(GKE)来提供Kubernetes集群,您可以在 ...

  2. python3.4中自定义数组类(即重写数组类)

    '''自定义数组类,实现数组中数字之间的四则运算,内积运算,大小比较,数组元素访问修改及成员测试等功能''' class MyArray: '''保证输入值为数字元素(整型,浮点型,复数)''' de ...

  3. .net string类型集合转int集合

    1.string集合转int集合 //string类型的集合 List<string> tempStr = new List<string>() { "21" ...

  4. Linux(Ubuntu)使用日记------markdown文件与pdf,doc,docx文件的相互转化(pandoc使用)

    安装: sudo apt-get install pandoc 使用: man pandoc   查看帮助文档 直接转换,命令如下: pandoc -f markdown -t docx ./test ...

  5. 怎么写自己的CMakeLists.txt

    一. 为什么要使用cmake 理论上说,任意一个C++程序都可以用g++来编译.但当程序规模越来越大时,一个工程可能有许多个文件夹和源文件,这时输入的编译命令将越来越长.通常一个小型C++项目可能含有 ...

  6. 关于解决Tomcat服务器Connection reset by peer 导致的宕机

    org.apache.catalina.connector.ClientAbortException: java.io.IOException: Connection reset by peer at ...

  7. /Date(1555554794000)/ 转换为日期格式

    /Date(1555554794000)/ 转换为 2019/4/18 new Date(parseInt('/Date(1555554794000)/'.substr(6, 13))).toLoca ...

  8. POJ 1821 Fence (算竞进阶习题)

    单调队列优化dp 我们把状态定位F[i][j]表示前i个工人涂了前j块木板的最大报酬(中间可以有不涂的木板). 第i个工人不涂的话有两种情况: 那么F[i - 1][j], F[i][j - 1]就成 ...

  9. ZYNQ原理图中添加RTL设计模块

    前言 已有的RTL模块怎么添加到原理图中? 流程 (1)添加文件到设计中. (2)右键文件添加到block design中. (3)连线即可. 以上.

  10. VimFaultException A specified parameter was not correct configSpec.guestId

    VimFaultException A specified parameter was not correct configSpec.guestId 在对接VMware的环境中创建虚拟机报错 查看错误 ...