Evaluate multiple variables in one 'if' statement?

前端 未结 8 2269
后悔当初
后悔当初 2020-12-05 15:50

Say I have a bunch of variables that are either True or False. I want to evaluate a set of these variables in one if statement to see if they are

8条回答
  •  温柔的废话
    2020-12-05 15:51

    You could do:

    if var1 == var2 == var3 == var4 == False:
      do_stuff()
    

    But, if the variables evaluate to true or false, you could also do this:

    if var1 and var2 and var3 and var4:
      do_stuff()
    

    Or

    if all([var1, var2, var3, var4]):
      do_stuff()
    

提交回复
热议问题