1.输出差异
python2:
>>> print "ok"
ok
>>> print ("ok")
ok
>>>
python3:
>>> print("ok")
ok
>>> print "ok"
File "<stdin>", line 1
print "ok"
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("ok")?
>>>
2.输入差异
python2:
>>> num1 = input("please input your number")
please input your number12
>>> type(num1)
<type 'int'>
>>> num1 = input("please input your number")
please input your number"abc"
>>> type(num1)
<type 'str'>
>>> num1 = raw_input("please input your number")
please input your number12
>>> type(num1)
<type 'str'>
>>>
3.数据类型差异
python2:
>>> type(10000000000000000000000000000000)
<type 'long'>
>>>
python3:
>>> type(100000000000000000000000000)
<class 'int'>
>>>
4.运算符差异
python2:
>>> 3/5
0
>>> 6/4
1
>>> 6.0/4
1.5
>>>
python3:
>>> 3/5
0.6
>>> 6/4
1.5
>>> 3//5
0
>>> 6//4
1
>>>
5.比较运算
>>> 6 <> 5
True
>>> 6 != 5
True
>>>
python3:
>>> 6 <> 5
File "<stdin>", line 1
6 <> 5
^
SyntaxError: invalid syntax
>>> 6 != 5
True
>>>
6.range的差异
python2:
>>> range(10) #直接返回一个列表,存在内存里
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> xrange(10) #生成一个可迭代对象,用的时候才生成到内存里,用完就释放内存
xrange(10)
>>>
python3:
>>> range(10) #生成一个可迭代对象,和python2的xrange一样
range(0, 10)
>>>
7.异常机制的差异
python2:
except IndexError , ie:
python3:
except IndexError as ie:
8.编码的差异
python2编码:ascii码
python3编码:utf-8
9.布尔类型的差异
python2:True和False不是关键字,可以作为变量名
python3:True和False是关键字,不可以作为变量名
|