python2与python3的区别(持续更新)

1,print(打印),python2不换行可以使用逗号,python3不换行使用end=''

python2版本:

print 'a' 输出一个字符串

print a 输出一个变量

print 'abc',
print 'def'
      运行结果:abc defg
      

python3版本:

print('a') 输出一个字符串

print(a) 输出一个变量

print('abc',end='')
print('def')

       运行结果:abcdef

2,输入 python3取消了raw_input的输入方式,使用input接收输入。

Python2版本:

raw_input是在Python2版本中的,用户所有的输入都会被作为字符串处理。

input 只能接收整型。

Python3版本:

无raw_input函数,使用input函数接收处理。

用户输入的是什么就是什么。比如,你输入的是字符串:

n  = input('Please input somthing:')

用户输入字符串,'abcdefg',需要加上单引号,才能表示字符串,不然python解释器会认为是变量的。

用户输入数字:123

用户输入变量:  a 

3,list使用sort排序

Python2中的list既可以是字符串和数字,然后使用sort排序,排序的时候使用Ascall码。

list1 = ['a','b',123]
list1.sort()
print list1

        运行结果:[123, 'a', 'b']

Python3中的list必须是纯数字才可以排序。

list1 = ['a','b',123]
list1.sort()



        运行结果:

Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/20180119/index.py", line 12, in <module>
list1.sort()
TypeError: '<' not supported between instances of 'int' and 'str'

 3,python3中字典没有iteritmes,iterkeys,itervalues等方法。

4,python3只有range,没有xrange函数。

持续更新!!!

原文地址:https://www.cnblogs.com/lin1/p/8315493.html