Differences Between Python 2 and 3

In: Python

3 Mar 2012

Which Python to choose? Python 2.7 series ,scheduled to be the last major version in the 2.x series before 2.x moves into an extended maintenance period. Python 3, the new backwards-incompatible series of Python. Python 2 is currently the most used version of Python but usage of Python 3 is increasing fast. If you have implemented Python 2 in your project, you can always transition it to Python 3 using utility script 2to3 which comes in Python 3. It auto converts Python 2 source code into Python 3 as much as it can. Still, you should know some of the key differences between Python 2 and 3.
Python 2 or 3
print Statement
In Python 2 print is a statement whereas in Python 3 print() is a function.

Python 2 Python 3
print print()
print 1, 2, print(1, 2, end=”)

To print a blank line, call print() without any arguments. TO print a single value call print() with a single argument and so on.

long Datatype
Python 2 has separate int and long types for non floating-point numbers. Python 3 has only one integer type int which behaves like the long type in Python 2.

Python 2 Python 3
x = 1000000000000L X = 1000000000000
long(x) int(x)
type(x) is long type(x) is int
isinstance(x,long) isinstance(x,int)

To obtain a variable to an integer, use int() function in Python 3. You can check datatypes using int not long to check for integers.

raw_input() and input() Global Functions
Python 2 has two global functions for asking the user for input. One called input(), expected the user to enter a Python expression and return the result. Second called raw_input() which just returned whatever the user has typed.
Python 3 has renames raw_input() to input()

Python 2 Python 3
raw_input() input()
input() eval(input())

Formatting strings with the format() Method and %s
In both Python 2 and 3, you can include %s inside a string and follow it with a list of values for each %s such as:

>>>'My name is %s and I am a %s.' % ('Hrishikesh', 'programmer')
'My name is Hrishikesh and I am a programmer.'

Python 3 adds a new string method called format().

>>>'My name is {0} and I am a {1}.'.format('Hrishikesh','programmer')
My name is Hrishikesh and I am a programmer.

>>>'My name is {0} {0} and I am a {1} - {0}'.format('Hrishikesh', 'programmer')
My name is Hrishikesh Hrishikesh and I am a programmer - Hrishikesh

xrange Global Function
In Python 2 there are two ways to get a range of numbers: range(), which returns a list and xrange() which returns an iterator. Python 3 has only range().

Python 2 Python 3
xrange(10) range(10)
some_list = range(10) some_list = list(range(10))
[i for i in xrange(10)] [i for i in range(10)]

try expect Statement
The syntax for catching exceptions is slightly different in Python 2 and Python 3.
In Python 2:

     import module
except ImportError, e

The same in Python 3:

     import module
except ImportError as e

has_key() Dictionary Method
Python 2 has a has_key() method to test if a dictionary had a certain key. Python 3 uses in operator.

Python 2 Python 3
a_dictionary.has_key(‘CollegeWires’) ‘CollegeWires’ in a_dictionary
a_dictionary.has_key(x or y) (x or y) in a_dictionary

And the list goes on. I have mentioned the key differences between Python 2 and 3. However for a further broad reading you should refer the Python Documentation.

About the author

Hrishikesh By Hrishikesh Kumar. An undergrad at IIT Guwahati, India. A programming enthusiast and a learner. He loves his android and builds Android applications in his spare time. Follow me on Google.

Random Post

Subscribe via email:

About this blog

Hrishikesh Kumar

A Programming tutorial blog by Hrishikesh Kumar. More...