Difference between Python 2 and Python 3Programming languages constantly evolve as developers extend the functionality of the language and iron out quirks that cause problems for developers. If you are new to Python language , you might be confused about the different versions that are available. Python 1.x refers to the first set of releases of the Python. This was long ago and nobody uses or cares about these anymore. Python 2.x refers the second line of releases of Python. Python Python 2.7.17 (19 October 2019) is the latest release in this line of releases. Python 3.x refers to the third major line of releases of Python. Python Python 3.8.2 (24 February 2020) is the latest release in this line of releases.
Backward CompatibilityPython has a rule that all 2.x versions will be backward compatible . The same rule applies to Python 3.x versions. However, Python does not guarantee backward compatibility between versions. Python 3 introduced several changes to the actual structure and syntax of the Python language. The whole Python community was pretty much sceptical when they received Python 3.x. Python 3.0 is fundamentally different to previous Python releases because it is the first Python release that is not compatible with older versions . Most of the things written in Python 2.x were not compatible with Python 3.x, as it did not support backward compatibility. Many applications and frameworks needed to rewritten completely due to this, hence, it was very difficult to port to Python 3.x from Python 2.x . Programmers who first learned to program in Python 2.x sometimes find the new changes difficult to adjust to, but newcomers often find that the new version of the language makes more sense.
Python 2.x Vs. Python 3.x
- specifically Python 2.7 vs 3.5
- Using the __future__ module
- The print function
- Integer Division
- List Comprehension Loop Variables
- xrange function
- The next() function and .next() method
- Raising Exceptions
- Handling exceptions
- For-loop variables and the global namespace leak
- Parsing user inputs via input()
- Returning iterable objects instead of lists
- Comparing unorderable types
Using the __future__ moduleThe __future__ statement forces Python interpreters to use newer features of the language. It is a directive to the compiler that a particular module should be compiled using syntax or semantics that will be available in a specified future release of Python . The idea of __future__ module is to help migrate to Python 3.x. If you are planning to have Python 3.x support in you 2.x code, you can use _future_ imports in our code. If you actually want to import the __future__ module, just do:
import __future__For example:
>>> from __future__ import print_functionwill allow you to use print as a function:
>>> print('Hello, World!')__future__ statements need to be near the top of the file because they change fundamental things about the language, and so the compiler needs to know about them from the beginning.
The print functionIn Python 2, "print" is treated as a statement rather than a function . However, parentheses work in Python 2.x if a space is added after print keyword because the interpreter evaluates it as an expression. But in Python 3.x it is compulsory to use parenthesis otherwise it will raise error .
- Python 2.x supports both syntaxes , print and print()
- Python 3.x supports only print()
print 'Hello, World' # Python 3.x doesn't support print('Hello, World')
Integer DivisionIn python 2, if you perform division on two integers then the output will be an integer too. But in python 3, output will be accurate, so the result can be in float too. In Python 2.x, division defaults to integer division if both operands are integer:
>>> 5 / 2 2 >>> 3 / 2.0 # of course it "works" if one or both are float 2.5
In Python 3.x:
>>> 5 / 2 2.5If you want integer division in Python 3.x, you can use the floor division operator (//), which has been around since long before Python 3:
>>> 5 / 2 2.5 >>> 5 // 2 1
List ComprehensionsPython 2.x
For List Comprehensions, lesser parenthesis than Python 3.x.
[item for item in 1, 2, 3, 4, 5] [1, 2, 3, 4, 5]Python 3.x
You need to use extra pair of parenthesis in Python 3.x.
[item for item in (1, 2, 3, 4, 5)] [1, 2, 3, 4, 5]
In Python 2.x strings are stored internally as 8-bit ASCII — a method of encoding English characters with an assigned number while in Python 3.x implicit str type is Unicode . This is important because Unicode is more versatile than ASCII. While functional, ASCII remains 7-bit, meaning it does not encompass a broad range of symbols. Unicode ( Unicode Transformation Format – 8-bit ) strings can store foreign language letters, Roman letters and numerals, symbols, emoji, etc., offering you more choices. You can still label your Unicode strings prefix with a "u" if you want to make sure your Python 3 code is compatible with Python 2 .