|
| 1 | +.. _python_2_python_3: |
| 2 | + |
| 3 | +.. |
| 4 | + >>> import numpy as np |
| 5 | +
|
| 6 | +Python 2 and Python 3 |
| 7 | +===================== |
| 8 | + |
| 9 | +**Author**: *Pierre de Buyl* |
| 10 | + |
| 11 | +.. topic:: Python 2 / 3 |
| 12 | + |
| 13 | + Two major versions of Python exist, Python 2 and Python 3. Python 3 is the only |
| 14 | + supported version since january 2020 but **the two versions coexisted for about a decade |
| 15 | + of transition from Python 2 to Python 3.** The transition has come to and end as most |
| 16 | + software libraries drop Python 2 support. |
| 17 | + |
| 18 | + |
| 19 | +A very short summary |
| 20 | +-------------------- |
| 21 | + |
| 22 | +- **Python 2 is not supported by the Python Software Foundation since January 1st 2020.** |
| 23 | + There will be no more security patches for Python 2.7. See `Sunsetting Python 2 |
| 24 | + <https://www.python.org/doc/sunset-python-2/>`_ and the `Python 3 Q & A |
| 25 | + <http://python-notes.curiousefficiency.org/en/latest/python3/questions_and_answers.html>`_. |
| 26 | + |
| 27 | +- **The default choice for everyone should be Python 3.** Choosing Python 2 should remain |
| 28 | + motivated by specific circumstances such as the dependency on unported libraries, with the |
| 29 | + understanding of the lack of official and community support. |
| 30 | + |
| 31 | +- Python 2 and Python 3 share most of their syntax, enabling many programmers to port their |
| 32 | + programs. It is even possible to make many codes Python 2/3 compatible, even though there |
| 33 | + are limitations. This strategy was important in making the transition but is no longer |
| 34 | + recommended. |
| 35 | + |
| 36 | +- The division of integers, 1/2 for instance, returns 0 under Python 2 (integer division, |
| 37 | + preserving type) and 0.5 under Python 3 (real division, promoting the integer to a |
| 38 | + floating point value). **A line of code can thus execute with no visible warning in both |
| 39 | + Python 2 and Python 3 but result in different outcomes.** |
| 40 | + |
| 41 | +- Most scientific libraries have moved to Python 3. NumPy and many scientific software |
| 42 | + libraries dropped Python 2 support or will do so soon, see the `Python 3 statement |
| 43 | + <https://python3statement.org/>`_. |
| 44 | + |
| 45 | + |
| 46 | +The SciPy Lecture Notes dropped Python 2 support in 2020. The release 2020.1 is almost |
| 47 | +entirely Python 2 compatible, so you may use it as a reference if necessary. Know that |
| 48 | +installing suitable packages will probably be challenging. |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +Breaking changes between Python 2 and Python 3 |
| 53 | +---------------------------------------------- |
| 54 | + |
| 55 | +Python 3 differs from Python 2 in several ways. We list the most relevant ones for |
| 56 | +scientific users below. |
| 57 | + |
| 58 | + |
| 59 | +Print function |
| 60 | +.............. |
| 61 | + |
| 62 | +The most visible change is that ``print`` is not a "statement" anymore but a |
| 63 | +function. |
| 64 | + |
| 65 | +Whereas in Python 2 you could write :: |
| 66 | + |
| 67 | + >>> print 'hello, world' # doctest: +SKIP |
| 68 | + hello, world |
| 69 | + |
| 70 | +in Python 3 you must write |
| 71 | + |
| 72 | + >>> print('hello, world') |
| 73 | + hello, world |
| 74 | + |
| 75 | +By making :func:`print` a function, one can pass arguments such a file identifier where the |
| 76 | +output will be sent. |
| 77 | + |
| 78 | + |
| 79 | +Division |
| 80 | +........ |
| 81 | + |
| 82 | +In Python 2, the division of two integers with a single slash character results in |
| 83 | +floor-based integer division:: |
| 84 | + |
| 85 | + >>> 1/2 # doctest: +SKIP |
| 86 | + 0 |
| 87 | + |
| 88 | +In Python 3, the default behavior is to use real-valued division:: |
| 89 | + |
| 90 | + >>> 1/2 |
| 91 | + 0.5 |
| 92 | + |
| 93 | +Integer division is given by a double slash operator:: |
| 94 | + |
| 95 | + >>> 1//2 |
| 96 | + 0 |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +Some new features in Python 3 |
| 101 | +----------------------------- |
| 102 | + |
| 103 | + |
| 104 | +Changing ``print`` to a function and changing the result of the division operator were only |
| 105 | +two of the motivations for Python 3. An incomplete list of the changes follows (there are |
| 106 | +many more). |
| 107 | + |
| 108 | +- By default, strings are in unicode. Sequence of arbitrary bytes use the type |
| 109 | + ``bytes``. This change leads to heavy porting for applications dealing with text. |
| 110 | + |
| 111 | +- Since Python 3.5 and NumPy 1.10, there is a matrix multiplication operator:: |
| 112 | + |
| 113 | + >>> np.eye(2) @ np.array([3, 4]) |
| 114 | + array([3., 4.]) |
| 115 | + |
| 116 | +- Since Python 3.6, there is a new string formatting method, the `"f-string" |
| 117 | + <https://docs.python.org/3/reference/lexical_analysis.html#f-strings>`_:: |
| 118 | + |
| 119 | + >>> name = 'SciPy' |
| 120 | + >>> print(f"Hello, {name}!") |
| 121 | + Hello, SciPy! |
| 122 | + |
| 123 | +- In Python 2, ``range(N)`` return a list. For large value of N (for a loop iterating many |
| 124 | + times), this implies the creation of a large list in memory even though it is not |
| 125 | + necessary. Python 2 provided the alternative ``xrange``, that you will find in many |
| 126 | + scientific programs. |
| 127 | + |
| 128 | + In Python 3, :func:`range` return a dedicated type and does not allocate the memory for the |
| 129 | + corresponding list. |
| 130 | + |
| 131 | + >>> type(range(8)) |
| 132 | + <class 'range'> |
| 133 | + >>> range(8) |
| 134 | + range(0, 8) |
| 135 | + |
| 136 | + You can transform the output of ``range`` into a list if necessary:: |
| 137 | + |
| 138 | + >>> list(range(8)) |
| 139 | + [0, 1, 2, 3, 4, 5, 6, 7] |
| 140 | + |
0 commit comments