python|python3与python2的一些区别

编码

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'>

异常处理

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值