Python2 vs Python3 | Syntax and performance Comparison
Last Updated :
28 Oct, 2019
Python 2.x has been the most popular version for over a decade and a half. But now more and more people are switching to Python 3.x. Python3 is a lot better than Python2 and comes with many additional features. Also, Python 2.x is becoming obsolete this year. So, it is now recommended to start using Python 3.x from now-onwards.
Still in dilemma?
Ever wondered what separates both of them? Let's find this thing out below.
First of all, let us go through this quick comparison through this image, which will give you a fair idea on what to expect.
Print Statement
Python 2.7: Extra pair of parenthesis is not mandatory in this.
Python
print 'Hello and welcome to GeeksForGeeks'
Python 3.x: Extra pair of parenthesis is mandatory.
Python3
print ('Hello and welcome to GeeksForGeeks')
Integer Division
Python 2.7:
The return type of a division (/) operation depends on its operands. If both operands are of type int, floor division is performed and an int is returned. If either operand is a float, a classic division is performed and a float is returned. The // operator is also provided for doing floor division no matter what the operands are.
Python
print 5 / 2
print -5//2
# Output:
# 2
# -3
Python 3.x:
Division (/) always returns a float. To do floor division and get an integer result (discarding any fractional result) you need to use // operator.
Python3
print (-5 / 2)
print (5//2)
# Output:
# -2.5
# 2
Input Function
Python 2.7:
When you use input() function, Python automatically converts the data type based on your input.
Python
val1 = input("Enter any number: ")
val2 = input("Enter any string: ")
type(val1)
type(val2)
raw_input
gets the input as text (i.e. the characters that are typed), but it makes no attempt to translate them to anything else; i.e. it always returns a string.
Python
val1 = raw_input("Enter any number: ")
val2 = raw_input("Enter any string: ")
type(val1)
type(val2)
Python 3.x
In Python3, the input function acts like
raw_input from Python 2.7 and it always returns string type.
Python3
val1 = input("Enter any number: ")
val2 = input("Enter any string: ")
type(val1)
type(val2)
# In order to fix this you need to apply
# float() function when user is prompted for input.
Round Function
Python 2.7: The output always results in a floating point number.
Python
print(round(69.9))
print(round(69.4))
# Output:
# 70.0
# 69.0
Python 3.x: The return results in n digit precision.
Python3
print(round(69.9))
print(round(69.4))
# Output:
# 70
# 69
List Comprehensions
Python 2.7: Refer to the example below, how global variable changes.
Python
num = 7
print (num)
mylist = [num for num in range(100)]
print (num)
# Output:
# 7
# 99
Python 3.x: There is no namespace leak now. This is quite fixed now.
Python3
num = 7
print (num)
mylist = [num for num in range(100)]
print (num)
# Output:
# 7
# 7
Range Function
Python 2.7 :
It has both
range
and
xrange
function. When you need to iterate one object at a time, use xrange and when you need an actual list, use range function. xrange is generally faster & saves memory.
Python
% timeit [i for i in range(1000)]
% timeit [i for i in xrange(1000)]
Python 3.x :
Here range does what xrange does in Python 2.7. xrange doesn't work in Python 3.x.
Python3
% timeit [i for i in range(1000)]
% timeit [i for i in xrange(1000)]
Exception Handling
Python 2.7 : This has a different syntax than Python 3.x.
Python
try:
YoYo
except NameError, error:
print error, "YOU HAVE REACHED FOR AN ERROR"
try:
YoYo
except NameError as error:
print error, "YOU HAVE REACHED AN ERROR, YET AGAIN !"
Python 3.x: 'As' keyword is needed to be included in this.
Python3
try:
YoYo
except NameError as error:
print (error, "THE ERROR HAS ARRIVED !")
List Comprehensions
Python 2.7: Lesser parenthesis than Python 3.x.
Python
[item for item in 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
Python 3.x: Extra pair of parenthesis is needed here.
Python3
[item for item in (1, 2, 3, 4, 5)]
[1, 2, 3, 4, 5]
next() function and .next() method
Python 2.7: Both next() and .next() are used here.
Python
generator = (letter for letter in 'abcdefg')
next(generator)
generator.next()
Python 3.x: Only next() is used here. Using .next() shows an AttributeError.
Python3
generator = (letter for letter in 'abcdefg')
next(generator)
ASCII, Unicode and Byte types
Python 2.7: It has ASCII string type, a separate unicode type, but there is no byte type.
Python
type(unicode('a'))
type(u'a')
type(b'a')
Python 3.x: We have unicode strings, and byte type.
Python3
type(unicode('a'))
# This returns an error
Note: List of Methods & Functions that don't return list anymore in Python 3.x.
In Python2.x -
zip()
map()
filter()
dictionary’s .keys() method
dictionary’s .values() method
dictionary’s .items() method
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read