Python 2 to Python 3 convert

2to3, 自动将 Python 2 代码转为 Python 3 代码

https://docs.python.org/zh-cn/2/library/2to3.html

$ 2to3 example.py

# -w
$ 2to3 -w example.py $

demo

before, Python 2

def greet(name):
print "Hello, {0}!".format(name)
print "What's your name?"
name = raw_input()
greet(name)

after, Python 3

def greet(name):
print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)

2to3 in action

# coding: utf8

__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__copyright__ = """
Copyright (c) 2012-2050, xgqfrms; mailto:xgqfrms@xgqfrms.xyz
""" class Test(object):
def sum(a, b):
sum a + b
print "a+b=" + sum
return sum

2to3

# coding: utf8

__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__copyright__ = """
Copyright (c) 2012-2050, xgqfrms; mailto:xgqfrms@xgqfrms.xyz
""" class Test(object):
def sum(a, b):
sum a + b
print("a+b=", sum)
return sum

Python 2.x 与 Python 3​​.x 版本区别

  1. print

  2. input

  3. unicode

  4. 除法运算 ///

  5. 异常 as

  6. xrange

  7. 八进制字面量 & 二进制字面量

八进制数必须写成 0o777,原来的形式 0777 不能用了;

二进制必须写成 0b111

新增了一个bin()函数用于将一个整数转换成二进制字串;

  1. 不等运算符

Python 2.x, != 和 <>

Python 3.x, !=

  1. 去掉了repr表达式``

  2. 多个模块被改名(根据PEP8)

10.数据类型 bytes 字节码类型

  1. 打开文件 open

  1. 函数式编程, map、filter 和 reduce

可迭代的对象, next()

https://www.runoob.com/python/python-2x-3x.html

refs

https://pythonconverter.com/

https://docs.python.org/2/library/2to3.html



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


Python 2 to Python 3 convert的更多相关文章

  1. python 提示 :OverflowError: Python int too large to convert to C long

    一次在使用orm进行联表查询的时候,出现   Python int too large to convert to C long 的问题: 在分析错误之后,在错误最后面提示中有: File " ...

  2. python第六天 函数 python标准库实例大全

    今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...

  3. 如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python 注释

    如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python  注释 PIP $ pip install beauti ...

  4. Python学习笔记—Python基础1 介绍、发展史、安装、基本语法

    第一周学习笔记: 一.Python介绍      1.Python的创始人为吉多·范罗苏姆.1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...

  5. 【Python大系】Python快速教程

    感谢原作者:Vamei 出处:http://www.cnblogs.com/vamei 怎么能快速地掌握Python?这是和朋友闲聊时谈起的问题. Python包含的内容很多,加上各种标准库.拓展库, ...

  6. python学习笔记-python程序运行

    小白初学python,写下自己的一些想法.大神请忽略. 安装python编辑器,并配置环境(见http://www.cnblogs.com/lynn-li/p/5885001.html中 python ...

  7. python day1:初识Python(一)

    一.Python 简介: Python免费.开源,面向对象的解释型语言,其语法简洁,在使用中无需考虑如何管理内存等底层问题,并且支持在linux,windows等多平台运行,Python的标准库很强大 ...

  8. 智普教育Python培训之Python开发视频教程网络爬虫实战项目

    网络爬虫项目实训:看我如何下载韩寒博客文章Python视频 01.mp4 网络爬虫项目实训:看我如何下载韩寒博客文章Python视频 02.mp4 网络爬虫项目实训:看我如何下载韩寒博客文章Pytho ...

  9. 和我一起学python,初识python (life is short ,we need python)

    作者:tobecrazy  出处:http://www.cnblogs.com/tobecrazy 欢迎转载,转载请注明出处.thank you! 由于项目需要(并非因为life is short), ...

随机推荐

  1. How does Go kit compare to Micro?

    Go kit - Frequently asked questions https://gokit.io/faq/ How does Go kit compare to Micro? Like Go ...

  2. B-tree R-tree B+-tree indexes 索引顺序存取方法 ISAM MySQL实现拓展ISAM为MyISAM

    High Performance MySQL, Third Edition by Baron Schwartz, Peter Zaitsev, and Vadim Tkachenko http://d ...

  3. You shouldn't use *any* general-purpose hash function for user passwords, not BLAKE2, and not MD5, SHA-1, SHA-256, or SHA-3

    hashlib - Secure hashes and message digests - Python 3.8.3 documentation https://docs.python.org/3.8 ...

  4. the minimum number of bits required to represent x 最小位数

    src/math/bits/bits.go:296 // --- Len ---// Len returns the minimum number of bits required to repres ...

  5. 济南学习D3T1__线性筛和阶乘质因数分解

    [问题描述] 从1− N中找一些数乘起来使得答案是一个完全平方数,求这个完全平方数最大可能是多少. [输入格式] 第一行一个数字N. [输出格式] 一行,一个整数代表答案对100000007取模之后的 ...

  6. Java三种IO模型和LinuxIO五种IO模型

    Java: https://github.com/Snailclimb/JavaGuide/blob/master/docs/java/BIO-NIO-AIO.md https://github.co ...

  7. linux 下解决mysql root 权限无法远程连接问题

    问题描述:MySQL数据库安装成功后,在服务器本地可以连接成功,但是使用工具navicat无法进行远程连接,如图: 原因:MySQL默认只允许root帐户在本地登录,如果要在其它机器上连接mysql, ...

  8. IP路由__动态路由

    1.使用协议来查找网络并更新路由表的配置,就是动态路由.它比使用静态或默认路由方便,但它需要一定的路由器CPU处理时间和网络链接带宽.路由协议定义了路由器与相邻路由器通信时所使用的一组规则. 在互联网 ...

  9. WPF权限控制——【2】模块、菜单、按钮

    周末没有工作,没有写博客,因为觉得休息很必要:曾听到一句话是这样说的:"你们得救在乎归回安息:你们得力在乎平静安稳".当我想到太阳没秒钟要燃烧420万吨的燃料时,想到的就是造物主的 ...

  10. oss-server 小型对象存储系统

    oss-server 项目介绍 oss-server是针对项目开发时提供的小型对象存储系统,开发者在针对文件上传时业务剥离,同时方便文件迁移,为满足单个项目,多个系统的情况下,提供统一的oss服务 o ...