[CentralOH] DOJO November 28, 2014: divmod() and tmpnb.org

pybokeh pybokeh at gmail.com
Sat Nov 29 04:17:22 CET 2014


Thanks to Jim, I found out about the divmod() built-in function.
help(divmod) returns:
divmod(...)
    divmod(x, y) -> (div, mod)

    Return the tuple ((x-x%y)/y, x%y).  Invariant: div*y + mod == x.

Basically, the function returns 2 values in a form of a tuple where the
first value is the quotient and the 2nd value is the remainder.

example:
minutes, seconds = divmod(3600, 60)  # unpacking a tuple into 2 variables
minutes, seconds = (60, 0) or minutes = 60 and seconds = 0

divmod() came in handy when I tried to make a countdown timer which I am
not ashamed of showing how terrible my code is:
http://nbviewer.ipython.org/github/pybokeh/ipython_notebooks/blob/master/dates/CountDown.ipynb

divmod() eliminated the need for me to handle the remainders of dividing up
the years, hours, and minutes.  Very nice!

I was also curious what other built-in functions are available.  You can
find out by doing:
import builtins
dir(builtins)

- Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20141128/bad970dd/attachment.html>


More information about the CentralOH mailing list