Finding a remainder from division #
In Python, the modulo operator % returns the remainder when one number is divided by another. For example, a % b returns the remainder when a is divided by b. The modulo operator is often used in programming to determine if a number is even or odd, or to wrap a value around a certain range.
Here are some examples of using the modulo operator in Python:
# 7 % 2 evaluates to 1, because 7 divided by 2 has a remainder of 1.
print(7 % 2) # output: 1
# 10 % 5 evaluates to 0, because 10 divided by 5 has no remainder.
print(10 % 5) # output: 0
Exercise #
- Set the variable
xto be the remainder when15is divided by2 - Must use the
%sign
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules