32

I am using Python and I would like to have an if statement with many variables in it.

Such as:

if A, B, C, and D >= 2:
    print (A, B, C, and D)

I realize that this is not the correct syntax and that is exactly the question I am asking — what is the correct Python syntax for this type of an if statement?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
chingchong
  • 493
  • 2
  • 8
  • 11

7 Answers7

52

You want to test a condition for all the variables:

if all(x >= 2 for x in (A, B, C, D)):
    print(A, B, C, D)

This should be helpful if you're testing a long list of variables with the same condition.


If you needed to check:

if A, B, C, or D >= 2:

Then you want to test a condition for any of the variables:

if any(x >= 2 for x in (A, B, C, D)):
    print(A, B, C, D)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Limbo Peng
  • 2,485
  • 19
  • 12
20

Another idea:

if min(A, B, C, D) >= 2:
    print A, B, C, D
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
7

I'd probably write this as

v = A, B, C, D
if all(i >= 2 for i in v):
    print v
DSM
  • 342,061
  • 65
  • 592
  • 494
  • Thanks! To clarify the meaning of what you are telling me... If I had A, B, C, and D. Could I just say v = A, B, C, D (RETURN - can't get the code tag to work) if all(v >= 2): (RETURN) print (v)? – chingchong Dec 27 '11 at 04:30
  • 1
    @TerriMoore: No, because that would be asking if the tuple (A, B, C, D) was greater than or equal to 2, not whether all the values are >= 2. For technical reasons that won't actually raise an Exception in python 2, it'll just give unexpected results, but it will raise an error (as it should) in python 3. – DSM Dec 27 '11 at 04:34
  • I am using python 3. What exactly would the "i in v" thing be looking at? – chingchong Dec 27 '11 at 04:36
  • @LimboPeng's answer is a little more directly implemented, without the intermediate 'v' variable. – PaulMcG Dec 27 '11 at 04:40
  • OK so you recommend Limbo's answer over DSM's? – chingchong Dec 27 '11 at 04:41
3

How about:

if A >= 2 and B >= 2 and C >= 2 and D >= 2:
    print A, B, C, D

For the general case, there isn't a shorter way for indicating that the same condition must be true for all variables - unless you're willing to put the variables in a list, for that take a look at some of the other answers.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
3

If you have ten variables that you are treating as a group like this, you probably want to make them elements of a list, or values in a dictionary, or attributes of an object. For example:

my_dict = {'A': 1, 'B': 2, 'C': 3 }

if all(x > 2 for x in my_dict.values()):
    print "They're all more than two!"
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
2

Depending on what you are trying to accomplish, passing a list to a function may work.

def foo(lst):
    for i in lst:
        if i < 2:
            return
    print lst
Phil
  • 2,080
  • 2
  • 12
  • 13
  • Oops! I've just posted the same answer as yours. LOL – Limbo Peng Dec 27 '11 at 04:17
  • If you could explain what this is doing in more detail I would really help me. Thanks! – chingchong Dec 27 '11 at 04:26
  • You pass the function a list of numbers. For each item in the list, check to see if the item is less than 2 - if the item is less than 2, then the function "returns" (or exits) without printing anything. Continue checking each item in the list. Once each item has been checked, and no item caused the function to return/exit, we know that ALL items are >= 2, so we can print the list. – Phil Dec 27 '11 at 05:03
1

Except that she's probably asking for this:

if A >= 2 and B >= 2 and C >= 2 and D >= 2:
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Dan
  • 10,531
  • 2
  • 36
  • 55