Blog Post #13: Beyond the Basics: Exploring Advanced Operators (//, %, **)

In Post #12, we covered the four fundamental arithmetic operators that handle everyday calculations. But Python’s mathematical toolkit doesn’t stop there. It includes several advanced operators that allow us to solve more specific and interesting problems with ease.

In this post, we’ll explore three of these powerful operators: floor division (//), the modulus operator (%), and exponentiation (**).

Floor Division (//): Division Without the Remainder

While standard division (/) gives you a precise float answer, sometimes you only care about how many times a number fits completely into another, without worrying about the fractional part. This is where floor division comes in.

The floor division operator (//) divides two numbers and then rounds the result down to the nearest whole number, returning an integer.

Let’s compare it directly with standard division:

# Standard division always gives a float
print(14 / 4)

# Floor division gives the whole number result
print(14 // 4)

The output will be:

3.5
3

A practical use case is when you need to group items. Imagine you have 30 eggs and you need to fill egg cartons that hold 12 eggs each. How many cartons can you completely fill?

total_eggs = 30
carton_size = 12
full_cartons = total_eggs // carton_size

print(full_cartons)

The output is 2, because you can only fill two cartons completely.

The Modulus Operator (%): Just the Leftovers

The modulus operator (%), often called the remainder operator, gives you what’s left over after a division. It’s the perfect companion to floor division.

Following our egg example, if we fill 2 cartons, how many eggs are left over?

total_eggs = 30
carton_size = 12
leftover_eggs = total_eggs % carton_size

print(leftover_eggs)

The output is 6. Because 12 goes into 30 two times (24), with 6 eggs remaining.

One of the most common and clever uses for the modulus operator is to determine if a number is even or odd. Any number that is perfectly divisible by 2 will have a remainder of 0, meaning it’s even.

number_a = 10
number_b = 11

print(number_a % 2)
print(number_b % 2)

The output will be:

0
1

This tells us that 10 is even and 11 is odd.

Exponentiation (**): To the Power Of

The exponentiation operator (**) is used to raise a number to the power of another. It’s Python’s way of saying “to the power of.”

Important: Some other programming languages use the caret symbol (^) for this, but in Python, ^ has a completely different meaning. Always use the double asterisk (**) for powers.

# 2 to the power of 3 (2 * 2 * 2)
print(2 ** 3)

# 5 squared (5 * 5)
print(5 ** 2)

# It also works with floats, allowing you to find roots
# 9 to the power of 0.5 (which is the square root of 9)
print(9 ** 0.5)

The output will be:

8
25
3.0

What’s Next?

You’ve now expanded your mathematical toolkit with three powerful operators: floor division (//) to find how many times a number divides completely, modulus (%) to get the remainder, and exponentiation (**) to calculate powers.

We’ve spent the last several posts learning about data types and operators in isolation. It’s time to put it all together to build something tangible. In Post #14, we will create our first mini-project: a basic calculator program that uses the concepts we’ve just learned.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment