Key Differences Between Python 2 And Python 3 With Examples

Introduction

 
In this tutorial, we will be discussing the differences between Python 2.0 and Python 3.0.
 
In this tutorial, I will be covering the following topics:
  • Expression
  • Print option
  • Not equal operator
  • Range
  • Automated Python 2to3py
  • Performance.
  • Some of the major housekeeping changes.
  • Dealing with problems.

Expression

 
Expressions represent something, like a number, a string, or an instance of a class. Any value is an expression! Anything that does something is a statement. Any assignment to a variable or function call is a statement. Any value contained in that statement in an expression.
 

Get expression in Python 2

 
Here, what has evaluated the expression?
 
Suppose we have an input in Python 2 that is a raw input after some value.
  1. X = raw_input ("enter some values")  

Get evaluated expression In Python 3

 
But, in Python 3 we have an input after some values.
  1. X = input ("enter some values")  
So whatever we enter, a value is assigned to variable x, in both Python 2 and Python 3. So, when I enter 2*6 in Python 2 the result will be 12, which means you will be getting evaluated values.
 
But in Python 3 we will be running this program 2*6 then we will get that result in string values which are like “2*6” in string format.
 
So how will you get the evaluated value in Python 3? To get the evaluated resulted we have to use an expression, or we can say function, eval. When you write eval before the input it will turn into an evaluated value in Python.
 
For example
  1. x= eval(input("enter some values")) = 12  

Detailed Expression Examples of Python 2 and Python 3

 
In Python 2
 
Input
  1. name = input("What is your name? ")  
  2. print ("Hello, %s." %name)  
 Output
  1. Input : https://mindmajix.com
  2. Output : Hello, https://mindmajix.com
In Python 3
 
Input
  1. name = input("What is your name? ")  
  2. print ("Hello, %s." %name)  
Output
 
 
So, this is the main difference between Python 2 and Python 3, which is evaluated as an expression.
 

Print option

 
In Python 2 print is a statement whereas in Python 3 print is a function. In Python 2 we don't need to add parentheses but in Python 3 we need to write the values in parentheses.
 
Ex
  1. Python 2 - print "hello world"
  2. Python 3 - ("hello world")
Python 2
 
Input
  1. print "hello world"  
Output
 
 
Python 3
 
Input
  1. 1 != 1.0  
  2. print (False)  
Output
 
 

Not equal operator

 
Let's move to the third difference when we use a not equal operator in Python 2. For that, we have to use greater than and less than signs.
 
But in Python 3 there is a general operator which is exclamation marks and equal to, so this operator is used in other languages. That's why I call it a general operator.
 
Example
  1. Python 2 - <> operator is used for not equal
  2. Python 3 - ! an operator is used for not equal
In Python 2
 
Input
  1. 1 <> 1.0  
  2. print "False"  
Output
 
 
In Python 3
 
Input
  1. 1 != 1.0  
  2. print (False)1 != 1.0  
  3. print (False)  
Output
 
 

Range

 
Let's move to the fourth difference, which is a range in Python 2 and Python 3.
 

So what is the range?

 
A range is used in for loop to iterate the values which are an integer. So, when we use a range in Python programming it will return a list.
 
 
So, here you can see X is equal to Range 10 and when we check the variable X it returns our list type, which means that in Python 2 range is a type of list. When I write X,  we get a list of objects, which is 0 1 2 3 4 5 6 7 8 9.
 
 
So, now let's move to Python 3 when we write x equal to range 5,
 
So, this value of range 5 is assigned to the variable X and when we check the type for variable X then it returns a range object itself which means that in Python 3 range is a range object itself, so these are the key differences between Python 2 and Python 3.
 

Detailed Range Examples of Python 2 and Python 3

 
Python 2
 
Input
  1. print range(0,10,1)  
Output
 
 
Python 3
 
Input
  1. print(list(range(10)))  
Output
 
 

Automated Python 2to3py

 
Now, let's move to the topic which is how to automate the Python 2 script into Python 3 code.
 
Here, we can Test with a simple program like Add 2 Numbers in Python.
 
Python 2
 
Input
  1. n1 = 1  
  2. n2 = 2  
  3. add = float(n1) + float(n2)  
  4. print 'sum of {0} and {1} is {2}'.format(n1, n2, add)   
Output
 
Now, using 2 to 3 Translation we can convert the above code.
 
Input 
  1. n1 = 1  
  2. n2 = 2  
  3. add = float(n1) + float(n2)  
  4. print('sum of {0} and {1} is {2}'.format(n1, n2, add))  
Output
 
So, here we see it can be converted to Python 3 x code by 2 to 3 on the command line, so this is the way to convert a Python 2 program into Python 3.
 
Python provides its own tool called 2to3.py which runs a bunch of scripts to translate your Python 2 code into 3.
  • Not perfect, but it does an amazing job.
  • After converting, go in and manually fix the problems.

Performance

 
In Python 3 most of the performance issues that have been fixed are almost negligible when comparing the current Python 3 to Python 2 benchmarks.
 
Some of the major housekeeping changes are-
 

Python  2

  • Print functional brackets are optional.
  • Prefix string with u to make Unicode string.
  • Division of integers always returns integer - 5/2=2.
  • Raw_input () reads string.
  • input() evaluates data read.
  • generator .next().

Python  3

  • Print functional brackets are compulsory.
  • String Unicode by default.
  • Division of integers may result in the float - 5/2=2.5.
  • Raw_input() not available.
  • Input always reads the string.
  • Next (generator).
  • Py2 to a py3 utility.
  • Dictionary .keys() and .values() returns a view not a list.
  • Can no longer use comparison operators on non natural comparisons.
For example,  None < None will raise a TypeError instead of returning false.
  • Percent (%) string formatting operator is deprecated use the .format() Function or concatenation.
Having Problems
  • You may encounter an error here and there if you have been working in Python 2.x for some time.
  • Just Google the problem, it's almost certain that someone has had that problem too when moving to Python 3.

Summary

 
I hope you have enjoyed reading this Python Tutorial. We have covered all the basic differences between Python 2 and Python 3, so you can start practicing now. Happy learning.


Recommended Free Ebook
Similar Articles