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. PHP 框架之一Laravel

    Laravel: Laravel The phpFramework for Web Artisans and one of the best php framework in year 2014. L ...

  2. 深入理解SPI机制-服务发现机制

    https://www.jianshu.com/p/3a3edbcd8f24 SPI ,全称为 Service Provider Interface,是一种服务发现机制.它通过在ClassPath路径 ...

  3. Redis 雪崩、穿透和击穿

    https://github.com/doocs/advanced-java/blob/master/docs/high-concurrency/redis-caching-avalanche-and ...

  4. SpringMVC听课笔记(十一:国际化)

    1. 关于国际化 -- 在页面上根据浏览器的语言设置情况对文本(不是内容),时间,数值进行本地化处理 使用JSTL的fmt标签 -- 可以在bean中获取国际化资源文件 Locale对应的消息 在be ...

  5. eclipse中Tomcat修改项目名称

    1.打开你的项目目录,找到一个.project文件,打开后修改<name> test</name>中的值,将test修改成你要修改的名字: 2.在项目目录下,打开.settin ...

  6. 读取固定ResourceBundle

    private static ResourceBundle ssoBundle = ResourceBundle.getBundle("CASHHSSO");//默认根目录 pub ...

  7. 8. Linux重要的环境变量

    1.Linux 系统执行命令的4个步骤 第 1 步:判断用户是否以绝对路径或相对路径的方式输入命令(如/bin/ls),如果是的话则直接执行.  第 2 步:Linux 系统检查用户输入的命令是否为& ...

  8. springboot基础配置-->Properties配置

    Spring Boot项目中的application.properties配置文件一共可以出现在如下4个位置: 项目根目录下的config文件夹中. 项目根目录下. classpath下的config ...

  9. 【函数分享】每日PHP函数分享(2021-2-6)

    array_combine - 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 说明: array_combine ( array $keys , array $values ) : ...

  10. cf1291c-Mind Control

    题意:n个数n个人依次取数,每个人只能取第一个数或最后一个数,你可以从一开始控制k个人取最前边或是最后边的数,你排在第m位,能取到的最大的数是多少.所有人取数都是最优策略(不是每次取最大数). 题解: ...