# What is the difference between Python 2 and Python 3?

Updated: Jul 29, 2019

Below are the key differences which I have observed and encountered frequently while working on Python 2 and Python 3.

**Integer division**

** Python2** : The return type of a division (/) operation depends on its

*operands*. If both operands are of type

*int*, floor division is performed and an

*int*is returned. If either operand is a

*float*,

*classic division*is performed and a

*float*is returned. The // operator is also provided for doing floor division no matter what the operands are.

`>>> 3/2`

`1 `

`>>> -3/2`

`-2 `

`>>> 3//2`

`1 `

`>>> -3//2`

`-2`

** Python3** : Division (/) always returns a

*float*. To do

*floor division*and get an integer result (discarding any fractional result) you need to use // operator.

`>>> 3/2`

`1.5 `

`>>> -3/2`

`-1.5 `

`>>> 3//21 >>> -3//2-2`

**Input function**

** Python2** : When you use

*input()*function, Python

*dynamically*converts the data type based on your

*input*. So input() function can return anything int, str, float, bool, list etc based on your input. For instance,

`>>> val = input("Enter any value: ")`

`Enter any value: 7`

`>>> type(val)`

**int**

`>>> val = input("Enter any value: ")`

`Enter any value: 7.0`

`>>> type(val)`

**float**

`>>> val = input("Enter any value: ")`

`Enter any value: 'abc'`

`>>> type(val)`

**str**

`>>> val = input("Enter any value: ")`

`Enter any value: `**True**

`>>> type(val)`

**bool**

`>>> val = input("Enter any value: ")`

`Enter any value: [1,2,3,4,5]`

`>>> type(val)`

**list**

When you use *raw_input()*, Python will simply return *string* irrespective of your input value data type.

`>>> val = raw_input("Enter any value: ")`

`Enter any value: 7`

`>>> type(val)`

**str**

`>>> val = raw_input("Enter any value: ")`

`Enter any value: 7.0`

`>>> type(val)`

**str**

`>>> val = raw_input("Enter any value: ")`

`Enter any value: 'abc'`

`>>> type(val)`

**str**

`and so on..`

*Python3***:** In Python 3, *input()* function returns *string* type (acts like *raw_input()*). It’s confusing sometimes when you are transiting from Python 2 to Python 3.

`>>> val = input("Enter any value: ")`

`Enter any value: 7`

`>>> type(val)`

**str**

*Note when you are using input() in Python 3 : *If you are using Python 2.7, it’s good idea to use raw_input() when you are getting user inputs.

In Python 3, you would use input(). Both the functions interprets all input as a string based on which version of Python you are using.

Sometimes, I feel instead of Python doing type change dynamically it’s always safe to handle it manually. In Python 3, when you use the input() func, every input will be interpreted as string and sometimes it creates unexpected results. Consider the below example,

`>>> a = input("Enter first number: ")`

`Enter first number: 3.4 `

`>>> b = input("Enter second number: ")`

`Enter second number: 5.7 `

`>>> `**print** a + b

`3.45.7 `

`# It's because`

`>>> type(a), type(b)`

`(str, str) `

`# In order to fix this you need to apply float() function when user is prompted for input.`

**Round function**

** Python2** : Return the

*floating point value*number rounded to n digits after the decimal point. If n digits is omitted, it defaults to zero. The result is a floating point number. Values are rounded to the closest multiple of 10 to the power minus n digits; if two multiples are equally close, rounding is done away from 0 (so, for example, round(0.5) is 1.0 and round(-0.5) is -1.0).

`>>> round(3.5)`

`4.0`

** Python3** : Return

*number*rounded to n digits precision after the decimal point. If n digits is omitted or is None, it returns the nearest integer to its input.

`>>> round(3.5)`

`4`

**Print function**

** Python2** : Extra pair of parenthesis was

*not mandatory*.

`>>> `**print** "Hello"

`Hello`

`>>> `**print** 'Hello'

`Hello`