What are the key differences between Python 2.7.x and Python 3.x?

PythonServer Side ProgrammingProgramming

Python 3.0 was released in Dec. 2008. It was designed to rectify certain flaws in earlier version. The guiding principle of Python 3 was: "reduce feature duplication by removing old ways of doing things". Python 3.0 doesn’t provide backward compatibility. That means a Python program written using version 2.x syntax doesn’t execute under python 3.x interpreter. Ver 2.7 is the final major release in Python 2.x series.

Although there are quite a few differences in usages of these two versions, the most obvious ones are mentioned below −

print is a keyword in Python 2.7 but has been included as built-in function in Python 3.x. As a result parentheses are mandatory while using it in Python 3 code

print “Hello World” # is acceptable in Python 2 but not in Python 3
print (“Hello World”) #acceptable in Python 2 and Python 3

raw_input() − function from Python 2.7 has been deprecated. The input() function treats the received data as string only.

Integer division − functionality has been changed in Python 3. In Python 2.x, 5/2 results in 2, but in Python 3.x, 5/2 is 2.5

UNICODE − In Python 3.x a string is Unicode by default. In Python 2.x, string has to be explicitly defined as Unicode by prefixing it with ‘u’ ( e.g. u’hello’)

Long integer − In Python 3.x, integer objects are long by default. In Python 2.x, an integer has to be postfixed by L (e.g. 100L)

Published on 22-Dec-2017 10:52:45