How to Divide Numbers in Python?

A short and sweet tutorial with code examples on how to divide numbers in Python.

How to Divide Numbers in Python?
Image: Made in Canva.

You started programming in Python and now need to use mathematical operations. I've got you covered!

To divide numbers in Python, all you need to do is use the slash operator like so:

result = 15 / 3
# result = 5.0
# Why 5.0 and not 5, you ask? Because division results in floating numbers. To get 5, you'll need to cast the number to int like so:
# result = int(15 / 3) = 5

What Does % Mean in Python?

What if you only want the remainder of a number after division? That's called modulus, and to calculate that in Python, you need to use the percentage operator like so:

result = 15 % 2
# result = 1

Let's explain why the result equals 1.

If you divide 15 by 2, you divide the 15 into two separate equal piles. Because 15 is not an even number, you have a reminder that is left and cannot go to any of the two "piles" you divided the 15 into. That's the "leftover" 1, and using modulus in Python allows you to get it.

Need to see it with your own eyes? Here's a video:

I hope this short and sweet article helped you out. If you have any questions, let me know in the comments below!