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
关于数字
# <> 运算符在三中被弃用, 比较大多用于数字, 所以就放在这里 # python3中range生成一个数字的生成器, 而在2中直接生成列表. 2中有xrange生成生成器 # 3中弃用了long长整型 # 2中/是整除, 3中则不是, 3中的整除是//
Python2.7与3.6的一些区别的更多相关文章
- python2.* 版本 与 3.* 版本中的区别
目录 Unicode编码 print函数 raw_input() 和 input( ) 不等运算符 数据类型 除法 map 和 filter Unicode编码 python2.x 解释器默认编码格式 ...
- 版本python2和版本3.X的一个区别之一
print函数 虽然print语法是Python 3中一个很小的改动,且应该已经广为人知,但依然值得提一下:Python 2中的print语句被Python 3中的print()函数取代,这意味着在P ...
- Python2和Python3关于reload()用法的区别
Python2 中可以直接使用reload(module)重载模块. Pyhton3中需要使用如下两种方式: 方式(1) >>> from imp >>> imp. ...
- python2与python3的input函数的区别
Python3.x 中 input() 函数接受一个标准输入数据,返回为 string 类型. Python2.x 中 input() 相等于 eval(raw_input(prompt)) ,用来获 ...
- python2与3实际中遇到的区别
1.type(1/2) python2是向下取整,0,为int:python3是正常除法,0.5,为float 2.
- python2和python3中TestSuite().addTest的区别
Python2中unittest.TestSuite().addTest()的参数是这样的:unittest.TestSuite().addTest(TestFun("test_nam&qu ...
- __getattr__在python2.x与python3.x中的区别及其对属性截取与代理类的影响
python2.x中的新类型类(New-style class)与python3.x的类一致,均继承object类,而不继承object的类称为经典类(classic class),而对于这两种类,一 ...
- python2.X和3.X的一些区别【整理中】
1.性能 Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可 以取得很好的优化结果. Py3.1性能比P ...
- urllib库在python2和python3环境下的使用区别
好东西啊!!! Python 2 name Python 3 name urllib.urlretrieve() urllib.request.urlretrieve() urllib.urlclea ...
随机推荐
- RPM管理工具
linux软件包从内容上可以分为binary code和source code(二进制包和源码包) binary code无需编译,可以直接使用 source code需要经过GCC,C++编译环境编 ...
- vscode的插件收集
转:https://zhuanlan.zhihu.com/p/27905838 转:https://segmentfault.com/a/1190000006697219
- 微擎$_W['uniacid']无法获取
原因: 微擎非系统级别管理员(不是商户管理员),必须要https才能取到值
- Spring Boot整合Elasticsearch
Spring Boot整合Elasticsearch Elasticsearch是一个全文搜索引擎,专门用于处理大型数据集.根据描述,自然而然使用它来存储和搜索应用程序日志.与Logstash和K ...
- Matlab安装完成后,出现错误licensing error:-8523的解决方法
Matlab2018安装成功后,打开出现licensing error:-8523解决方法 https://blog.csdn.net/qq_41634276/article/details/8000 ...
- nginx日志格式定义和nginx.conf配置模板说明
在http的功能里添加log_format模块,内容如下: log_format main escape=json '{ "@timestamp": "$time_iso ...
- 微信小程序 TLS 版本必须大于等于1.2问题解决
微信小程序 TLS 版本必须大于等于1.2问题解决 此问题最近在微信小程序开发中,比较常见. 在解决这个问题之前,我们需要了解一下,当前的系统环境是否支持TLS1.2以上,可以参考一下表格: 确认系 ...
- .Net Core在Centos7上初体验
本文主要内容是简单介绍如何在centos7上开发.Net Core项目,在此之前我们首先了解下.Net Core的基本特性. 1 .Net Core和.Net FrameWork的异同 1.1 .Ne ...
- easyExcel导出excel的简单使用
easyExcel导出excel的简单使用 Java解析.生成Excel比较有名的框架有Apache poi.jxl.但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定 ...
- 编译VisualVM源码解决乱码问题
编译VisualVM源码解决乱码问题 起因 今天在使用VisualVM对测试服务器进行JVM监控的时候,发现所有统计图的横纵坐标都是显示乱码(小方块),即使我的Ubuntu系统使用的是英文语言环境.奇 ...