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.
print 'Hello and welcome to GeeksForGeeks'
|
Python 3.x: Extra pair of parenthesis is mandatory.
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 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.
print (-5 / 2)
print (5//2)
|
Input Function
Python 2.7:
When you use input() function, Python automatically converts the data type based on your input.
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.
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.
val1 = input("Enter any number: ")
val2 = input("Enter any string: ")
type(val1)
type(val2)
|
Round Function
Python 2.7: The output always results in a floating point number.
print(round(69.9))
print(round(69.4))
|
Python 3.x: The return results in n digit precision.
print(round(69.9))
print(round(69.4))
|
List Comprehensions
Python 2.7: Refer to the example below, how global variable changes.
num = 7
print (num)
mylist = [num for num in range(100)]
print (num)
|
Python 3.x: There is no namespace leak now. This is quite fixed now.
num = 7
print (num)
mylist = [num for num in range(100)]
print (num)
|
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.
% 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.
% 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.
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.
try:
YoYo
except NameError as error:
print (error, "THE ERROR HAS ARRIVED !")
|
List Comprehensions
Python 2.7: Lesser parenthesis than Python 3.x.
[item for item in 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
|
Python 3.x: Extra pair of parenthesis is needed here.
[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.
generator = (letter for letter in 'abcdefg')
next(generator)
generator.next()
|
Python 3.x: Only next() is used here. Using .next() shows an AttributeError.
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.
type(unicode('a'))
type(u'a')
type(b'a')
|
Python 3.x: We have unicode strings, and byte type.
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
Python2 vs Python3 | Syntax and performance Comparison
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
4 min read
Tips to Improve the Performance of Python Application
Python...We all know the popularity of this language. Python is a powerful higher-order programming language. In today's tech world, it's almost everywhere. Whether you're building a web application or working with machine learning, this language has become the first choice for developers. When it c
6 min read
Python | Set 4 (Dictionary, Keywords in Python)
In the previous two articles (Set 2 and Set 3), we discussed the basics of python. In this article, we will learn more about python and feel the power of python. Dictionary in Python In python, the dictionary is similar to hash or maps in other languages. It consists of key-value pairs. The value ca
5 min read
Python Object Comparison : "is" vs "=="
Both "is" and "==" are used for object comparison in Python. The operator "==" compares values of two objects, while "is" checks if two objects are same (In other words two references to same object). # Python program to demonstrate working of # "==" # Two different objects having same val
2 min read
Chaining comparison operators in Python
Checking more than two conditions is very common in Programming Languages. Let's say we want to check the below condition: a < b < c The most common syntax to do it is as follows: if a < b and b < c : {...} In Python, there is a better way to write this using the Comparison operator Chai
5 min read
10 Tips to Maximize Your Python Code Performance in 2024
Python is a powerful programming language that has been gaining popularity in recent years. It's known for its versatility and ease of use, making it a great choice for beginners and experienced programmers alike. However, with so many resources available on the internet, it can be difficult to know
15+ min read
Python | Set 2 (Variables, Expressions, Conditions and Functions)
Introduction to Python has been dealt with in this article. Now, let us begin with learning python. Running your First Code in Python Python programs are not compiled, rather they are interpreted. Now, let us move to writing python code and running it. Please make sure that python is installed on th
3 min read
Comparing Old-Style and New-Style Classes in Python
In Python, the difference between old-style and new-style classes is based on the inheritance from the built-in object class. This distinction was introduced in Python 2.x and was fully adopted in Python 3.x, where all classes are new-style classes. In this article, we will see the difference betwee
4 min read
Choosing the Right IDE for Python Development: A Comparison
Python, being one of the most popular programming languages, has a rich ecosystem of development environments. Selecting the right Integrated Development Environment (IDE) for Python development can greatly enhance your productivity, streamline coding workflows, and improve debugging processes. Diff
5 min read
Primary and secondary prompt in Python
The ">>>" sign seen on the Python's interactive shell is called a prompt. Python's interactive shell, which appears after firing command "python3" (if we are working on python version 3) on the terminal, has two such prompts. ">>>" - Primary prompt "..." - Secondary prompt When we
2 min read
Upgrading from Python2 to Python3 on MacOS
Python is a high level and general-purpose programming language. It is widely used in a variety of fields such that it fulfills the need of developers who are in the front end, backend, or both. It is being used rigorously in Machine Learning, Artificial Intelligence, Web Development, and in various
4 min read
Python Automation Tutorial: Beginner to Advanced
Python is a very powerful programming language and it's expanding quickly because of its ease of use and straightforward syntax. In this Python Automation Tutorial, we will explore various techniques and libraries in Python to automate repetitive tasks. Automation can save you time and reduce error
10 min read
Introduction To Machine Learning using Python
Machine learning has revolutionized the way we approach data-driven problems, enabling computers to learn from data and make predictions or decisions without explicit programming. Python, with its rich ecosystem of libraries and tools, has become the de facto language for implementing machine learni
6 min read
Comparison of Python with Other Programming Languages
Python is an easily adaptable programming language that offers a lot of features. Its concise syntax and open-source nature promote readability and implementation of programs which makes it the fastest-growing programming language in current times. Python has various other advantages which give it a
3 min read
Important differences between Python 2.x and Python 3.x with examples
In this article, we will see some important differences between Python 2.x and Python 3.x with the help of some examples. Differences between Python 2.x and Python 3.x Here, we will see the differences in the following libraries and modules: Division operatorprint functionUnicodexrangeError Handling
5 min read
Fix: "SyntaxError: Missing Parentheses in Call to 'print'" in Python
In Python, if we see the "SyntaxError: Missing Parentheses in Call to 'print'" error, it means we forgot to use parentheses around what we want to print. This is the most common error that we encounter when transitioning from Python 2 to Python 3. This error occurs due to a significant change in how
3 min read
Java vs .NET vs Python: Choosing the Best
The process of selecting the right programming language for development can appear to be an intimidating task in light of the numerous options available in today’s technology landscape. Some of the most common choices include .NET, Java, and Python each having their own strengths and weaknesses. Det
3 min read
Map vs List comprehension - Python
The map() function is used to apply a given function to every item of an iterable, such as a list or tuple and returns a map object (which is an iterator). Whereas, List comprehension is a way to create lists using a concise syntax. It allows us to generate a new list by applying an expression to ea
3 min read
Python Tokens and Character Sets
Python is a general-purpose, high-level programming language. It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code, and these codes are known as scripts. These scripts contain character sets, tokens, and identifiers.
6 min read