Python2与Python3的某些区别

整除

python3整除时返回为float类型,python2返回的是int类型

# python3
root@bian-virtual-machine:/home# python3
Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(4 / 2)
2.0

# python2
[root@localhost local]# python
Python 2.7.5 (default, Nov 16 2020, 22:23:17) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print(4 / 2)
2
>>> 

字典中的方法:dict.items()

python3中dict.items()返回值为dict_items类型, python2中dict.items()返回值为列表

dicts = {"name": "lisi", "age": 1}
dicts2 = {"class": "数学", "score": 100, "name": "maliu"}
# Python3的 dict.items()返回值由原来的列表修改为dict_items类型
print(type(dicts.items()))    # <class 'dict_items'>

print(dict(dicts.items()))    # {'name': 'lisi', 'age': 1}

print(list(dicts.items()))    # [('name', 'lisi'), ('age', 1)]

map函数

在python3中map函数返回的是map类型的对象,而在python2中map返回的是列表。

# python3
def square(x):
    return x ** 2

res = map(square, [1, 2, 3, 4])
print(list(res))
print(type(res))

// output
[1, 4, 9, 16]
<class 'map'>

# python2
Python 2.7.5 (default, Nov 16 2020, 22:23:17) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> res = map(lambda x: x**2, [1,2,3])
>>> res
[1, 4, 9]
>>> type(res)
<type 'list'>
>>> 

项目中遇到的问题

python中逻辑运算的优先级

print(not "name" in dicts and dicts["name"])  # in的优先级 > not的优先级 > and的优先级

// output
PS D:\code\test> & "D:/Program Files/Python37/python.exe" d:/code/test/test.py
False

优先级
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

活火石

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

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

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

打赏作者

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

抵扣说明:

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

余额充值