Python Modulo With Examples

Good day, learners. In our previous tutorial, we learned about Python NumPy Module. In this tutorial we are going to see examples of Python Modulo.

Python Modulo

Basically Python modulo operation is used to get the reminder of a division. The basic syntax of Python Modulo is a % b. Here a is divided by b and the remainder of that division is returned.

In many language, both operand of this modulo operator has to be integer. But Python Modulo is flexible in this case. The operands can be either integer or float.

Python Modulo Example

We have discussed a little bit about modulo operation in the python operators tutorial. Now we will see python modulo example here.

In the following program you will be asked if you want to continue the program. By pressing ‘y’ means, you want to continue. Otherwise, the program will terminate instantly.

When you continue with the program, you have to enter two numbers and by using modulo operation, the reminder will be printed.

The output will vary for different input. For my input, the output was the following:

Python Modulo Operator Exception

The only Exception you get with python modulo operation is ZeroDivisionError error. This happens if the divider operand of the modulo operator become zero. That means the right operand can’t be zero. Let’s see the following code to know about this python exception.

The output of the following code will be like this

Python Modulo

That’s all about python modulo example. If you have any query feel free to use the comment box.

Reference: Official Documentation, ZeroDivisionError

By admin

Leave a Reply

%d bloggers like this: