# New modulo operator to get both quotient and remainder

**URL:** <https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608>\
**Category:** Python Help\
**Created:** [September 25, 2023, 9:02am UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608 "2023-09-25T09:02:25Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [September 25, 2023, 10:20am UTC](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608/3 "2023-09-25T10:20:39Z")

</div>

It does somewhat slowly, though. A benchmark:

```python
 40.99 ± 0.15 ns q, r = x // y, x % y
 40.92 ± 0.09 ns x // y; x % y
 68.08 ± 0.30 ns q, r = divmod(x, y)
 66.14 ± 0.42 ns q, r = dm(x, y)

Python: 3.11.4 (main, Sep 9 2023, 15:09:21) [GCC 13.2.1 20230801]

```

Edit: Hmm, part of the slowness is from creating the tuple, which the operator would likely also do. So how fast would the operator be? I don’t see a way to find out other than implementing it…

> **Benchmark script**
>
> ```python
> from timeit import timeit
> from statistics import mean, stdev
> import sys
> 
> setup = 'x, y, dm = 42, 23, divmod'
> codes = [
> 'q, r = x // y, x % y',
> 'x // y; x % y',
> 'q, r = divmod(x, y)',
> 'q, r = dm(x, y)',
> ]
> 
> times = {c: [] for c in codes}
> def stats(c):
> ts = [t * 1e9 for t in sorted(times[c])[:5]]
> return f'{mean(ts):6.2f} ± {stdev(ts):5.2f} ns '
> for _ in range(100):
> for c in codes:
> t = timeit(';'.join([c] * 100), setup, number=10**3) / 1e5
> times[c].append(t)
> for c in codes:
> print(stats(c), c)
> 
> print('\nPython:', sys.version)
> 
> ```
> 
> [Attempt This Online!](https://ato.pxeger.com/run?1=bVJLTsMwEJVY-hSzQbYrk_4oglQVV2AfIhQSB4xkO9hu1ajqSdiwgT1X4BicBn9SUBFZZd543rw3My_vXe8etXp9fVu79uzy6-S6NVqCE5ILB0J22rghQjFjXeWEdaK2h6zklWIeb_gGDZDtLUKWu3UHK8BbBj2DRvr_8xmD2dwHYiN1g1GtG249XiDwH35mYHy0hfE4lGzhFHrMUi6By2NwKEh0JDSif1PyBy4RCk5Cv12dQ1FCqw3UIBREHXvU8DYatKSmeaRxUZ2DEUz5VXzvwnvrXfKGRLqiLmmRL8oyFhjv2iho8S7MhThL84ts1u7h8wN2cUgRW0RMWcAokN4FUlOpB06mk8nQ-1hdwqImLymthOAlzp60UMSLCBp9rV9FGDwDtZb33Kymk9FoTmHsDSx-KQbhWdV1XHkjFP3XrTNCOXKYCIOaIpQwfKtu4uXkmIV1ZxturNCKpjsazulwVt8)

I’d prefer `/%` or `//%` though, to indicate both things it does and the order of the results:

```python
quotient, remainder = value /% divider
quotient, remainder = value //% divider

```

About usefulness, @mdickinson wrote [about `divmod`](https://discuss.python.org/t/minmax-function-alongside-min-and-max/2834/73) a while ago.

---

_[View the full topic](https://discuss.python.org/t/new-modulo-operator-to-get-both-quotient-and-remainder/34608)._
