python第七十课——python2与python3的一些区别
1.性能:
py3.x起始比py2.x效率低,但是py3.x有极大的优化空间,效率正在追赶 2.编码:
py3.x原码文件默认使用utf-8编码,使得变量名更为广阔
中国='CHI'
print(中国) #结果为:CHI
3.语法:
3.1 去除了<>,改用!=
#python2
>>> 1<>2
True >>> 1!=2
True #python3
>>> 1<>2
File "<stdin>", line 1
1<>2
^
SyntaxError: invalid syntax >>> 1!=2
True
3.2 加入as和with关键字,还有True,False,None 3.3 整型触发返回浮点数,整除请使用//
#python2
>>> 5/3
1 >>> 5.0/3
1.6666666666666667 #python3
>>> 5/3
1.6666666666666667
3.4 加入nonlocal语句 3.5 去除print语句,加入print()函数
# py2.x
print 'The answer is',2*2 #py3.x
print('The answer is',2*2)
3.6 去除了raw_input,加入input()函数 3.7 新的super(),可以不再给super()传参数
class C(object):
def __init__(self,a):
print('C',a)
class D(C):
def __init__(self,a):
super().__init__(a) #无参数调用super()
3.8 改变了顺序操作符的行为,例如x<y,当x和y类型不匹配时抛出
TypeError而不是返回随即的bool值
#python2
>>> 2<""
True #python3
>>> 2<""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
3.9 新式的8进制字变量
#python2
>>> 0666
438 #python3
>>> 0666
File "<stdin>", line 1
0666
^
SyntaxError: invalid token >>> 0o666
438
4.字符串和字节串
python2:字符串以8-bit字符串存储 python3:字符串以16-bit Unicode字符串存储,
现在字符串只有str一种类型 5.数据类型
5.1 Py3.x去除了long类型,现在只有一种类型--int,但它的行为就像2.x版本的long 5.2 新增了bytes类型,对应于2.x版本的八位串
>>> b=b'china'
>>> b
b'china'
>>> type(b)
<class 'bytes'>
str对象和bytes对象可以使用.encode()(str->bytes) or .decode()(bytes->str)方法相互转化 6.面向对象
引入抽象基类 7.异常
所有异常都从BaseException继承,并删除了StardardError
#python2
try:
#....
except Exception,e:
#....
#python3
try:
#....
except Exception as e:
#....
8.其他
8.1 xrange()改名为range(),要想使用range()获得一个list,必须显调用
#python2
>>> list(xrange(5))
[0, 1, 2, 3, 4] #python3
>>> list(range(5))
[0, 1, 2, 3, 4]
8.2 file类被废弃
#python2
>>> file
<type 'file'> 打开文件:
file(path)
open(path) #python3
>>> file
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
打开文件:
open(path)
返回目录
python第七十课——python2与python3的一些区别的更多相关文章
- python版本坑:md5例子(python2与python3中md5区别)
对于一些字符,python2和python3的md5加密出来是不一样的. Python2 和Python3MD5加密 # python2.7 pwd = "xxx" + chr(1 ...
- 孤荷凌寒自学python第七十九天开始写Python的第一个爬虫9并使用pydocx模块将结果写入word文档
孤荷凌寒自学python第七十九天开始写Python的第一个爬虫9 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 到今天终于完成了对docx模块针对 ...
- 孤荷凌寒自学python第七十五天开始写Python的第一个爬虫5
孤荷凌寒自学python第七十五天开始写Python的第一个爬虫5 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...
- python解释器的安装;python2与python3同时在环境变量中时的解决方案
新文档 html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,addres ...
- CentOS7保留默认Python版本并安装更新Python2和Python3共存
CentOS 7 默认的python版本是python2.7.5.因为yum依赖于默认的python版本的缘由,所以要先保留默认版本,并修改yum文件头部后,才能开始安装更新python2和pytho ...
- 孤荷凌寒自学python第七十六天开始写Python的第一个爬虫6
孤荷凌寒自学python第七十六天开始写Python的第一个爬虫6 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 不过由于对python-docx模 ...
- 孤荷凌寒自学python第七十四天开始写Python的第一个爬虫4
孤荷凌寒自学python第七十四天开始写Python的第一个爬虫4 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...
- 孤荷凌寒自学python第七十天学习并实践beautifulsoup对象用法3
孤荷凌寒自学python第七十天学习并实践beautifulsoup对象用法3 (完整学习过程屏幕记录视频地址在文末) 今天继续学习beautifulsoup对象的属性与方法等内容. 一.今天进一步了 ...
- 字符编码 + python2和python3的编码区别(day08整理)
目录 昨日回顾 二十三.元组内置方法 二十四.散列表 二十五.字典内置方法 二十六.集合内置方法 二十七.深浅拷贝 拷贝 浅拷贝 深拷贝 今日内容 二十八.字符编码 1.文本编辑器存储信息的过程 2. ...
随机推荐
- struts2_项目运行报404
1.缺少jar包,commons-lang3-3.2.jar 参考网址:https://blog.csdn.net/u013457382/article/details/50972401 2.stru ...
- 2015 Multi-University Training Contest 6 solutions BY ZJU(部分解题报告)
官方解题报告:http://bestcoder.hdu.edu.cn/blog/2015-multi-university-training-contest-6-solutions-by-zju/ 表 ...
- Fzreo matlab
fzero Root of nonlinear function collapse all in page Syntax x = fzero(fun,x0) example x = fzero(fun ...
- php环境安装
Windows安装 下载php压缩包, http://php.net/downloads.php, 一定要下载Windows版本的呦 将压缩包解压到指定目录下: 创建配置文件, 其中有两个配置文件在根 ...
- js 二叉树遍历
二叉树定义这里不再赘述. 我这里有个二叉树: var tree = { "id": 0, "name": "root", "lef ...
- crontab 配置文件
1.系统配置文件 etc/crontab 2.vim打开crontab 以上配置解释 1. 代表用bash去执行shell command line2.代表crontab 默认的环境变量3.cront ...
- K8S 通过 yaml 文件创建资源
创建 pod cd ~ vi pod-demo.yaml # 内容如下 apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: def ...
- TCP/UDP 协议
传输层建立端口到端口的通信. 网络层的 ip 为我们区分子网,以太网层的 mac 帮我们找到主机.然后大家使用的都是应用程序,你的电脑上可能同时开启qq,暴风影音,等多个应用程序,那么我们通过ip和m ...
- jQuery的一生
jQuery 1.什么是jQuery? 是轻量级的,兼容多浏览器的JavaScript库,使用户能够方便的处理HTML Document,Events,实现动画效果,方便进行Ajax交互,能够极大地简 ...
- JS apply的巧妙用法以及扩展到Object.defineProperty的使用
Math.max 实现得到数组中最大的一项 var array = [1,2,3,4,5]; var max = Math.max.apply(null, array); console.log(ma ...