By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,491 Members | 1,108 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Take a Closer look at Python 2 vs Python 3

P: 1
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 the following topics:
  1. Expression
  2. Print option
  3. Not equal operator.
  4. Range.
  5. Automated Python 2to3py.
  6. Performance.
  7. Some of major Housekeeping changes.
  8. 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 an expression.

Suppose we have a input in python 2 witch is raw input after that some value.

Expand|Select|Wrap|Line Numbers
  1.     Expand|Select|Wrap|Line Numbers
  •  
  •     
  •  
  • X = raw_input ("enter some values")
  •  
  •     
  •  
  • To Get evaluated expression In python 3 :
  •  
  • But python 3 we have a input after that some values. 
  •  
  •     Expand|Select|Wrap|Line Numbers
  •  
  •     
  •  
  • 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, witch 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:
    Expand|Select|Wrap|Line Numbers
    1.  x= eval(input("enter some values")) = 12 
    Detailed Expression Examples of Python 2 and Python 3 :

    In Python 2 :

    Input :

    Expand|Select|Wrap|Line Numbers
    1. name = input("What is your name? ")
    2. print ("Hello, %s." %name)
    Output :

    Expand|Select|Wrap|Line Numbers
    1. Python 2.7.10 (default, jul 14 2015, 19:46:27)
    2. [GCC 4.8.2] on linux
    3.  
    4. What is your name? vinod
    5. Hello, vinod.
    6.  
    In python 3 :

    Input :

    Expand|Select|Wrap|Line Numbers
    1. name = input("What is your name? ")
    2. print ("Hello, %s." %name)
    Output :

    Expand|Select|Wrap|Line Numbers
    1. Python 3.6.1 (default, Dec 2015, 13:05:11)
    2. [GCC 4.8.2] ON LINUX
    3.  
    4. What isname?name ? Vinod
    5. Hello, Vinod.

    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 :

    Expand|Select|Wrap|Line Numbers
    1. Python 2 - print "hello world"
    2. Python 3 - ("hello world")
    Python 2

    Input :

    Expand|Select|Wrap|Line Numbers
    1. print "hello world"
    Output :

    Expand|Select|Wrap|Line Numbers
    1. Python 2.7.10 {default, jul 14 2015, 19:46:27)
    2. [GCC 4.8.2] on linux
    3.  
    4. Hello World
    Python 3 :

    Input :

    Expand|Select|Wrap|Line Numbers
    1. 1 != 1.0
    2. print (False)1 != 1.0
    3. print (False)
    Output :

    Expand|Select|Wrap|Line Numbers
    1. Python 3.6.1 (default, Dec 2015, 13:05:11)
    2. [GCC 4.8.2] on Linux
    3.  
    4. False

    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.

    Expand|Select|Wrap|Line Numbers
    1. Ex :
    2. Python 2 - <> operator is used for not equal
    3. Python 3 -  !  operator is used for not equal
    4.  
    In python 2 :

    Input :

    Expand|Select|Wrap|Line Numbers
    1. 1 <> 1.0
    2. print "False"
    Output :

    Expand|Select|Wrap|Line Numbers
    1. Python 3.6.1 (default, Dec 2015, 13:05:11)
    2. [GCC 4.8.2] ON LINUX
    3.  
    4. False
    In Python 3 :

    Input :

    Expand|Select|Wrap|Line Numbers
    1. 1 != 1.0
    2. print (False)1 != 1.0
    3. print (False)
    4.  
    Output :

    Expand|Select|Wrap|Line Numbers
    1. Python 3.6.1 (default, Dec 2015, 13:05:11)
    2. [GCC 4.8.2] on linux
    3.  
    4. False
    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

    Expand|Select|Wrap|Line Numbers
    1. print range(0,10,1)
    Output

    Expand|Select|Wrap|Line Numbers
    1. Python 2.7.10 (default, jul 14 2015, 19:46:27)
    2. [GCC 4.8.2] on linux
    3.  
    4. [0,1,2,3,4,5,6,7,8,9]

    Python 3

    Input

    Expand|Select|Wrap|Line Numbers
    1. (list(range(10))
    )

    Output

    Expand|Select|Wrap|Line Numbers
    1. Python 3.6.1 (default, Dec 2015, 13:05:11)
    2. [GCC 4.8.2] on linux
    3.  
    4. =>[0,1,2,3,4,5,6,7,8,9]

    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 Two Numbers in python.

    Python 2 :

    Input

    Expand|Select|Wrap|Line Numbers
    1. num1 = 1
    2. num2 = 2
    3.  
    4. # Add two numbers
    5. sum = float(num1) + float(num2)
    6.  
    7. # Display the sum
    8. print 'The sum of {0} and {1} is {2}'.format(num1, num2, sum)
    Output


    Expand|Select|Wrap|Line Numbers
    1. Python 2.7.10 (default, Jul 14 2015, 19:46:27)
    2. [GCC 4.8.2] ON LINUX
    3.  
    4. The sum of 1 and 2 is 3.0
    Now Using 2 to 3 Translation We can convert the above Code

    Input :

    Expand|Select|Wrap|Line Numbers
    1. num1 = 1
    2. num2 = 2
    3.  
    4. # Add two numbers
    5. sum = float(num1) + float(num2)
    6.  
    7. # Display the sum
    8. print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
    Expand|Select|Wrap|Line Numbers
    1. Output :
    Expand|Select|Wrap|Line Numbers
    1. Python 3.6.1 (default, Dec 2015, 13:05:11)
    2. [GCC 4.8.2] On linux
    3.  
    4. =>[0,1,2,3,4,5,6,7,8,9]
    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 the of major Housekeeping changes :

    Python - 2
    • print functional brackets optional.
    • Prefix string Unicode 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 practising now. Happy Learning.
    1 Week Ago #1
    Share this Article
    Share on Google+