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
10 Tips to Maximize Your Python Code Performance
Ever written Python code that feels... slow? Or maybe youâve inherited a codebase that takes forever to run? Donât worry youâre not alone. Python is loved for its simplicity, but as your project grows, it can start to lag.The good news? You donât need to switch languages or sacrifice readability to
13 min read
Chaining comparison operators in Python
Checking multiple conditions in a single expression is common in programming. In Python, comparison operator chaining allows us to write cleaner, more readable code when evaluating multiple conditions. Instead of using multiple and conditions, Python enables chaining comparisons directly in a mathem
4 min read
Comparison Operators in Python
Python operators can be used with various data types, including numbers, strings, boolean and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters
4 min read
Python Object Comparison : "is" vs "=="
In Python, both is and == are used for comparison, but they serve different purposes:== (Equality Operator) â Compares values of two objects.is (Identity Operator) â Compares memory location of two objects.Pythona = [1,2,3] b = [1,2,3] print(a == b) print(a is b) OutputTrue False Explanation: a and
2 min read
Python := Walrus Operator in Python 3.8
The Walrus Operator is a new addition to Python 3.8 and higher. In this article, we're going to discuss the Walrus operator and explain it with an example. Walrus Operator allows you to assign a value to a variable within an expression. This can be useful when you need to use a value multiple times
2 min read
Python | Timing and Profiling the program
Problems - To find where the program spends its time and make timing measurements. To simply time the whole program, itâs usually easy enough to use something like the Unix time command as shown below. Code #1 : Command to time the whole program Python3 1== bash % time python3 someprogram.py real 0m
3 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
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
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
Python | sympy.compare() method
With the help of sympy.compare() method, we can compare the variables and it will return 3 values i.e -1 for smaller, 0 for equal and 1 for greater by using sympy.compare() method. Syntax : sympy.compare() Return : Return the value of comparison i.e -1, 0, 1. Example #1 : In this example we can see
1 min read