[dev][python] 从python2进阶到python3你都需要了解什么
基于python2快速掌握python3
0. 前言
- 这是一篇road map。
- 如果你会python2,读完这篇文章之后,你将掌握python3
1. 为什么会出现python3
- python2中的string类型存在歧义,它是一个C字符串也是一个str对象。
python3去除这一不明确的string用法。 - python2 对unicode兼容不好。
因为python的第一个版本早于unicode的第一个版本。 - python的后续版本不希望向后兼容1)和2)
基于以上三点,出现了python3. 总之就是想弄的更好又不想束手束脚。
2. python的禅
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
3. 基于python2的python3教程
3.1 不得不porting的两个理由
1. Python 3 is simply a nicer language than Python 2.
2. more code will be written in Python 3 than in Python 2 over the history of the Python language, so not porting means your project will eventually be left behind.
3.2 主要的区别
3.2.1 除法
python2里的除法是与C语言保持一致的,一个斜杠“/”代表向下取整。
python3里不在是这样,有两个符号表示除法“/”,和“//”
一个斜线‘/’代表严格意义的除法,结果是float类型。结果为int向下取整的除法是两个斜线‘//’。
3.2.2 类
python3中所有类派生自object类。
python2中不是这样的???好吧。。。
3.2.3 字符串与二进制
python2中用str对象表示字符串和二进制,用unicode对象表示unicode。
python3中用bytes对象表示二进制,用str对象表示一切text。
3.2.4 print
print从一个表达式变成了一个函数。所以在python3里,print需要加括号。像python2里那个的语法将报错。
3.2.5 异常捕获
在python3,多个异常类型,需要用括号括起来,如:
>>> import sys
>>> try:
... a = 1/'0'
... except (ZeroDivisionError, TypeError):
3.2.6 int和long
在python3里,int和long类型被合并。在数组后边加后缀L的语法会报错,如:
“1024L”
在python3里,0开头的数字也会报错。0o开头代表8进制,0x开头代表16进制。
3.2.7 unicode和binary
在python3里,
尽管这两个语法也好使:
u"", b""
但应该尽量选择用下面这种:
u(), b()
3.2.8 C扩展时的区别
python2的C扩展与python3不同,然后python2的本来我就不熟。略。
3.2.9 缩进
在python2里,一个tab和8个空格可能对等互换。也就是说在python2里,你可以一个tab和8个空格混用来缩进
解释器也不会报错。
在python3里,这样是不用的。一个tab只能与一个tab等价。
3.3 所有python2与python3之间的不同
好了,现在可以快乐的python3了。
[dev][python] 从python2进阶到python3你都需要了解什么的更多相关文章
- 【python】python2.x 与 python3.x区别对照+缩进错误解决方法
仅仅列出我用到的,不全. 划重点: 1. urllib2 用 urllib.request 代替 2. urllib.urlencode 用 urllib.parse.urlencode 代替 3. ...
- 在Windows下同时安装Python2.x和Python3.x
前言: Python现在是两个版本共存,Python2.x和Python3.x都同时在更新.但是Python2.x和Python3.x的区别还是很多的(以后我可能会写一篇文章列举一下Python2.x ...
- python之路-python2.x与python3.x区别
Python崇尚优美.清晰.简单,是一个优秀并广泛使用的语言. Python2.x 与 Python3.x的区别: python2.x:源码混乱,重复代码较多,冗余. python3.x:源码规范,崇 ...
- Python第一天:python2.x和python3.x的区别
查看Python版本 # python -V Python2.7.5是centos7中默认安装的Python [root@localhost ~]# python -V Python [root@lo ...
- python环境搭建-Linux系统下python2.7升级python3.5.2步骤
首先Python 查看版本 , 在Linux下特别注意权限问题,创建目录时候切记给予权限 如果是 ubnutu 请使用首先切换到 sudo su , 否则 make install 会出现问题.. 升 ...
- python 内置2to3工具将python2代码转换为python3代码
python2与python3代码不兼容,如果需要python2代码在python3环境下运行,需要将代码进行转换,本文介绍使用python3内置工具2to3.py对代码进行转换 一:2to3.py在 ...
- python:python2与python3共存时,pip冲突,提示Fatal error in launcher: Unable to create process using '"d:\python27\python2.exe" "D:\Python27\Scripts\pip2.exe" '
问题背景: 机器上同时装了python2.和python3后,导致只能用pip3了,使用pip2时提示:Fatal error in launcher: Unable to create proces ...
- Python第五十一天 python2升级为python3
Python第五十一天 python2升级为python3 公司使用的生产环境系统是centos7,所以这里以centos7系统为基础,讲解将python2升级为python3的方法 centos7 ...
- Python语言基础-语法特点、保留字与标识符、变量、基本数据类型、运算符、基本输入输出、Python2.X与Python3.X区别
Python语言基础 1.Python语法特点 注释: 单行注释:# #注释单行注释分为两种情况,例:第一种#用于计算bim数值bim=weight/(height*height)第二种:bim=we ...
随机推荐
- WEB 3D SVG CAD 向量 几个实施(转)
一.他们所有的发展.从地上爬起来 VML+SVG发展矢量地图.你并不需要导入第三方的图片作为背景,直接在地图编辑器可以在底图内容编辑,由于岩石.巷道.煤层.画水.础地图样子再在其上面画出智慧线等设 ...
- Redis学习之二 数据类型和相关命令
原文:https://www.cnblogs.com/lonelyxmas/p/9073928.html 如果还不懂安装的,请看 Windows环境下安装Redis Redis一共支持五种数据类型 1 ...
- zabbix3.2使用fping批量监控ip的连通性
.在zabbix-agent端安装fping wget http://www.fping.org/dist/fping-3.16.tar.gz tar zxvf fping-3.16.tar.gz c ...
- 【原创】大数据基础之Spark(4)RDD原理及代码解析
一 简介 spark核心是RDD,官方文档地址:https://spark.apache.org/docs/latest/rdd-programming-guide.html#resilient-di ...
- C++入门篇三
引用:& &放在左边就是引用,放在右边就是取地址 int main() { //引用的类型必须相同,一经引用,不可以在被引用 ; int &b = a;//b引用a之后,两个同 ...
- js 校验 btc eth 地址
NPM 安装 npm install wallet-address-validator Browser <script src="wallet-address-validator.mi ...
- C#学习-接口的成员
在接口中定义方法不能添加任何访问修饰符,因为接口中的方法是默认为public,如果显式地指定了修饰符,则会出现编译时错误. 定义玩接口之后,如果有类想继承该接口,则它必须显示接口中定义的所有方法. 在 ...
- Python调用selenium
import time from selenium import webdriver from selenium.webdriver.common.touch_actions import Touch ...
- 期货大赛项目|五,表格插件datatatables在MVC中的应用
系统中都需要表格,我见过最好的表格就是Datatables了,但中文文档有限,英文能力有限,就写一些简单用法 上图看效果先 要了分页和排序 基本用法 引入js和css bundles.Add(new ...
- hdu5707-Combine String(DP)
Problem Description Given three strings a, b and c , your mission is to check whether c is the combi ...