the importance of the remainder function

Coda with remaining values

Why understanding the remainder() function matters

Christiaan Huizer

My youngest son’s casual announcement, ‘We’re learning Python in school,’ sparked more than just my usual programming curiosity. It was a chance to see how coding concepts are introduced to 14-year-olds. What I encountered was a surprisingly rigorous curriculum, demanding a deep understanding of core principles right from the start. One element, in particular, stood out: the remainder function, at least this is the name we apply in Coda.

They presented it early, before loops or even conditional statements. I was struck by this, having often seen the percentage symbol (%) used in Python, as in a % b, to achieve the same result. Why, I wondered, was this concept so fundamental that it preceded so many other building blocks?

What is the Remainder (Modulo) Operator?

The remainder operator, represented by the percent symbol (%), gives you the remainder after dividing one number by another. For example:

  • 10 % 3 equals 1 because 10 divided by 3 is 3 with a remainder of 1.
  • 15 % 5 equals 0 because 15 divided by 5 is 3 with no remainder.

Remainders are perfect for creating repeating patterns or cycles. Think of a clock (12-hour cycle), days of the week (7-day cycle), or dividing items into groups. A very common use is to determine if a number is even or odd. If a number divided by 2 has a remainder of 0, it’s even. Otherwise, it’s odd.

anyNumber.Remainder(2) = 0 - this is the same as IsEven()
anyNumber.Remainder(2)!
= 0 - this is the same as IsOdd()

One of the problems my son had to solve was: convert a number of seconds into hours, minutes, and remaining seconds. That was an interesting challenge.

Last December, I wrote a blog post about calculating employee workdays. Some people worked every Wednesday, while others worked Wednesdays and every other Saturday. My initial attempt using IsOdd() and IsEven() functions quickly proved insufficient, and I realized I needed a better approach.

Instead of focusing on fixed weekly or bi-weekly patterns, I explored scenarios with recurring schedules every 3, 4, 5, or more weeks. This broader perspective revealed a pattern solvable using the Remainder() function which is also known as the modulo operator (%).

When planning with clients, the remainder function is often a lifesaver. While IsOdd() and IsEven() are handy, their simplicity can sometimes narrow your thinking and lead to tunnel vision. Similarly, the equals operator can feel limiting, whereas Contains() lets you check for multiple items in a list at once.

How we think really matters. I try to stay open to all options. You learn this by doing it. It’s amazing that coding helps high schoolers find number patterns they might miss, or only find much later — for example when you start using Coda.

Hopefully, this post has shed some light on this common Coda challenge and empowered you to create more interactive and insightful data experiences. On a personal note, creating these in-depth posts takes a lot of time and effort. While I love sharing my knowledge, a little support goes a long way. If you found this helpful, what about a donation and sharing this post with your fellow Coda enthusiasts? Every bit of encouragement helps!

My name is Christiaan, and I regularly blog about Coda. While this article is free, my professional services (including consultations) are not, but I’m always happy to chat and explore potential solutions. You can find my free contributions in the Coda Community and on X. The Coda Community is a fantastic resource for free insights, especially when you share a sample doc.

No responses yet

Write a response