【Python】Python2和Python3的一些常见功能区别

目录

1.python2 和python3的print区别

2.python2和python3中input的区别

3.python2 和 python3中range的区别

4.python2 和python3中除法的区别

5.python2 和python3中字典方法的区别


1.python2 和python3的print区别

# python2:
print "hello"

# python3:
print("hello")


2.python2和python3中input的区别

# python2 input解析为int 型, raw input 为str类型
input()
# python3 input解析为str型
input()


3.python2 和 python3中range的区别


# python2: 
xrange()
# python3: 
range()


4.python2 和python3中除法的区别


python2的除法:和c语言类似,两个整数相除的结果一定是整数,除数与被除数有一个是浮点数,结果就是浮点数。
(貌似从2.7开始,python2的除法就和python3一样了,反正2.6.4是不行的)

# Python2:
121/4=30
# Python3:
121/4=30.25


要想python2实现和python3一样的效果,两种方法可行(2.6.4版本测试通过)。

1.导入division 模块

from future import division


2.使用浮点数参与计算。

num=121/4.0


5.python2 和python3中字典方法的区别


确定key 是否存在于字典中,返回True 或Flase

# python2: 
dict.has_key(key)
# python3: 
in 或 not in

6.python2 和python3不等于号的区别

# python2:
<>

# python3:
!=

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员赵大宝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值