OneCompiler

Key differences between python 2.x and python 3.x

715

Many of the beginners would be wondering which version should i start learning?

I would say Python 3.x is the clear winner in 2020 as it's not worthy to spend time on a version which is becoming obsolete. Python3.x was released to fix the problems of Python2.x. Because of these changes, Python3.x is not much compatible with Python2.x and when you are migrating from Python2.x to Python3.x, you will end up doing alot of changes not only to the projects and applications but also in libraries.

It's better to know a little bit of history about Python and understand the key differences between the versions.

1. Print Function

This a well-known syntactical difference. print statement has been replaced with a print () function in Python3.x.

For example,

print "Hello World" # is valid in Python2.x but throws syntactical error in Python3.x
print("Hello World") # valid statement in Python3.x

2. Integer division

Python3.x has improvised integer division. For example, When you perform integer division operation, Outcome differs in both Python2.x and Python 3.x.

Python2.x

print(4/3) # results 1
print(-4/3) # results -2

Python3.x

print(4/3) # results 1.3333333333333333
print(-4/3) # results -1.3333333333333333

3. Unicode

Python3.x supports unicode characters better. Text strings are Unicode by default in Python3.x and the text strings are ASCII by default in Python2.x. You can also store string as unicode in Python 2.x by adding u while storing the strings. Unicode strings are more versatile and can store foreign language letters, numerals, symbols, emojis, etc.,

print(type('string literal')) 
print(type(b'Byte literal')) 
print(type(u'unicode string')) 

Python2.x results

<type 'str'>
<type 'str'>
<type 'unicode'>

Python3.x results

<class 'str'>
<class 'bytes'>
<class 'str'>

Please note Python3.x treats strings and unicode as same, and strings and bytes are treated different. But Python2.x treats strings and bytes as same, and strings and unicode strings are treated different.

4. For-loop variable value gets changed in Python2.x but remains same in Python3.x

For example,

Python2.x

i = 1
print 'before for loop i =', i

for i in range(5):
  print "Inside loop i=",i

print 'after for loop i =', i

Results

before for loop i = 1
Inside loop i= 0
Inside loop i= 1
Inside loop i= 2
Inside loop i= 3
Inside loop i= 4
after for loop i = 4

Python3.x

i = 1
print('before for loop i =', i)

for i in range(5):
  print ("Inside loop i=",i)

print ('after for loop i =', i)

Results

before for loop i = 1
Inside loop i= 0
Inside loop i= 1
Inside loop i= 2
Inside loop i= 3
Inside loop i= 4
after for loop i = 4

5. Other notable differences are:

  • Python3.x syntax is relatively simpler and easily understandable when compared to Python2.x
  • xrange() is very popular in Python 2.x for creating an iterable object but doesn’t exist in Python 3.x. range() function is used to create iterable objects in Python3.x
  • Exceptions should be enclosed in parenthesis in Python3.x where as exceptions should be enclosed in notations in Python2.x. For example, raise IOError, "file error"is used in Python2.x but raise IOError("file error") in Python3.x