Lecture
Remainder Operator %
The % operator returns the remainder of a division between two numbers.
For example, 10 % 3 returns 1, which is the remainder when 10 is divided by 3.
Using the Remainder Operator
remainder = 10 % 3 print(remainder) # 1
Applications of the Remainder Operator
The remainder operator is often used to determine whether a number is even or odd by checking the remainder when it is divided by 2.
Determine Even or Odd
# Example of determining even or odd number = 7 # A number is even if it's divisible by 2 with no remainder; otherwise, it's odd if number % 2 == 0: print("It's an even number.") else: print("It's an odd number.")
It can also be used to find the unit digit of a number by calculating the remainder when it is divided by 10.
Finding the Units Digit
# Finding the units digit of a number number = 123 ones_place = number % 10 print(ones_place) # 3
Lessons in this chapter Β· Numeric Data Types and Operators
- 1. Working with Numbers in Python
- 2. Arithmetic Operations in Python
- 3. Integer Division Operator //
- 4. Remainder Operator %
- 5. How to Calculate Squares in Python
- 6. Multiple-choice quiz
- 7. Determining Operator Precedence
- 8. TypeError Caused by Operations Between Different Data Types
- 9. What Do `+=`, `-=` Mean?
- 10. Python Integer Division Operator Coding Quiz
- 11. Multiple-choice quiz
- 12. Fill-in-the-blank quiz
Quiz
0 / 1
When writing code to determine whether a number is even or odd using the remainder operator, what is the appropriate code to fill in the blank?
number = 7if number % 2 == : print('Even')else: print('Odd')Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help