Take a Closer look at Python 2 vs Python 3

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinodkasipuri
    New Member
    • Mar 2019
    • 1

    Take a Closer look at Python 2 vs Python 3

    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.

    Code:
    [CODE]X = raw_input ("enter some values")
    To Get evaluated expression In python 3 :

    But python 3 we have a input after that some values.

    Code:
    X = input ("enter some values")
    [/CODE]



    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:
    Code:
     x= eval(input("enter some values")) = 12
    Detailed Expression Examples of Python 2 and Python 3 :

    In Python 2 :

    Input :

    Code:
    name = input("What is your name? ")
    print ("Hello, %s." %name)
    Output :

    Code:
    Python 2.7.10 (default, jul 14 2015, 19:46:27)
    [GCC 4.8.2] on linux
    
    What is your name? vinod
    Hello, vinod.
    In python 3 :

    Input :

    Code:
    name = input("What is your name? ")
    print ("Hello, %s." %name)
    Output :

    Code:
    Python 3.6.1 (default, Dec 2015, 13:05:11)
    [GCC 4.8.2] ON LINUX
    
    What isname?name ? Vinod
    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 :

    Code:
    Python 2 - print "hello world"
    Python 3 - ("hello world")
    Python 2

    Input :

    Code:
    print "hello world"
    Output :

    Code:
    Python 2.7.10 {default, jul 14 2015, 19:46:27)
    [GCC 4.8.2] on linux
    
    Hello World
    Python 3 :

    Input :

    Code:
    1 != 1.0
    print (False)1 != 1.0
    print (False)
    Output :

    Code:
    Python 3.6.1 (default, Dec 2015, 13:05:11)
    [GCC 4.8.2] on Linux
    
    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.

    Code:
    Ex :
    Python 2 - <> operator is used for not equal
    Python 3 -  !  operator is used for not equal
    In python 2 :

    Input :

    Code:
    1 <> 1.0
    print "False"
    Output :

    Code:
    Python 3.6.1 (default, Dec 2015, 13:05:11)
    [GCC 4.8.2] ON LINUX
    
    False
    In Python 3 :

    Input :

    Code:
    1 != 1.0
    print (False)1 != 1.0
    print (False)
    Output :

    Code:
    [B]Python 3.6.1 (default, Dec 2015, 13:05:11)
    [GCC 4.8.2] on linux
    
    False[/B]
    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

    Code:
    print range(0,10,1)
    Output

    Code:
    Python 2.7.10 (default, jul 14 2015, 19:46:27)
    [GCC 4.8.2] on linux
    
    [0,1,2,3,4,5,6,7,8,9]

    Python 3

    Input

    Code:
    (list(range(10))
    )

    Output

    Code:
    Python 3.6.1 (default, Dec 2015, 13:05:11)
    [GCC 4.8.2] on linux
    
    =>[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

    Code:
    num1 = 1
    num2 = 2
     
    # Add two numbers
    sum = float(num1) + float(num2)
     
    # Display the sum
    print 'The sum of {0} and {1} is {2}'.format(num1, num2, sum)
    Output


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

    Input :

    Code:
    num1 = 1
    num2 = 2
     
    # Add two numbers
    sum = float(num1) + float(num2)
     
    # Display the sum
    print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
    Code:
    Output :
    Code:
    Python 3.6.1 (default, Dec 2015, 13:05:11)
    [GCC 4.8.2] On linux
    
    =>[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.
    Last edited by Rabbit; Mar 12 '19, 08:03 PM. Reason: external link removed per forum policy
Working...