python3.6和2.7的区别_Python2.7与3.6的一些区别

2.7实现了一部分3的功能, 更早版本可能会稍稍涉及一点

首先是关键字的差别

python3.6

import keyword

print(keyword.kwlist)

[‘False‘, ‘None‘, ‘True‘, ‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘,

‘except‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘nonlocal‘, ‘not‘, ‘or‘, ‘pass‘,

‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]

python2.7

import keyword

keyword.kwlist

[‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘exec‘, ‘finally‘, ‘for‘,

‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘,

‘with‘, ‘yield‘]

python2.4

[‘and‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘,

‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘,‘lambda‘, ‘not‘,

‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘yield‘]

这么多看不过来了把, 哈哈

python36 = {‘False‘, ‘None‘, ‘True‘, ‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘,

‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘,

‘in‘, ‘is‘, ‘lambda‘, ‘nonlocal‘, ‘not‘, ‘or‘, ‘pass‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘,

‘with‘, ‘yield‘}

python27 = {‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘,

‘exec‘, ‘finally‘, ‘for‘,‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘not‘, ‘or‘,

‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘,‘with‘, ‘yield‘}

python24={‘and‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘,

‘exec‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘,‘lambda‘, ‘not‘,

‘or‘, ‘pass‘, ‘print‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘yield‘}

print(python36-python27) # {‘nonlocal‘, ‘False‘, ‘True‘, ‘None‘}

print(python36-python24) # {‘False‘, ‘nonlocal‘, ‘None‘, ‘as‘, ‘with‘, ‘True‘}

最主要的就是这个nonlocal了, python2中存在False, True, None但是不属于关键字

语法差异

异常:

# python3

try:

raise IndexError("傻傻")

except IndexError as e:

print(e)

# python2

try:

raise IndexError("傻傻")

except IndexError , e:

print(e)

其他

yield from # 好像是3.4

async和await # 3.5

数据类型

在python3中str是Unicode编码的字节, bytes是其他字节

在python2中str默认是ascii编码的字节, Unicode是另一种类型

# 在pycharm中点击bytes

bytes = str

bytes: 就是八个二进制的字节, 我们存储和在网络传输中实际上都是字节

Unicode, GBK, ASCII, UTF8都是将字节翻译成有用的信息, Unicode可以存储所有文字, 可以用他来做一个中间编码来转换其他的编码

python3

# 转成字节

a = "爸爸"

byte = a.encode("utf8")

print(byte, type(byte))

byte = bytes(a, encoding="utf8")

print(byte, type(byte))

# 转成字符串

st = byte.decode("utf8")

print(st, type(st))

st = str(byte, encoding="utf8")

print(st, type(st))

python2

# _*_ coding:utf8 _*_

# 转成Unicode, 这里用utf8转是因为上面写的

nui = "我是你爸爸".decode("utf8")

print nui, type(nui)

# 转成字符串

st = nui.encode("utf8")

print st, type(st)

其他

输入

# python3

input() -> str

# python2

raw_input() -> str

python2中也有input不过他只能接收数字

输出

# python3

print()

# python2

print

关于数字

# <> 运算符在三中被弃用, 比较大多用于数字, 所以就放在这里

# python3中range生成一个数字的生成器, 而在2中直接生成列表. 2中有xrange生成生成器

# 3中弃用了long长整型

# 2中/是整除, 3中则不是, 3中的整除是//

原文:https://www.cnblogs.com/wwg945/p/9812498.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值