整除
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
优先级