'IF' Syntax For Alternative Conditions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rshepard@nospam.appl-ecosys.com

    'IF' Syntax For Alternative Conditions

    All my python books and references I find on the web have simplistic
    examples of the IF conditional. A few also provide examples of multiple
    conditions that are ANDed; e.g.,
    if cond1:
    if cond2:
    do_something.

    However, I cannot find, nor create by trial-and-error, the syntax for
    alternative conditions that are ORed; e.g.,

    if cond1 OR if cond2:
    do_something.

    I've tried using the C syntax for OR (||) but python complained. I'm sure
    there's a way to do this rather than using if cond1: elif cond2: both with
    the same code to execute.

    Please pass me a pointer so I can learn how to correctly write this.

    Rich
  • Leif K-Brooks

    #2
    Re: 'IF' Syntax For Alternative Conditions

    rshepard@nospam .appl-ecosys.com wrote:
    However, I cannot find, nor create by trial-and-error, the syntax for
    alternative conditions that are ORed; e.g.,
    >
    if cond1 OR if cond2:
    do_something.
    if cond1 or cond2:
    do_something()

    Comment

    • Paul Rubin

      #3
      Re: 'IF' Syntax For Alternative Conditions

      rshepard@nospam .appl-ecosys.com writes:
      if cond1:
      if cond2:
      do_something.
      You can write:
      if cond1 and cond2:
      do_something
      if cond1 OR if cond2:
      do_something.
      if cond1 or cond2:
      do_something
      I've tried using the C syntax for OR (||) but python complained. I'm sure
      there's a way to do this rather than using if cond1: elif cond2: both with
      the same code to execute.
      Python uses the "and" and "or" keywords for && and ||.

      Comment

      • Gabriel Genellina

        #4
        Re: 'IF' Syntax For Alternative Conditions

        En Thu, 08 Feb 2007 01:01:38 -0300, <rshepard@nospa m.appl-ecosys.com>
        escribió:
        However, I cannot find, nor create by trial-and-error, the syntax for
        alternative conditions that are ORed; e.g.,
        >
        if cond1 OR if cond2:
        do_something.
        >
        Please pass me a pointer so I can learn how to correctly write this.
        See the Python tutorial:

        Note that most (if not all) Python keywords are lowercase.

        --
        Gabriel Genellina

        Comment

        • Duncan Booth

          #5
          Re: 'IF' Syntax For Alternative Conditions

          "Gabriel Genellina" <gagsl-py@yahoo.com.ar wrote:
          Note that most (if not all) Python keywords are lowercase.
          >
          All keywords are lower case.

          and del from not while
          as elif global or with
          assert else if pass yield
          break except import print
          class exec in raise
          continue finally is return
          def for lambda try

          'None' is not entirely lowercase, and you cannot assign to it, but
          technically it isn't a keyword.

          Comment

          • James Stroud

            #6
            Re: 'IF' Syntax For Alternative Conditions

            rshepard@nospam .appl-ecosys.com wrote:
            All my python books and references I find on the web have simplistic
            examples of the IF conditional. A few also provide examples of multiple
            conditions that are ANDed; e.g.,
            if cond1:
            if cond2:
            do_something.
            >
            However, I cannot find, nor create by trial-and-error, the syntax for
            alternative conditions that are ORed; e.g.,
            >
            if cond1 OR if cond2:
            do_something.
            >
            I've tried using the C syntax for OR (||) but python complained. I'm sure
            there's a way to do this rather than using if cond1: elif cond2: both with
            the same code to execute.
            >
            Please pass me a pointer so I can learn how to correctly write this.
            >
            Rich
            For lots of conditions:

            import operator
            reduce(operator .or_, list_of_conditi ons)

            e.g.:

            pyimport operator
            pylist_of_condi tions = [
            .... 'big' < 'small',
            .... 'all' 'half',
            .... 'five' 'one',
            .... 'six' < 'seven'
            .... ]
            pylist_of_condi tions
            [True, False, False, False]
            pyreduce(operat or.or_, list_of_conditi ons)
            True

            James

            Comment

            • rshepard@foobar.appl-ecosys.com

              #7
              Re: 'IF' Syntax For Alternative Conditions

              On 2007-02-08, Paul Rubin <httpwrote:
              rshepard@nospam .appl-ecosys.com writes:
              > if cond1:
              > if cond2:
              > do_something.
              >
              You can write:
              if cond1 and cond2:
              do_something
              >
              > if cond1 OR if cond2:
              > do_something.
              >
              if cond1 or cond2:
              do_something
              >
              > I've tried using the C syntax for OR (||) but python complained. I'm sure
              >there's a way to do this rather than using if cond1: elif cond2: both with
              >the same code to execute.
              >
              Python uses the "and" and "or" keywords for && and ||.
              Allow me to thank all of you who responded with this one article. For
              whatever reason, it did not occur to me to use the words 'and' and 'or.'
              And, I did not see this in the tutorial or introduction ... which is my
              fault.

              So, I do thank all of you.

              Rich

              Comment

              Working...