LoginSignup
1
1

More than 3 years have passed since last update.

[python] 商と剰余の取得

Last updated at Posted at 2020-03-31

商と剰余を同時に取得してみる

ポイント

  • divmodを使う
n = 3
cnt = 10
quot, mod = divmod(cnt, n)
#商
print(quot)
#剰余
print(mod)
>>> #商
>>>
>>> print(quot)
3
>>> #剰余
>>>
>>> print(mod)
1
>>>

商と剰余を別々にとってみる

ポイント

  • 商 //
  • 剰余 %
n = 3
cnt = 10
#商
print(cnt // n)
#剰余
print(cnt % n)
>>> #商
>>>
>>> print(cnt // n)
3
>>> #剰余
>>>
>>> print(cnt % n)
1
>>>
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1