Home > Backend Development > Python Tutorial > What is the difference between Python2 and Python3

What is the difference between Python2 and Python3

青灯夜游
Release: 2021-04-18 14:06:30
Original
54037 people have browsed it

The differences between Python2 and Python3 are: 1. Differences in output print; 2. Differences in integer division and return values; 3. List comprehension loop variables; 4. Unicode strings; 5. Errors In terms of processing; 6. xrange is different and so on.

What is the difference between Python2 and Python3

The operating environment of this article: Windows 7 system, Dell G3 computer, python version 3.6.4.

This article will introduce to you the differences between Python2 and Python3, so that everyone can understand the Python version. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

If we are not familiar with Python, we may be confused by the different versions of Python available. There is currently no clear answer to the question of which version of Python you should use. The decision depends on what you want to achieve.

Although Python 3 is the latest generation of the language and is the future of the language, many programmers are still using Python 2.7, the final updated version of Python 2 released in 2010. This is because some older libraries and packages only work with Python 2.

Why are there different versions of Python?

Programming languages ​​continue to evolve as developers expand the capabilities of the language and eliminate quirks that cause problems for developers. Python 3 was introduced in 2008 with the goal of making Python easier to use and changing the way it handles strings to match today's requirements for the language. Programmers who are new to programming in Python 2 sometimes find it difficult to adapt to the new changes, but newbies often find that new versions of the language make more sense.

Python 3.0 is fundamentally different from previous Python versions, as it is the first Python version to be incompatible with older versions. Programmers usually don't need to worry about small updates (e.g., from 2.6 to 2.7) because they usually only change the inner workings of Python and don't require programmers to change their syntax. The changes between Python 2.7 (the final version of Python 2) and Python 3.0 are much more significant, making code that works in Python 2.7 likely need to be written differently to work in Python 3.0.

What are the main differences between Python 2 and Python 3?

1. Output --print

In Python 2, "print" is treated as a statement rather than a function. You don't need to wrap the text you want to output in parentheses, although you can. This can be confusing because most other operations in Python use functions that require arguments to be placed within parentheses. It can also cause unexpected results if you place parentheses around a comma-separated list of items to output.

In Python 3, "print" is explicitly treated as a function, which means that you must pass the items you want to output to the function in parentheses in the standard way, otherwise you will get a syntax error . Some Python 2 programmers find this change annoying, but it can help prevent errors from occurring.

Example:

print 'Hello, Geeks'   # Python 3.x 不支持
print('Hope You like these facts')
Copy after login

Output:

Output of Python 2.x:

What is the difference between Python2 and Python3

Python 3.x Output:

What is the difference between Python2 and Python3

Because print 'Hello, Geeks' is not supported in Python 3.x, an error occurred:

What is the difference between Python2 and Python3

2. Integer division:

Python 2 treats numbers without any digits after the decimal point as integers, which may lead to unexpected results during division. For example, if you enter the expression: 3/2 in Python 2 code, it will evaluate to 1, not 1.5 as you expected.

This is because Python 2 will assume that you want the result of the division to be an integer, so it will round the calculation down to the nearest integer. To get the result 1.5, you have to write 3.0/2.0 to tell Python that you want it to return a floating point number, i.e. include the numbers after the decimal point in the result.

Python 3 will evaluate 3/2 as 1.5 by default, which is more intuitive for new programmers.

3. List comprehension loop variables:

In previous Python 2.x versions, the variables iterated in the list comprehension were given the same name as the global variables May cause the value of the global variable to be changed, which is usually not what you want.

This annoying bug has been fixed in Python 3. Therefore, you can use a variable name that is already used for the control variable in the list comprehension without having to worry about it leaking and messing with the value of the variable in the rest of the code.

4. Unicode string:

默认情况下,Python 3是将字符串存储为Unicode,而如果Python 2要将字符串存储为Unicode,则要求使用“u”标记字符串。Unicode字符串比ASCII字符串更通用,它们都是Python 2的默认字符串,因为它们可以存储来自外语的字母以及表情符号和标准的罗马字母和数字。如果要确保Python 3代码与Python 2兼容,你仍然可以使用“u”来标记Unicode字符串。

5、错误处理:

Python 2和Python 3两个版本的错误处理有一个小的变化。

在python 3.x中,'as'关键字是必需的。例:

try: 
    trying_to_check_error 
except NameError, err: 
    print err, 'Error Caused'   # 不能在Python 3中工作
Copy after login

运行:

Python 2.x中输出:

What is the difference between Python2 and Python3

Python 3.x 中运行,报错:

What is the difference between Python2 and Python3

try: 
     trying_to_check_error 
except NameError as err:    # 'as' 是 Python 3.x 必需的存在的关键字
     print (err, 'Error Caused')
Copy after login

运行:

Python 2.x中输出:

What is the difference between Python2 and Python3

Python 3中输出:

What is the difference between Python2 and Python3

6、xrange:

Python 3.x中不存在Python 2.x的xrange()。在Python 2.x中,range返回一个列表,即range(3)返回[0,1,2],而xrange返回一个xrange对象,即xrange(3)返回iterator对象,它与Java迭代器类似,并在需要时生成数字。
如果我们需要多次迭代相同的序列,我们更喜欢range(),因为range提供了一个静态列表。xrange()每次重建序列。xrange()不支持切片和其他列表方法。xrange()的优点是,当任务迭代大范围时,它可以节省内存。

在Python 3.x中,范围函数现在执行xrange在Python 2.x中的功能,因此为了保持代码的可移植性,我们可能希望坚持使用范围。所以Python 3.x的范围函数是来自Python 2.x的xrange。

例:

for x in xrange(1, 5): 
    print(x), 
  
for x in range(1, 5): 
    print(x),
Copy after login

运行:

Python 2.x中输出:

What is the difference between Python2 and Python3

Python 3中运行,报错:

What is the difference between Python2 and Python3

选择哪种版本归结为你需要的是什么库

Python 2已经存在更长时间,这可能是一个优势,并不是所有可用于Python 2的库都已移植到Python 3.另一方面,一些开发人员正在为Python 3创建可能与Python 2不兼容的库。对于很多人来说,决定使用Python 2还是Python 3取决于他们想要使用哪些库。当然,如果你正在学习Python来处理现有的Python应用程序,那么学会使用编写软件的Python版本是有意义的。

许多人认为Python 3是Python 2的改进版本,因为一些更新消除了程序员常见的错误(参见上面的打印示例)。如上所述,一些更改使Python 3更易于初学者理解。因此,不需要使用任何特定库的新程序员可能会考虑学习Python 3,因为在未来几年可能会逐渐转向新语言,因为Python 2的更新停止并支持旧版本该语言版本减少。用法统计显示使用Python 3的程序员数量已逐渐增加。

除非有明确的理由选择一个版本的Python而不是另一个版本,例如需要处理用Python 2编写的现有代码,否则不值得担心这个决定。大多数语法在每种语言版本中都是相同的。如果你需要从Python 2切换到Python 3,反之亦然,那么熟悉打印语句/函数的更改以及Python处理整数除法的方式之间的差异不应花费太长时间。

总结:以上就是本篇文章的全部内容,希望能对大家的学习有所帮助。

The above is the detailed content of What is the difference between Python2 and Python3. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template