[Python] 몫과 나머지

디딧·2022년 11월 20일

Python 문법

목록 보기
4/5
post-thumbnail

1. 몫 구하기

// 연산자

tmp = 7 // 2
print(tmp)
# output
3

2. 나머지 구하기

% 연산자

tmp = 7 % 2
print(tmp)
# output
1

3. 몫과 나머지를 동시에 구하기

divmod(number1, number2)
number1을 number2로 나눴을 때의 몫과 나머지를 튜플 형식으로 반환

tmp1, tmp2 = divmod(7, 2)
print('divmod(7, 2) =', divmod(7, 2))
print('tmp1 =', tmp1, '\ntmp2 =', tmp2)
# output
divmod(7, 2) = (3, 1)
tmp1 = 3 
tmp2 = 1
profile
M.S. in Statistics, 2022 - present

0개의 댓글