Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - What are the new features of Python3 compared with Python2?
What are the new features of Python3 compared with Python2?
Python is an open source and free universal scripting language. It is simple to use and powerful, and it is also one of the most popular programming languages on the Internet. Whether it is traditional Web development, PC software development, Linux operation and maintenance, or big data analysis, machine learning, artificial intelligence, Python can be competent. For those who are ready to teach themselves or want to improve Python, finding a suitable course can often get twice the result with half the effort! I have provided you with four sets of Python master video courses by teacher Li, the gold medal lecturer of Chaoxiang Education. If you are interested, you can click Learn ~

What are the new features of Python3 compared with Python2?

1, Python3.xprint function replaced the print statement.

In Python2.x, the Print statement is used to output data, for example:

But in Python3.x, the print statement disappeared and was replaced by the print function, such as:

If you still use the print statement as in Python2.x, the Python compiler will report an error, such as:

2.Python3.x uses UTF-8 encoding by default.

Compared with the ASCII encoding adopted by Python2.x by default, Python3.x adopts UTF-8 encoding by default, which can well support Chinese or other non-English characters.

For example, to output a sentence in Chinese, the differences between Python2.x and Python3.x are as follows:

In addition, in Python3.x, the following code is legal:

3.Python3.x division operation

Compared with other languages, Python's division operation is much higher. Its division operation contains two operators, namely/and//. The usage of these two operators in Python2.x and Python3.x is as follows:

/operator

In Python2.x, the way of division with the operator/is similar to Java and C languages. The result of integer division is still an integer, and floating-point division will retain the decimal point, for example:

However, using the/operator in Python3.x, if the integer is divisible, the result will be a floating-point number. For example:

Operator//

The division operation with the operator//is called base division, that is, the largest integer (rounding) not greater than the result value is output. The usage of this operator is the same in Python2.x and Python3.x, for example:

4.Python3.x exception

In Python3.x version 3. X. Changes in exception handling are mainly reflected in the following aspects:

In Python version 2.x, all types of objects are thrown directly, but in Python version 3.x, only objects that inherit BaseException can be thrown.

In Python version 2.x2.x, the syntax for catching exceptions is "exceptException, var:"; However, in Python3.x version 3. X, the as keyword is introduced, and the syntax for catching exceptions is changed to "exceptExceptionasvar:".

In Python3.x version 3. X use "raiseException(args)" instead of "raiseException, args" when handling exceptions.

In Python3.x version 3. Sequence behavior and. The message property of the exception class was canceled.

The sample code about exception handling in Python2.x and Python3.x versions is as follows:

5.Python3.x Octal Text Representation

In Python3.x, there is only one way to express octal literal, which must be written as "0o 1000". The original method of "0 1000" cannot be used. For example:

6.Python3.x is not equal to the operator.

There are two ways to write inequality operators in Python2.x, namely! = sum

7.Python3.x data type

The data types in Python3.x have also changed, such as:

Python3.x has removed the long type, and now there is only one integer int, but its behavior is the same as the long in Python2.x version.

Python3.x adds bytes type, which corresponds to the octet string of Python2.x version. The method of defining byte literals is as follows:

String objects and byte objects can be accessed through the. Encode () or. Decode () method, for example:

In Python3.x, the keys (), items () and values () methods of the dictionary are to return iterators, and the previous functions such as iterkeys () are abandoned. At the same time, dict.has_key () was removed and replaced in in.

The above is about "What are the new features of Python3 compared with Python2?" I already shared it. I hope the rabbit's wonderful answer will help you learn Python programming! Python computer language looks very professional and difficult to learn, but as long as you master the principle logic, you can gradually master the breakthrough! If you want to teach yourself Python, please click this link: