while var, but var ==16 != true

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • maestro

    while var, but var ==16 != true

    why does this work? "while p" = "while p != 0" ? 1 is True and 0 is
    false in python but other numbers have no boolean value so why doesnt
    it abort.


    >>p=16
    >>p
    16
    >>while p:
    print p
    p -= 1


    16
    15
    14
    13
    12
    11
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    >>>

    i can also do:
    >>k=[]
    >>while k:
    k.pop()

    >>k=[1,2,3]
    >>while k:
    k.pop()


    3
    2
    1
    >>>

    so obv while var means while not empty or why not zero but it isnt
    something youd guess unless youd beeen shown it.
  • Larry Bates

    #2
    Re: while var, but var ==16 != true

    maestro wrote:
    why does this work? "while p" = "while p != 0" ? 1 is True and 0 is
    false in python but other numbers have no boolean value so why doesnt
    it abort.
    >
    >
    >
    >>>p=16
    >>>p
    16
    >>>while p:
    print p
    p -= 1
    >
    >
    16
    15
    14
    13
    12
    11
    10
    9
    8
    7
    6
    5
    4
    3
    2
    1
    >
    >
    i can also do:
    >
    >>>k=[]
    >>>while k:
    k.pop()
    >
    >
    >>>k=[1,2,3]
    >>>while k:
    k.pop()
    >
    >
    3
    2
    1
    >
    >
    so obv while var means while not empty or why not zero but it isnt
    something youd guess unless youd beeen shown it.
    Python manuals outline that 0, None, empty list, empty dictionary are False
    anything else evaluates to True. This is all over this list as well.

    -Larry

    Comment

    • Tim Roberts

      #3
      Re: while var, but var ==16 != true

      maestro <notnorwegian@y ahoo.sewrote:
      >
      >why does this work? "while p" = "while p != 0" ? 1 is True and 0 is
      >false in python but other numbers have no boolean value so why doesnt
      >it abort.
      Because your statement is incorrect. Everything has a boolean value in
      Python. 0, None, False, '' (empty string), [] (empty list), () (empty
      tuple), and {} (empty dictionary) all have a False value. Everything else
      has a True value.

      Python didn't even have a boolean type (True and False) until rather
      recently (2.2?).

      This is a very handy feature, and it's one of the things I love about
      Python.
      >so obv while var means while not empty or why not zero but it isnt
      >something youd guess unless youd beeen shown it.
      It's clearly stated in the documentation. I don't know how you concluded
      that True and False were the only boolean values.
      --
      Tim Roberts, timr@probo.com
      Providenza & Boekelheide, Inc.

      Comment

      • John Machin

        #4
        Re: while var, but var ==16 != true

        On Jul 14, 3:32 pm, Tim Roberts <t...@probo.com wrote:
        maestro <notnorweg...@y ahoo.sewrote:
        >
        why does this work?  "while p" = "while p != 0" ? 1 is True and 0 is
        false in python but other numbers have no boolean value so why doesnt
        it abort.
        >
        Because your statement is incorrect.  Everything has a boolean value in
        Python.  0, None, False, '' (empty string), [] (empty list), () (empty
        tuple), and {} (empty dictionary) all have a False value.  Everything else
        has a True value.
        Not quite; for example:
        >>bool(set())
        False
        >>>
        According to section 5.10 of the Reference Manual:
        """
        In the context of Boolean operations, and also when expressions are
        used by control flow statements, the following values are interpreted
        as false: False, None, numeric zero of all types, and empty strings
        and containers (including strings, tuples, lists, dictionaries, sets
        and frozensets). All other values are interpreted as true.
        """

        ... and for the definition for what a user-written class needs to do,
        see section 3.4.1:
        """
        __nonzero__( self)

        Called to implement truth value testing, and the built-in operation
        bool(); should return False or True, or their integer equivalents 0 or
        1. When this method is not defined, __len__() is called, if it is
        defined (see below). If a class defines neither __len__() nor
        __nonzero__(), all its instances are considered true.
        """

        Cheers,
        John

        Comment

        • bruno.desthuilliers@gmail.com

          #5
          Re: while var, but var ==16 != true

          On 14 juil, 07:32, Tim Roberts <t...@probo.com wrote:
          (snip)
          Everything has a boolean value in
          Python. 0, None, False, '' (empty string), [] (empty list), () (empty
          tuple), and {} (empty dictionary) all have a False value. Everything else
          has a True value.
          Unless the author of the class specified it otherwise (implementing
          the appropriate __magic_method_ _).

          Comment

          • Jeffrey Froman

            #6
            Re: while var, but var ==16 != true

            Tim Roberts wrote:
            Everything has a boolean value in
            Python.  0, None, False, '' (empty string), [] (empty list), () (empty
            tuple), and {} (empty dictionary) all have a False value.  Everything else
            has a True value.
            Empty set objects also evaluate as false in a boolean context.


            Jeffrey

            Comment

            Working...