Updated: Aug 18, 2020
As we have covered introduction of python in last article ,now we will learn differences of python 2 and 3.
Differences Between Python 2.x And Python 3.x
Why are there different versions of Python?
Programming languages constantly evolve as developers extend the functionality of the language and iron out quirks that cause problems for developers. Python 3 was introduced in 2008 with the aim of making Python easier to use and change the way it handles strings to match the demands placed on the language today. Programmers who first learned to program in Python 2 sometimes find the new changes difficult to adjust to, but newcomers often find that the new version of the language makes more sense.
Difference between Python 2 and 3
Here you will know about the difference between python 2 and 3.
Please note that Python 2 is now end of life ( EOL) so this course will completely focus on Python 3 but just for the knowledge purpose, we will discuss the differences between two versions.
1. Integer Division:
In python 2 if we 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. Still want the result in integer, then you can use print(9//2) it return an integer result.
2. Print Function:
In python 2 parenthesis '( )' aren’t compulsory to use we can print anything without using parenthesis but in Python 3 it is compulsory to use parenthesis otherwise it will raise an error.
In Python 2, the implicit str(string) type is ASCII, but in Python 3 implicit str type is Unicode
4. xrange function:
xrange() of Python 2.x doesn’t exist in Python 3.x. In Python 2.x, range returns a list i.e. range(3) returns [0, 1, 2] while xrange returns a xrange object i. e., xrange(3) returns iterator object which works similar to Java iterator and generates number when needed. range in python 3 does the same thing that xrange did in Python 2
# Program in python 3 for i in range(1,10): print(i) # Program in python 2 for i in xrange(1,10): print i
1 2 3 4 5 6 7 8 9
5. Raising exception:
As we’ve seen the difference between print function, raising an exception will follow the same syntax i.e. a parenthesis is mandatory in Python 3
In python 2, its
raise IOError, "file not found"
In python 3, its
raise IOError("file not found")
6. Handling exception:
there is a minor change in syntax, we’ve to use a keyword in python 3.
In python 2, its
except IOError, err:
In python 3, its
except IOError as err:
7. Leak of for-loop variables in the global namespace:
In python 2 there was a problem that the value of a global variable had changed while using that variable in for-loop.
But in Python 3, for loop variables don’t leak into the global namespace anymore!
8. .next() method:
Python have .next() method and next() function to fetch the next element of an iterator.
But in python 3 .next() method is no more. We have to use only the next() function to iterate the next element of an iterator.
Python 2 have input() and raw_input() methods for taking input. The difference between them raw_input() returns a string, and input() tries to run the input as a python expression.
Mostly all we want the string as input then we convert it into any datatype as we want.
In Python 3 there is no raw_input() method. The raw_input() method is replaced by input() in python 3. If you still want to use the input() method like in python 2 then we can use eval() method.
it will work as same as input() in python 2.
10. The __future__ Module:
As we know there are many things that python 3 supports but python 2 don’t. if you’re planning python 3 support for your code then use the __future__ module.
Let’s say we want python 3’s integer division behavior in our python 2 program then our program will look like
from __future__ import division print 9/2
without __future__ module
Next, we will see the installation of Python 3