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
age
=
[on_true] if [expression] else [on_false]0
[on_true] if [expression] else [on_false]1
[on_true] if [expression] else [on_false]2
=
[on_true] if [expression] else [on_false]4
[on_true] if [expression] else [on_false]5
[on_true] if [expression] else [on_false]6
[on_true] if [expression] else [on_false]7
=
[on_true] if [expression] else [on_false]9
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code20
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code24
[on_true] if [expression] else [on_false]5
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code26
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code27
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
SORRY ! YOU ARE NOT ALLOWED. BYE !1
[on_true] if [expression] else [on_false]5
Output:
SORRY ! YOU ARE NOT ALLOWED. BYE !
PROGRAM 2:
program that checks the agreement of the user to the terms
SORRY ! YOU ARE NOT ALLOWED. BYE !3
=
SORRY ! YOU ARE NOT ALLOWED. BYE !5
[on_true] if [expression] else [on_false]1
SORRY ! YOU ARE NOT ALLOWED. BYE !7
=
=
PROGRAM 2:0
PROGRAM 2:1
SORRY ! YOU ARE NOT ALLOWED. BYE !3
=
=
PROGRAM 2:5
PROGRAM 2:6
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
YOU SAID NO0
[on_true] if [expression] else [on_false]5
YOU SAID NO2
SORRY ! YOU ARE NOT ALLOWED. BYE !7
=
=
SORRY ! YOU ARE NOT ALLOWED. BYE !5
PROGRAM 2:1
SORRY ! YOU ARE NOT ALLOWED. BYE !3
=
=
9 is the largest1
PROGRAM 2:6
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
9 is the largest6
[on_true] if [expression] else [on_false]5
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code26
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code27
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
working3
[on_true] if [expression] else [on_false]5
Output:
YOU SAID NO
PROGRAM 3: program to compare the entered three numbers
working5
=
working7
working8
=
age
0age
1=
age
3
[on_true] if [expression] else [on_false]1
age
5[on_true] if [expression] else [on_false]6
age
7[on_true] if [expression] else [on_false]6
age
9=
working8
[on_true] if [expression] else [on_false]6
=
3=
=
5if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
=
8=
9[on_true] if [expression] else [on_false]5
YOU SAID NO2
[on_true] if [expression] else [on_false]02
[on_true] if [expression] else [on_false]6
[on_true] if [expression] else [on_false]04
[on_true] if [expression] else [on_false]6
[on_true] if [expression] else [on_false]06
=
working5
[on_true] if [expression] else [on_false]6
[on_true] if [expression] else [on_false]10
=
=
5if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
[on_true] if [expression] else [on_false]15
=
9[on_true] if [expression] else [on_false]5
YOU SAID NO2
[on_true] if [expression] else [on_false]19
[on_true] if [expression] else [on_false]6
[on_true] if [expression] else [on_false]21
[on_true] if [expression] else [on_false]6
[on_true] if [expression] else [on_false]23
=
working5
[on_true] if [expression] else [on_false]6
[on_true] if [expression] else [on_false]27
=
[on_true] if [expression] else [on_false]29
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
[on_true] if [expression] else [on_false]32
=
9[on_true] if [expression] else [on_false]5
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code26
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code27
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code21
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code22
if [cond1 AND/OR COND2] AND/OR [cond3 AND/OR cond4]: code1 else: code23
[on_true] if [expression] else [on_false]40
[on_true] if [expression] else [on_false]5
Output:
9 is the largest
Not just two conditions we can check more than that by using ‘and’ and ‘or’.
PROGRAM 4:
working5
=
[on_true] if [expression] else [on_false]44
working8
=
[on_true] if [expression] else [on_false]44
age
1=
[on_true] if [expression] else [on_false]44
[on_true] if [expression] else [on_false]1
[on_true] if [expression] else [on_false]52
=
=
[on_true] if [expression] else [on_false]44
[on_true] if [expression] else [on_false]6
working8
=
=
[on_true] if [expression] else [on_false]44
[on_true] if [expression] else [on_false]6
age
1=
=
[on_true] if [expression] else [on_false]44
PROGRAM 2:6