Trusted answers to developer questions

What are division operators in Python?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

If you tried to equally divide an unequal amount of candies, you would be left with a few remaining candies. In division, those that remain are called remainders.

What do we do with this remainder? Do we display the number as a floating point or do we round it down to the closest whole number?

Python has an answer with its division operators.

Types of division operators in Python

In Python, there are two types of division operators:

  • /: Divides the number on its left by the number on its right and returns a floating point value.

  • //: Divides the number on its left by the number on its right, rounds down the answer, and returns a whole number.

The illustration below shows how this works.

Examples

Now that we know how the operators work and what kind of division each operator performs; let’s look at an example of division in Python.

# Dividing an integer by an integer
print("The answer for 5/2: ")
print(5/2)
print("The answer for 5//2: ")
print(5//2)
print("\n")
# Dividing an integer by a float
print("The answer for 5/2.75: ")
print(5/2.75)
print("The answer for 5//2.75: ")
print(5//2.75)
print("\n")
#Dividing a float by an integer
print("The answer for 5.5/2: ")
print(5.5/2)
print("The answer for 5.5//2: ")
print(5.5//2)
print("\n")
#Dividing a float by an float
print("The answer for 5.5/2.5: ")
print(5.5/2.5)
print("The answer for 5.5//2.5: ")
print(5.5//2.5)

RELATED TAGS

floating point
round down
python
divide
numerical operator
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?