Python2.x和Python3.x的区别

python2.x 与python3.x 的区别

所有的区别都来源于python官方文档,建议朝python3.x版本转换的童鞋都看下官方文档https://docs.python.org/3/whatsnew/index.html

主要差别

Old: print "The answer is", 2*2
New: print("The answer is", 2*2)

Old: print x,           # Trailing comma suppresses newline
New: print(x, end=" ")  # Appends a space instead of a newline

Old: print              # Prints a newline
New: print()            # You must call the function!

Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)

Old: print (x, y)       # prints repr((x, y))
New: print((x, y))      # Not the same as print(x, y)!

python3 不在自动支持空白空格

The print() function doesn’t support the “softspace” feature of the old print statement. For example, in Python 2.x, print "A\n", "B" would write "A\nB\n"; but in Python 3.0, print("A\n", "B") writes "A\n B\n".

排序比较

1.python3不在支持不同类型间的<, <=, >=, >操作,会抛出一个TypeError.None < None也是如此
2.sotred和list.sort()不在接受cmp函数,
3.删除了cmp函数,删除__cmp__。如果实现排序需要使用__lt__ __hash__ __eq__.如果确实需要使用类似cmp(a,b)的功能。使用如下表达式(a>b)-(a<b)

整型

1.python3不在存在long
2.1/2 返回float,如果实现取证使用1//2
3.长整型repr()不在表现为"11L"
4.Octal literals are no longer of the form 0720; use 0o720 instead. 

文本

八进制表示方式。

1.八进制限制使用0o720替代0720

nonlocal关键字

新的解包规则

(a, *b, c) = range(5)  
print(a)
0
print(b)
[1, 2, 3]
print(c)
4

异常新的语法

    try:
        pass
    except Exception as exe:
        pass

元类使用

1.新的语法
        class C(metaclass=M):
        pass
2. 模块的__metaclass__语法不在支持
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值