학습 자료

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
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 = 7


if number % 2 == 
:

    print('Even')

else:

    print('Odd')

학습 자료

AI 튜터

디자인

업로드

수업 노트

즐겨찾기

도움말

코드 에디터

코드 실행
코드 생성

실행 결과