In this Tutorial, we will be discussing the difference between Python 2 and Python 3, so here we have some differences between Python 2.0 and 3.0.
In this Tutorial, I would be covering following topics :
- Expression
- Print option
- Not equal operator.
- Range.
- Automated Python 2to3py.
- Performance.
- Some of major Housekeeping changes.
- Having Problems.
So here X is a version just like 2.1 2.2 2.7 and in Python 3 point X which is Python 3.1 to 3.5 3.6 so now
let's see the differences Python 2 and Python 3
1 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.
To Get expression In python 2 :
here what is evaluated expression.
Suppose we have a input in python 2 which is raw input after that some value.
X = raw_input ("enter some values")
To Get evaluated expression In python 3 :
But python 3 we have a input after that some values.
X = input ("enter some values")
Here So whatever we enter then value is assigned variable x, in both python 2 and python 3. So when i enter 2*6 in Python 2 result will be 12, which means you will get evaluated values.
But in Python 3 when run this program 2*6 then we get that result is string values which is like “2*6” in string format.
Then how will get the evaluated value in Python 3, so get evaluated resulted we have to use a expression or we can say function which is eval, when you write eval before the input so it will turn a evaluated value in Python.
Ex:
x= eval(input("enter some values")) = 12
Detailed Expression Examples of Python 2 and Python 3 :
In python 2 :
Input :
name = input("What is your name? ")print ("Hello, %s." %name)
Output :
In python 3 :
Input :
name = input("What is your name? ")print ("Hello, %s." %name)
Output :
So this is main difference in Python 2 and Python 3, which is evaluated expression.
2 Print option
In Python 2 print is statement where as Python 3 print is a function, in Python 2 we don't need to add parenthesis but in Python 3 we need the write the values in parenthesis.
Ex :
Python 2 - print "hello world"
Python 3 - ("hello world")
Python 2
Input :
print "hello world"
Output :
Python 3 :
Input :
1 != 1.0print (False)
Output :
3 Not equal operator
let's move to the third difference, when we use a not equal to operator in python 2 so for that we have to use or greater than and less than sign.
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 a general operator.
Ex :
Python 2 - <> operator is used for not equal
Python 3 - ! operator is used for not equal
In python 2 :
Input :
1 <> 1.0print "False"
Output :
In Python 3 :
Input :
1 != 1.0print (False)1 != 1.0print (False)
Output :
4 Range
let's move to the fourth difference which is range in Python 2 and Python 3.
so what is the range?
Range is used in for loop iterate the values which is a integer. so when we use a range in Python programing to the it will return a list.
so here you can see X equal to Range 10 and when we check the variable X it returned our list type, which means that in Python 2 range is the type of list. when I write X and after that we get a list of object. which is 0 1 2 3 4 5 6 7 8 9.
so now let's move to the 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 returned a range object itself which means that in Python 3 range is a range object itself, so these are the key difference between Python 2 and Python 3.
Detailed Range Examples of Python 2 and Python 3 :
Python 2 :
Input
print range(0,10,1)
Output
Python 3
Input
print(list(range(10)))
Output
5 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 simple program like Add 2 Numbers in python.
Python 2 :
Input
n1 = 1n2 = 2add = float(n1) + float(n2)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 :
n1 = 1n2 = 2add = float(n1) + float(n2)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 so that is all about the difference between the Python 2 and the 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 an manually fix up the problems.
6 Performance :
In Python 3 most of the performance issues have been fixed are almost negligible when comparing current python 3 to python 2 benchmarks.
7 Some of major Housekeeping changes :
Python - 2
- print functional brackets optional.
- Prefix string with u to make unicode string.
- Division of integers always return integer - 5/2=2.
- Raw_input () reads string.
- input() evaluates data read.
- generator .next().
Python - 3
- print functional brackets compulsory.
- String unicode by default.
- Division of integers may result in float - 5/2=2.5.
- Raw_input() not available.
- Input always reads string.
- Next (generator).
- Py2 to py3 utility.
- Dictionary .keys() and .values() returns a view not a list.
- Can no longer use comparison operators on non natural comparisons.
Eg. None < None will raise a TypeError instead of returning false.
- Percent (%) string formatting operator is deprecated use the .format() Function or concatenation.
8 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 python3
I hope you have enjoyed reading this Python Tutorial. We have covered all the basics Difference in Python 2 and Python 3, so you can start practicing now. Happy Learning