python2.7过渡到python3.6时遇到的差别总结

1.Python3中print为一个函数,必须用括号括起来而Python2中print为classpython

print('hello')函数

2.python3将raw_input和input进行了整合,只有input,输入的为str编码

3.Python3中/表示真除,%表示取余,//结果取整;Python2中带上小数点/表示真除,%表示取余,//结果取整code

4.python3中没有了xrange,python3的range就是xrange,也就是说range()返回的再也不是一个列表而是一个class,若是要获得列表须要list(),for i in range(3)照样能够这样使用unicode

5.python3中字典类方法has_key()也再也不支持,用运算符处理,eg:'abc' in dic
字典的items()和keys()返回的再也不是一个列表而是一个class,须要用list()转换为list;字符串

6.字符串编码
在Python2中,做为两种类型的字符序列,str与unicode须要转换,它们是这样转换的.
str——decode方法——》unicode——encode方法——》strinput

u'string'          string

在python3中能够这样对应这转换,配合上面的图,也许会好理解一点。
byte——decode(解码)方法——》str——>encode(编码)方法——》byte 
it

b'string'    class

相关文章
相关标签/搜索