Guido van Rossum created Python in 1991, he decided to clean up Python 2.x properly, without considering any newer releases to it as well as backward compatibility. The whole Python community was pretty much skeptical when they received Python 3.x. 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 Python3 from Python2. Why was it that difficult? What are the differences? Python 2 vs Python 3?
Let us see some of the key difference between Python 2 and Python 3.
Print : Python 2 vs Python 3
In Python2, print is treated as a statement rather than a function. You don’t need wrap the text in parentheses. But, it was very confusing, as most the actions or work that you do with Python requires arguments to be placed inside the parentheses .In Python3, “print” works like a function, where you wrap the text inside the parentheses. If you don’t use parentheses, there will be a syntax error. Though, many Python2 programmers find this change to be irritating, but it really helps in preventing mistakes as some time if you put parentheses around a comma-separated lists then you would get unexpected outcomes. You can see this difference of print function in the example.
In Python3, syntax error, when executed without parentheses,
Integer Division : Python 2 vs Python 3
In Python2, whenever you type any number without any digit after decimal, it treats that number as integer. This might lead to some unexpected results during division. We will see it with an example.
Here, when we made the division of 3 by 2, we got 1, this is because Python2, assumes that as the programmer entered integer values, the output should be also integer and hence it rounds off the output to the nearest value. If you want, the output as 1.5 then you will have to entered the input in float as shown here.
Thus, in Python2, if you want the output in float then you need to have the input also in float.
Below is the output from Python3, you can see that even though the input is in integer, you get the output in float which very much intuitive for new programmers.
List Comprehension-Loop Variables
In Python2, when a variable that is being iterated over a for-loop and a global variable, both are having same name, the value of the global variable changes whenever there is an iteration, this is highly undesirable. I have shown it here in the example that follows.
Hence, in Python 3.x for-loop variables don’t leak into the global namespace anymore!
In Python2, if you want to store a string as Unicode, you need to mark it with “u”, whereas in Python3, all the strings are stored in Unicode by default. Unicode strings are much more versatile compared to ASCII, which is the default standard for Python2. You can store emojis, letters from foreign languages, roman letters as well as numerals in Unicode.
Syntax for raising an error in Python2 and Python3, are different.
In Python2, to raise an IO Error, you would write
In Python3, to raise an IO Error, you will write
This example, again reminds you that in Python3, you need to pass arguments in the parentheses, which creates less confusion.
Apart from this, there are many other examples of slight differences, in the syntax of Python2 and Python3. Python3.6 works approximately at the same speed as 2.7, though there are some benchmarks which suggests that Python3 is faster compared to Python2.
Here, we have tried to mention all key points and important topics about Python 2 vs Python 3.
Other Important Topics:
- Python and Other Object-Oriented Programming Languages
- Python 2 vs Python 3
- Introduction to Python Programming
- The Basic Elements of Python Programming
- Branching, Indentation, Looping and Control Structure in Python
- Indexing & Slicing of Strings and Capturing Inputs
- Built-in Data Types and Functions in Python
- Specifications, Global Variables, Modules and Packages in Python
- Working with Files in Python
- Strings in Python
- Lists in Python
- Tuples in Python
- Dictionaries in Python
- Mutable and Immutable Python Objects
- Functions as Objects, map(), filter() and reduce()
- Exception Handling in Python
- Classes and Object Oriented Programming in Python
- Searching Algorithms in Python
- Method Resolution Order (MRO) in Python
- Sorting Algorithms in Python