What is integer and float division in Python?

The division function in Python has two variations:

  • Float division: gives a decimal answer.

  • Integer division: gives the answer in whole numbers (the division result is rounded to the nearest whole number).

Float division ( / )

The float division operation / is used when you want a more precise division result, as it does not round off the value to whole numbers.

It must be noted that for recurring decimal values, there is a limit to how many digits a float variable can store, so it is not always possible to store the exact value.

The return value for the float division operator is always of type float, even when the operands of the operator are not float variables themselves.

This behavior of the float division operator began with the advent of Python3, as in Python2, the float division operator would return an integer value if both operands were integers.

Float division example

The following code example shows that regardless of what numeric data type your variables are, your answer is always in float type if you use the float division operator.

# initializing two floats
my_float1 = 10.0
my_float2 = 10.0
print ("Result of float division of two floats: ", my_float1/my_float2)
# initializing two integers
my_int1 = 10
my_int2 = 10
print ("Result of float division of two integers: ", my_int1/my_int2)

Integer division ( // )

The integer division operation // is used when you want your answer to be in whole numbers. Since it is very common for the result of dividing two integers to be a decimal value, in Python3 integers, division rounds the result to the lower boundalso known as flooring the result.

The return value for the integer division operator is of type integer if the operands are both integers. In contrast, it returns a float value if either one of the operands is a float.

The integer division operator was introduced in Python3, so for systems running Python2, the user must import the __future__ module to use it.

Integer division examples

The following code example shows the division of two integers to produce an integer result.

# initializing two integers
my_int1 = 10
my_int2 = 10
print ("Result of integer division of two integers: ", my_int1//my_int2)

The following example demonstrates that if either one of the operands (or both) is a float, then the answer is also a float, but rounded to the nearest lower bound whole number.

# initializing two floats
my_float1 = 4
my_float2 = 3.0
print ("Result of integer division of floats and integers: ", my_float1//my_float2)
# initializing two floats
my_float3 = 4.0
my_float4 = 3
print ("Result of integer division of floats and integers ", my_float3//my_float4)

Note how the result is a float type value, but just as for the normal integer division we saw earlier, the result is still floored. This means that only the quotient part is kept from the original float answer, while everything coming after the decimal is discarded and represented with a 0.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved