Boolean result of divmod

Discussion in 'Python' started by Gnarlodious, Jun 21, 2011.

  1. Gnarlodious

    Gnarlodious Guest

    What is the easiest way to get the first number as boolean?

    divmod(99.6, 30.1)

    Or do I have to say:

    flote, rem=divmod(99.6, 30.1)
    bool(flote)

    -- Gnarlie
    Gnarlodious, Jun 21, 2011
    #1
    1. Advertising

  2. Gnarlodious

    Chris Torek Guest

    In article <>
    Gnarlodious <> wrote:
    >What is the easiest way to get the first number as boolean?
    >
    >divmod(99.6, 30.1)


    divmod returns a 2-tuple:

    >>> divmod(99.6,30.1)

    (3.0, 9.2999999999999901)

    Therefore, you can subscript the return value to get either
    element:

    >>> divmod(99.6,30.1)[0]

    3.0

    Thus, you can call bool() on the subscripted value to convert
    this to True-if-not-zero False-if-zero:

    >>> bool(divmod(99.6,30.1)[0])

    True
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: gmail (figure it out) http://web.torek.net/torek/index.html
    Chris Torek, Jun 21, 2011
    #2
    1. Advertising

  3. Gnarlodious

    MRAB Guest

    On 21/06/2011 01:28, Gnarlodious wrote:
    > What is the easiest way to get the first number as boolean?
    >
    > divmod(99.6, 30.1)
    >
    > Or do I have to say:
    >
    > flote, rem=divmod(99.6, 30.1)
    > bool(flote)
    >

    divmod returns a tuple, so:

    bool(divmod(99.6, 30.1)[0])
    MRAB, Jun 21, 2011
    #3
  4. Gnarlodious

    Terry Reedy Guest

    On 6/20/2011 8:28 PM, Gnarlodious wrote:
    > What is the easiest way to get the first number as boolean?
    >
    > divmod(99.6, 30.1)
    >
    > Or do I have to say:
    >
    > flote, rem=divmod(99.6, 30.1)
    > bool(flote)


    divmod(x,y) == x//y, x%y

    so bool(x//y)

    --
    Terry Jan Reedy
    Terry Reedy, Jun 21, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J Leonard
    Replies:
    4
    Views:
    12,620
    Mark Space
    Jan 19, 2008
  2. Ethan Furman
    Replies:
    3
    Views:
    377
    Ken Starks
    Jul 16, 2008
  3. kj
    Replies:
    3
    Views:
    793
    Martin v. Löwis
    Sep 27, 2009
  4. Michael Tan
    Replies:
    32
    Views:
    925
    Ara.T.Howard
    Jul 21, 2005
  5. Dirk Traulsen

    Bug: Numeric#divmod calculates wrongly

    Dirk Traulsen, Oct 8, 2007, in forum: Ruby
    Replies:
    11
    Views:
    226
    Axel Etzold
    Oct 15, 2007
Loading...

Share This Page