Python2.x and 3.x versions difference Ⅱ

Division

Python in the division than other languages ​​is very high, there is a very complex set of rules. Python division in two operators, / and //

First of all / division:

In the python 2.x / division just like most of the language we cooked https://www.xuanhe.net/ noted, such as Java C ah ah almost integer result of the division is an integer, the fractional part is completely ignored , decimal floating-point division will retain part of a floating point result obtained.

In the python 3.x / division did not, for the division between integers, floating point result will be.

Python 2.x:

 

 Python 3.x:

 

 

For // division, this division is called a division floor, a floor operation will automatically result of division, is the same in the python 2.x and python 3.x.

python 2.x:

python 3.x:

 

 

abnormal

3 abnormal processing in Python also slightly changed, and we now use as a keyword in Python 3.

Syntax for catching exceptions made  except exc, var  changed  the except EXC AS var .

Syntax except (exc1, exc2) as var can catch exceptions multiple categories at the same time. Python 2.6 has support for both syntax.

  • 1. In the 2.x era, are all types of objects that can be thrown directly in the 3.x era, only inherited from BaseException objects can be thrown.
  • 2. 2.x raise statement throws commas to separate object types and parameters, 3.x canceled the wording of this wonderful work directly call the constructor throws an object can be.

 

In 2.x era, anomalies in the code in addition to that program errors, but also often do some things common control structures should do, it can be seen in 3.x, the designers make an exception become more loyal, and only in error the situation in order to deal with abnormal capture statement.


xrange

Creating an iteration object in Python xrange 2 () usage is extremely popular. For example: for a loop or list / set / inferential dictionary.

The performance is like a generator (such as. "Lazy evaluation"). But this xrange-iterable is infinite, unlimited means that you can traverse.

Because of its lazy evaluation, if you not only do not traverse it once, xrange () function () faster (for example for loop) ratio range. Nevertheless, a comparison of iteration, iteration is not recommended that you repeat several times, because the generator from scratch each time.

In Python 3, range () is like xrange () so as to achieve a specific xrange () function no longer exists (in Python 3 in xrange () will throw an exception name).

 

 Python 2

 

 Python 3

 

 

Guess you like

Origin www.cnblogs.com/danjiu/p/12034281.html