Logical AND question

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

    Logical AND question

    Hello,
    Can someone explain to me why only one of these two if/print statements
    prints something? Thanks!
    Louis

    IDLE 1.0.2[color=blue][color=green][color=darkred]
    >>> s = ''
    >>> t = ''
    >>> if s and t == '':[/color][/color][/color]
    print "This doesn't work!"

    [color=blue][color=green][color=darkred]
    >>> s and t[/color][/color][/color]
    ''[color=blue][color=green][color=darkred]
    >>> if s == '' and t == '':[/color][/color][/color]
    print "This does!"


    This does![color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]


  • Tor Iver Wilhelmsen

    #2
    Re: Logical AND question

    "3c273" <nospam@nospam. com> writes:
    [color=blue][color=green][color=darkred]
    > >>> if s and t == '':[/color][/color]
    > print "This doesn't work!"[/color]

    This is really

    if (s) and (t == '')

    which evaluates to false since "if (s)" returns false for the empty
    string.

    Comment

    • Peter Otten

      #3
      Re: Logical AND question

      Tor Iver Wilhelmsen wrote:
      [color=blue]
      > "3c273" <nospam@nospam. com> writes:
      >[color=green][color=darkred]
      >> >>> if s and t == '':[/color]
      >> print "This doesn't work!"[/color]
      >
      > This is really
      >
      > if (s) and (t == '')
      >
      > which evaluates to false since "if (s)" returns false for the empty
      > string.[/color]

      The original poster probably wants
      [color=blue][color=green][color=darkred]
      >>> if s == t == "":[/color][/color][/color]
      .... print "this works"
      ....
      this works[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      or the conventional
      [color=blue][color=green][color=darkred]
      >>> if s == "" and t == "":[/color][/color][/color]
      .... print "but this is more common"
      ....
      but this is more common[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Peter

      Comment

      • 3c273

        #4
        Re: Logical AND question

        Thank you both :-)

        "Peter Otten" <__peter__@web. de> wrote in message
        news:c808i3$aen $06$1@news.t-online.com...[color=blue]
        > Tor Iver Wilhelmsen wrote:
        >[color=green]
        > > "3c273" <nospam@nospam. com> writes:
        > >[color=darkred]
        > >> >>> if s and t == '':
        > >> print "This doesn't work!"[/color]
        > >
        > > This is really
        > >
        > > if (s) and (t == '')
        > >
        > > which evaluates to false since "if (s)" returns false for the empty
        > > string.[/color]
        >
        > The original poster probably wants
        >[color=green][color=darkred]
        > >>> if s == t == "":[/color][/color]
        > ... print "this works"
        > ...
        > this works[color=green][color=darkred]
        > >>>[/color][/color]
        >
        > or the conventional
        >[color=green][color=darkred]
        > >>> if s == "" and t == "":[/color][/color]
        > ... print "but this is more common"
        > ...
        > but this is more common[color=green][color=darkred]
        > >>>[/color][/color]
        >
        > Peter
        >[/color]


        Comment

        Working...