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 ...
随机推荐
- MySQL实现按天统计数据的方法
一.首先生成一个日期表,执行SQL如下: CREATE TABLE num (i int); ), (), (), (), (), (), (), (), (), (); CREATE TABLE i ...
- nginx安装,启动亲测有效
一:安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 二:安装PCRE,让 N ...
- 【算法】shortest distance
好不容易找到的. #include<iostream> #include<iomanip> #include<cmath> using namespace std; ...
- 【.Net】优秀的开源框架
AutoMapper 官网:http://automapper.org/ github:https://github.com/AutoMapper/AutoMapper 文档:http://autom ...
- ES6 对象增强
对象字面量语法扩展: 1, 属性初始化语法简写:给一个属性赋一个变量值,如果变量名和属性名相同,可以省略变量名和冒号,直接写属性名,js引擎在执行代码的时候,自动查找 和属性命名相同的变量并赋值. l ...
- jquery笔记整理
01-jquery简介 1)功能: ·html元素选取 ·Html元素操作 ·Css操作 ·Html事件函数 ·JavaScript特效和动画 ·DOM ...
- codeforces850E Random Elections
题目链接:codeforces 850E 翻译:luogu 读题是第一要务(大选这么随便真的好吗) 其实答案问你的就是在所有选民心中支持的人的所有情况中,能让一个人连赢两场的情况数是多少 我们假设\( ...
- ES6中6种声明变量的方法
相关阅读:http://es6.ruanyifeng.com/#docs/let 相关阅读:https://www.cnblogs.com/ksl666/p/5944718.html 相关阅读:htt ...
- Linux lvs-NAT模式配置详解
本篇文档主要是记录NAT模式实现过程,以及各配置步骤的原理.“lvs三种模式工作原理”中描述了LVS的NAT.DR.TUN三种模式的工作原理. NAT模式是通过director将报文目标IP地址修改, ...
- 如何对mRemoteNG在进行Linux终端访问时自定义配色
Its not that easy to config mRemoteNG custom color themes, not like XShell which is really convinent ...