Check multiple conditions in if statement – Python

If-else conditional statement is used in Python when a situation leads to two conditions and one of them should hold true.

Syntax:

if (condition):
    code1
else:
    code2
[on_true] if [expression] else [on_false]

Note: For more information, refer to Decision Making in Python (if , if..else, Nested if, if-elif)

Multiple conditions in if statement

Here we’ll study how can we check multiple conditions in a single if statement. This can be done by using ‘and’ or ‘or’ or BOTH in a single statement.

Syntax:



if (cond1 AND/OR COND2) AND/OR (cond3 AND/OR cond4):
    code1
else:
    code2
  • and comparison = for this to work normally both conditions provided with should be true. If the first condition falls false, the compiler doesn’t check the second one. If the first condition is true and the compiler moves to the second and if the second comes out to be false, false is returned to the if statement.
  • or Comparison = for this to work normally either condition needs to be true. The compiler checks the first condition first and if that turns out to be true, the compiler runs the assigned code and the second condition is not evaluated. If the first condition turns out to be false, the compiler checks the second, if that is true the assigned code runs but if that fails too, false is returned to the if statement.

The following examples will help understand this better:
PROGRAM 1: program that grants access only to kids aged between 8-12

filter_none

edit
close

play_arrow

link
brightness_4
code

age = 18
  
if ((age>= 8) and (age<= 12)):
    print("YOU ARE ALLOWED. WELCOME !")
else:
    print("SORRY ! YOU ARE NOT ALLOWED. BYE !")

chevron_right


Output:

SORRY ! YOU ARE NOT ALLOWED. BYE !
PROGRAM 2:

program that checks the agreement of the user to the terms

filter_none

edit
close

play_arrow

link
brightness_4
code

var = 'N'
  
if (var =='Y' or var =='y'):
    print("YOU SAID YES")
elif(var =='N' or var =='n'):
    print("YOU SAID NO")
else:
    print("INVALID INPUT")

chevron_right


Output:

YOU SAID NO

PROGRAM 3: program to compare the entered three numbers

filter_none

edit
close

play_arrow

link
brightness_4
code

a = 7
b = 9
c = 3
  
  
if((a>b and a>c) and (a != b and a != c)):
    print(a, " is the largest")
elif((b>a and b>c) and (b != a and b != c)):
    print(b, " is the largest")
elif((c>a and c>b) and (c != a and c != b)):
    print(c, " is the largest")
else:
    print("entered numbers are equal")

chevron_right


Output:

9  is the largest

Not just two conditions we can check more than that by using ‘and’ and ‘or’.
PROGRAM 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

a = 1
b = 1
c = 1
if(a == 1 and b == 1 and c == 1):
    print("working")
else:
    print("stopped")

chevron_right


Output:

working

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.