The percentage symbol (%) in python has a different meaning and purpose. Modulus operator is similar to the division (/) operation, but the result is the remainder instead of the quotient. In this blog, we will see what is a modulus operator(%) in python and how to use it.
The syntax for modulus operator is
Where a,b and c are a numeric value.
Let’s take a look at a python code example
You can even use float point numbers in python to get their remainders.
Let’s take a look to see how the modulus calculates the remainder
All this calculation is required if you don’t use the modulus operator (%).
Here is a programming example that can help you understand better how the operator works.
To find the leap year
year = int(input("Enter a year: ")) if (year % 4) == 0: if (year % 100) == 0: if (year % 400) == 0: print(" leap year",year) else: print("not a leap year",year) else: print("leap year",year) else: print("not a leap year",year)