编码
python2编码:ascii码
python3编码:utf-8
bool值
python2中用0和1表示真假,false和true不是关键字,可以作为变量名
>>> True = 5
>>> False = 10
>>> print(True,False)
(5, 10)
python3中则不能作为变量名了
>>> True = 5
File "<stdin>", line 1
SyntaxError: can't assign to keyword
>>> False = 10
File "<stdin>", line 1
SyntaxError: can't assign to keyword
>>>
print()
python3中语法规则为
>>> print("abc")
abc
>>>
python2中语法规则为
>>> print("ok")
ok
>>> print "ok"
ok
数据类型
python2中长整数数据类型为long
>>> type(100)
<type 'int'>
>>> type(10000000)
<type 'int'>
>>> type(10000000000)
<type 'int'>
>>> type(100000000000000)
<type 'int'>
>>> type(10000000000000000000000)
<type 'long'>
python3中都为int
>>> type(100)
<class 'int'>
>>> type(100000)
<class 'int'>
>>> type(10000000000000)
<class 'int'>
>>> type(100000000000000000000000)
<class 'int'>
>>> type(1000000000000000000000000000000000000000)
<class 'int'>
>>> type(1000000000000000000000000000000000000000000000000)
<class 'int'>
整数除法
python3中为真除,如
>>> 3/5
0.6
>>> 6/4
1.5
python2中整数为地板除,如
>>> 3/5
0
>>> 6/4
1
如果想要实现真除只能使用浮点类型
<>
python2中
>>> 6<>5
True
表示6不等于5,在python3中不支持这一方法
File "<stdin>", line 1
6<>5
^
SyntaxError: invalid syntax
range()
python2中range()直接返回一个列表,把数据都生成好了,xrange()运行的时候才生成数据
>>> range(5)
[0, 1, 2, 3, 4]
>>> xrange(5)
xrange(5)
python3中的range()就是python2中的xrange(),返回的是一个迭代器
input()
python3中的input()就是python2中的raw_input()
返回值全为str类型
>>> a = input("请输入:")
请输入:123
>>> type(a)
<class 'str'>
>>>
python2中使用input函数从键盘输入时必须给数据添加双引号,否则报错。返回值也不全为str类型
raw_input()从键盘输入时不需要给数据加双引号了,返回值都为str类型
>>> input("请输入:")
请输入:abc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'abc' is not defined
>>> input("请输入:")
请输入:"abc"
'abc'
>>> raw_input("请输入:")
请输入:abc
'abc'
>>> a = input("请输入:")
请输入:123
>>> type(a)
<type 'int'>