How is the logical processing being done for strings like 'Dog' and'Cat'

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

    How is the logical processing being done for strings like 'Dog' and'Cat'

    Hi all,
    I am a novice programmer in Python.
    Please could you explain me the results (regarding logical operators).

    I get this:
    >>print bool('God' and 'Devil')
    True

    [This is ok because (any) string is True, so; (True and True) gives
    True]


    >>print('God' and 'Devil')
    Devil

    [This is what I don't get ]
    and for that matter,I also checked out this:
    >>01 and 10
    10


    What is python doing when we type in ('God' and 'Devil') or key in (01
    and 10) ?

  • Benjamin

    #2
    Re: How is the logical processing being done for strings like 'Dog'and 'Cat'

    On Oct 20, 8:41 pm, Sumitava Mukherjee <sm...@cognobyt es.comwrote:
    Hi all,
    I am a novice programmer in Python.
    Please could you explain me the results (regarding logical operators).
    >
    I get this:
    >
    >print bool('God' and 'Devil')
    >
    True
    >
    [This is ok because (any) string is True, so; (True and True) gives
    True]
    >
    >print('God' and 'Devil')
    >
    Devil
    >
    [This is what I don't get ]
    and for that matter,I also checked out this:
    >
    >01 and 10
    >
    10
    >
    What is python doing when we type in ('God' and 'Devil') or key in (01
    and 10) ?
    This is an interesting property of python's logical operators. They
    don't have to return a boolean object. (It's documented here:
    http://docs.python.org/reference/exp...ean-operations)

    Comment

    • zaarg

      #3
      Re: How is the logical processing being done for strings like 'Dog'and 'Cat'

      On Oct 20, 9:41 pm, Sumitava Mukherjee <sm...@cognobyt es.comwrote:
      Hi all,
      I am a novice programmer in Python.
      Please could you explain me the results (regarding logical operators).
      >
      I get this:
      >
      >print bool('God' and 'Devil')
      >
      True
      >
      [This is ok because (any) string is True,
      Not quite so. Be careful with this. The empty string gets False:
      print bool("") --False
      Any *non-empty* string gets True.
      so; (True and True) gives
      True]
      >
      >print('God' and 'Devil')
      >
      Devil
      >
      [This is what I don't get ]
      and for that matter,I also checked out this:
      >
      >01 and 10
      >
      10
      Note that AND is a boolean operator, not a bit operator. If you want
      to hack bits of an integer, use the "binary bitwise operators"
      This chapter explains the meaning of the elements of expressions in Python. Syntax Notes: In this and the following chapters, extended BNF notation will be used to describe syntax, not lexical anal...

      e.g.
      >>01 & 10
      0
      >>01 | 10
      11
      >
      What is python doing when we type in ('God' and 'Devil') or key in (01
      and 10) ?

      Comment

      • Steven D'Aprano

        #4
        Re: How is the logical processing being done for strings like 'Dog'and'Cat'

        On Mon, 20 Oct 2008 18:41:23 -0700, Sumitava Mukherjee wrote:
        What is python doing when we type in ('God' and 'Devil') or key in (01
        and 10) ?

        There are two important things you need to know:

        (1) All Python objects have a boolean context.

        (2) Python's boolean operators are short-circuit operators.


        Point (1) means that you can say this:


        if x:
        print 'x has a true value'
        else:
        print 'x has a false value'

        and it will work for any x, not just True and False.

        As a general rule, false values are empty:

        - the empty string is false: ''
        - empty lists are false: []
        - empty tuples are false: ()
        - zeroes are false: 0, 0.0
        - None is false
        - etc.

        and everything else is true.

        So you can do this:

        if alist:
        # alist is not empty
        x = alist[0] # so this is safe
        else:
        print 'alist is empty'


        config = get_config_pars er() # whatever that is...
        if config:
        do_stuff_with_c onfig
        else:
        print "config is empty"




        Point (2) means that Python will only evaluate the least number of
        objects needed. So for example:

        42 or 19

        Since 42 is a true object, looking at the second item is pointless, and
        Python short-circuits by returning 42.

        0 or 19

        Since 0 is a false object, Python must look at the second item. Since 19
        is true, it returns 19.

        And similarly for and.


        This is especially useful with the or operator. Instead of this:

        astring = something()
        if len(astring) == 0:
        astring = '<empty>'
        do_something_wi th(astring)


        you can do this:

        astring = something() or '<empty>'
        do_something_wi th(astring)



        --
        Steven

        Comment

        Working...