Compared to Python 2.x, python 3.x has made some changes in statement output, encoding, operations, and exceptions, and this article provides a brief overview of these changes.

The python3. x print function replaces the print statement

In Python 2.x, the output data is printed using print statements, such as:

>>>print"3,4" 3,4 >>>print (3,4)Copy the code

But in Python 3.x, the print statement is gone. Instead, the print function is used, for example:

> > > print (3, 4, 3, 4Copy the code

If the print statement is also used as in Python 2.x, the Python compiler will report an error, for example:

>>> print "3,4" <stdin>", line 1 print "3,4" ^ SyntaxError: Missing parentheses in call to 'print'Copy the code

Python 3.x uses UTF-8 encoding by default

In contrast to the ASCII encoding used by Python 2.x by default, Python 3.x uses UTF-8 encoding by default, which supports Chinese and other non-English characters well.

For example, to output a Chinese sentence, the difference between using Python 2.x and Python 3.x is as follows:

X >>> STR =" fingertip programming ">>> STR 'C\xe8\xaf\xad\xe8\xa8\x80\xe4\xb8\xad\xe6\x96\x87\xe7\ XBD \x91' #Python 3.x >>> STR =" fingertip programming ">>> STR 'fingertip programming'Copy the code

Not only that, but in Python3.x, the following code is legal:

>>>print(China) print(ChinaCopy the code

Python 3.x division algorithm

Python’s division algorithm is much more advanced than that of other languages. It contains two operators, / and //, which are used in Python 2.x and Python 3.x as follows:

The operator /

In Python 2.x, the/operator is used in a similar way to Java and C. The result of dividing an integer is still an integer. Floating-point division preserves the decimal part.

1/2 0 > > > > > > 1.0/2 0.5Copy the code

But with the/operator in Python 3.x, division between integers will also result in floating point numbers. Such as:

> > > 1/2 0.5Copy the code

The operator / /

A division operation using the // operator is called floor division, which outputs a maximum integer (rounded down) that is not greater than the value of the result. The use of this operator is the same in Python 2.x as in Python 3.x, for example:

#Python 2.x
>>>-1//2
-1

#Python 3.x
>>>-1//2
-1
Copy the code

Python 3. X

Exception handling changes in Python 3.x in the following ways:

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

2. In Python 2.x, the syntax for catching exceptions is “except Exception, var:”; In Python 3.x, however, the as keyword was introduced, and the syntax for catching exceptions was changed to “except Exception as var:”.

3. In Python 3.x, raise Exception(args) is used instead of Raise Exception, args.

4. In Python 3.x, the sequence behavior of exception classes and the.message attribute are removed.

Sample code for handling exceptions in Python 2.x and Python 3.x is shown below:

# Python 2 x > > > try:... Raise TypeError," TypeError "... Except TypeError, err:... Print err. Message... X >>> Try:... Raise TypeError,(" TypeError ")... Except TypeError as err:... Print (err)... Type errorCopy the code

Python 3.x octal literal representation

In Python 3.x, there is only one way to express octal literals, and it must be written as “0o1000” instead of “01000”. Here’s an example:

#Python 2.x
>>> 0o1000
512
>>> 01000
512

#Python 3.x
>>> 01000
  File "<stdin>", line 1
    01000
        ^
SyntaxError: invalid token
>>> 0o1000
512
Copy the code

Python 3.x is not equal to the operator

The unequal operator in Python 2.x is written in two ways:! = and <>, but in Python 3.x <> is removed, only! = for example:

#Python 2.x >>> 1! =2 True >>> 1<>2 True #Python 3.x >>> 1! =2 True >>> 1<>2 File "<stdin>", line 1 1<>2 ^ SyntaxError: invalid syntaxCopy the code

The Python 3.x data type

There are also changes to data types in Python 3.x, such as:

1.Python 3.x has removed the long type. There is now only one integer int, but it behaves just like the long in Python 2.x.

2.Python 3.x adds bytes types, corresponding to octet strings in Python 2.x.

>>>b=b'China'
>>>type(b)
<type'bytes'>
Copy the code

String objects and bytes objects can be converted to each other using either.encode() or.decode() methods, for example:

>>>s=b.decode()
>>>s
'China'
>>>b1=s.encode()
>>>b1
b'China'
Copy the code

3. In Python 3.x, the dictionary keys(), items(), and Values() methods return iterators, and the previous iterkeys() functions are deprecated. Dict.has_key () was also removed and replaced with in.

This article covers only a few of the changes to Python 3.x, and due to space constraints, I won’t cover them all here.

Learn Python. Learn to program faster and better.