Python 2x and 3x difference

        Difference between Python 2.x and Python 3.x?

1.      Python 2.x print is treated as a statement instead of a function .Python 2.x doesn’t have a problem with additional parenthesis, it can execute code with parenthesis. While in Python 3.x print is treated as a function. Whatever you want to print, pass it to print ().

Example:  print “Hello World”                                                                   in Python 2.x
                 print (“Hello World”)                                                                 in Python 3.x


2.      If we are porting code or if we are executing python 3.x code in python 2.x, since the change in integer division behavior.

Example: 3/2=1                                                                                           in Python 2.x
                3//2=1

                3/2=1.5                                                                                        in Python 3.x
                3//2=1

3.      In Python 2.x, it uses reduce() function whereas in Python 3, the reduce() function has been removed from the global namespace and placed in the functools module.

Example:  reduce(a, b, c)                                                                            in Python 2.x
                 
                  from funtools import reduce                                                     in Python 3.x
                  reduce(a, b, c)

4.      The raw_input() function allows us to input strings into a running program:

Example: name=raw_input(“what is your name”)                                     in Python 2.x
                 print(“hello” + name)

The raw_input() function is renamed as input()  in python 3.x:

                 name=input(“whats your name”)                                              in Python 3.x
                 print(“hello” +name)



5.      The  code in python 3.x run slower than python 2.x

Example:  1000 loops, best of 3:1.72 ms per loop                                    in Python 2.x

                  100 loops, best of 3:2.68 ms per loop                                     in Python 3.x

Comments