Division Operators in Python

Introduction

Suppose we have 12 mangoes and we have to equally distribute the mangoes to 3 kids. So, how can we decide how many mangoes should be given so that each one will get the same number of mangoes? For this, we can use the division operator.

While performing the division on numbers, we may expect the output as a precise floating-point number or the output as a rounded off integer value.

Python division operator is of two types:

  • / : Divides the two numbers and returns the floating value.
  • // : Divides the two number and returns the floor value.

Program

1.      Division operator(/)

print("Divide integer by integer")

print("4/2 = ", 4/2)

print("15/5 = ", 15/5)

print("-4/2 = ", -4/2)

print("-15/5 = ", -15/5)

print(" ")




print("Divide float by integer")

print("4.2/2 = ", 4.2/2)

print("15.7/5 = ", 15.7/5)

print("-4.2/2 = ", -4.2/2)

print("-15.7/5 = ", -15.7/5)

print(" ")




print("Divide integer by float")

print("4/2.2 = ", 4/2.2)

print("15/5.1 = ", 15/5.1)

print("-4/2.2 = ", -4/2.2)

print("-15/5.1 = ", -15/5.1)

print(" ")




print("Divide float by float")

print("4.6/2.5 = ", 4.6/2.5)

print("15.5/5.5 = ", 15.5/5.5)

print("-4.6/2.5 = ", -4.6/2.5)

print("-15.5/5.5 = ", 1-5.5/5.5)

print(" ")

Output:

Division Operators in Python Output 1

2.      Floor division operator(//)

 

print("Divide integer by integer")

print("4//2 = ", 4//2)

print("15//5 = ", 15//5)

print("-4//2 = ", -4//2)

print("-15//5 = ", -15//5)

print(" ")




print("Divide float by integer")

print("4.2//2 = ", 4.2//2)

print("15.7//5 = ", 15.7//5)

print("-4.2//2 = ", -4.2//2)

print("-15.7//5 = ", -15.7//5)

print(" ")




print("Divide integer by float")

print("4//2.2 = ", 4//2.2)

print("15//5.1 = ", 15//5.1)

print("-4//2.2 = ", -4//2.2)

print("-15//5.1 = ", -15//5.1)

print(" ")




print("Divide float by float")

print("4.6//2.5 = ", 4.6//2.5)

print("15.5//5.5 = ", 15.5//5.5)

print("-4.6//2.5 = ", -4.6//2.5)

print("-15.5//5.5 = ", -15.5//5.5)

print(" ")

 

Output:

Division Operators in Python Output 2

From the output of the two programs, we can observe that the division operator(/) divides the values and returns the output as floating point number whereas the floor division operator(//) is first dividing the values and then rounding off the output to floor value before displaying.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.