A little history of Python 2 vs 3
Let’s start off by considering a quick summary of the usage of Python 2 in comparison to Python 3.
- Python 2.0 was first released in 2000. Its latest version, 2.7, was released in 2010.
- Python 3.0 was released in 2008. Its newest version, 3.6, was released in 2016, and version 3.7 is currently in development.
- Although Python 2.7 is still widely used, Python 3 adoption is growing quickly. In 2016, 71.9% of projects used Python 2.7, but by 2017, it had fallen to 63.7%. This signals that the programming community is turning to Python 3–albeit gradually–when developing real-world applications.
- Notably, on January 1, 2018, Python 2.7 will “retire” and no longer be maintained. (The clock is literally ticking!)
Subsequently, we will look at the divergences between Python 2 and Python 3 in 2018.
What are the main Python 2 vs 3 2018 differences?
The following are five key contrasts between various versions of the Python programming language:
1. Python 2 is legacy, Python 3 is the future.
For over a decade and a half, Python 2 has been the most well-used version, so there are still instances where it is embedded in the programs of certain businesses.
However, as more companies transition from Python 2 to 3, those wanting to learn Python programming for starters may decide against committing time to a version that is fading out.
2. Python 2 and Python 3 have different (sometimes incompatible) libraries
Many developers today are constructing libraries that only work with Python 3 as it is the upcoming standard.
In a similar manner, many libraries created for Python 2 do not work with future versions.
It is possible to transfer a 2.x library to 3.x; however, it is an advanced task which requires skill and knowledge. It is not suitable for those who are just starting out with Python.
3. There is better Unicode support in Python 3
In Python 3, text strings are Unicode by default. In Python 2, strings are stored as ASCII by default; if you want to store them as Unicode, you must add a “u”.
The significance of this lies in the fact that Unicode is more adaptive than ASCII. Unicode strings allow you a wide array of choices, such as characters from other languages, Roman letters and numbers, symbols, and even emojis.
4. Python 3 has improved integer division
In Python 2, if you incorporate a number with no decimals, the computation will be rounded to the closest complete number.
For illustration, if you’re doing the calculation of 5 divided by 2, and you enter 5 / 2, the answer will be 2 obliged to rounding. To get the precise answer of 2.5, you must write it out as 5.0 divided by 2.0.
In Python 3, dividing 5 by 2 yields the accurate answer of 2.5 without the need to add any extra zeroes.
This serves as an illustration of how the syntax of Python 3 can be more user-friendly, thereby facilitating the learning process of programming in Python for beginners.
5. The two versions have different print statement syntaxes
It is merely a variation in syntax, which could be thought of as insignificant, so it does not interfere with the working of Python. You ought to be aware that it is still a substantial and easily noticeable difference.
Basically, Python 3 changed the print statement to a print() function.
In Python 2, one would type “print “hello”” to achieve the same effect, whereas in Python 3, you would use “print (“hello”)”.
If you are just starting to learn Python programming, it should not be too challenging. If you are accustomed to Python 2, the transition to a new Python version can be difficult.
Why companies are moving from Python 2 to 3
Previously it was mentioned that many companies still utilize Python 2 for historical reasons, but increasingly individuals and organizations are starting to adopt or convert to Python 3.
Let’s investigate why Instagram and Facebook have decided to migrate from Python 2 to Python 3, or are in the midst of doing so.
In 2017, Instagram changed a lot of their code written in Python 2.7 to Python 3.
Why they use it:
- Python is not traditionally a typed language, but Python v3.5 supports typing, which removes development conflicts when working new pieces of code.
- Each newer version of Python continues to get faster runtime. Meanwhile, nobody’s currently working to make Python 2.7 work faster.
- Community support is better with Python 3.
Facebook is currently changing over their back-end structure and management system from Python 2 to Python 3.4.
Why they use it:
- According to RealPython, “The ease of using Python libraries means that the production engineers don’t have to write or maintain as much code, allowing them to focus on getting improvements live. It also ensures that the infrastructure of Facebook is able to scale efficiently.”
- Watch this talk on YouTube for more information about the changing culture of Python at Facebook.
Why are there different Python versions?
Let’s look at a few pieces of code to clarify the contrasts between Python 2 and 3!
The new print() function
In Python 3, the print statement found in Python 2 has been replaced by the print() function, which requires us to put the item we wish to print within parentheses.
Python 2:
>>> print 'Hello DataCamp' Hello DataCamp
Python3:
>>> print('Hello Datacamp') Hello Datacamp
Integer division
When using Python 2, a division of two integers would result in an integer answer, not a fraction. In Python3, the result of integer division will always include decimal points, thereby making it more comprehendible.
Python 2:
>>> 5/2 2
Python 3:
>>> 5/2 2.5
Unicode support
In Python 2, all Unicode strings have to have the “u” prefix since the system uses ASCII characters as the standard. Unlike Python 2, Python 3 defaults to saving strings in Unicode format, which is more flexible than ASCII strings.
Python 2:
>>> print type("Hello DataCamp!") # this is an ASCII string <type 'str'> >>> print type(u"Hello DataCamp!") # this is a Unicode string <type 'unicode'>
Python 3:
>>> print(type("Hello DataCamp!")) # this is a Unicode string <class 'str'>
Range function
In Python 3, the xrange() function which was present in Python 2 no longer exists. The range() function has taken the place of the old method, providing enhanced speed when cycling through sequences.
Python 2:
>>> xrange(1,10) xrange(1, 10) >>> type(xrange(1,10)) <type 'xrange'>
Python 3:
>>> range(1,10) range(1, 10) >>> type(range(1,10)) <class 'range'>
Leave a Reply