程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Some differences between python2 and python3

編輯:Python

to be divisible by

python3 Return as when dividing float type ,python2 The return is int type

# python3
[email protected]-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
[[email protected] 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
>>>

The method in the dictionary :dict.items()

python3 in dict.items() The return value is dict_items type , python2 in dict.items() The return value is list

dicts = {
"name": "lisi", "age": 1}
dicts2 = {
"class": " mathematics ", "score": 100, "name": "maliu"}
# Python3 Of dict.items() The return value is changed from the original list to dict_items type 
print(type(dicts.items())) # <class 'dict_items'>
print(dict(dicts.items())) # {'name': 'lisi', 'age': 1}
print(list(dicts.items())) # [('name', 'lisi'), ('age', 1)]

map function

stay python3 in map Function returns map Object of type , And in the python2 in map Back to the list .

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

Problems encountered in the project

python Priority of logical operations in

print(not "name" in dicts and dicts["name"]) # in The priority of the > not The priority of the > and The priority of the 
// output
PS D:\code\test> & "D:/Program Files/Python37/python.exe" d:/code/test/test.py
False

priority


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved