z
Python Conditional
Statements
z
Python Conditional Statements,
Decision making in Python
programming, Python if
statement, Python else
statement, and Python elif
statement.
z
Python Language Conditional
Statements
Conditional Statements are part of
control flow statements in computer
programming
The control flow is the order in which the
computer executes statements in a script.
z
We have three types of Control
Flow Statements in Python:
1. Conditional
Statements
2. Loop Statements
3. Branching Statements
z
in Python language also we can find these 3 types of statements
Generally in any Programming language, conditional Statements
are 2 types…
1. If Statement
2. Switch Statement
but in Python no Switch statement, only if the statement
z
Usage of Decision making/Conditional
Statements
1. Run a block of statements when a condition is true
2. Run a block of statements when a condition is true, otherwise, run
another block of statements
3. Run a block of statements when a compound condition is true
4. Run a block of statements when a compound condition is true,
otherwise, run another block of statements
5. Decide among several alternates (elif)
6. Run a block of statements when more than one condition is true
(nested if)
z
1. Run a block of statements when a
condition is true
Example:
a=100
b=900
if (a>b):
print(“A is Big Number”)
z
2. Run a block of statements when a condition is
true, otherwise, run another block of statements
EXAMPLE:
a=100
b=900
if (a>b):
print(“A is Big Number”)
else:
print(“B is Big Number”)
z
True or False
10 > 20
10.234 > 10345
“Philippines” > “China”
z
EXAMPLE:
a=”Philippines”
b=”China”
if (a>b):
print(“Philippines is Big”)
else:
print(“China is Big”)
z
3. Run a block of statements when a
compound condition is true
EXAMPLE:
a=100
b=90
c=80
if ((a>b) and (a>c)):
print (“A is Big Number”)
z
3. Run a block of statements when a
compound condition is true
EXAMPLE:
a, b, c = 100, 90, 80
if ((a>b) and (a>c)):
print (“A is Big Number”)
z
4. Run a block of statements when a compound
condition is true, otherwise run another block of
statements
a=100
b=90
c=800
d=70
if ((a>b) and (a>c) and (a>d)):
print (“A is Big Number”)
else:
print (“A is Not Big Number”)
z
4. Run a block of statements when a compound
condition is true, otherwise run another block of
statements
a=100
b=90
c=800
d=70
if ((a>b) or (a>c) or (a>d)):
print (“A is Big Number”)
else:
print (“A is Not Big Number”)
z
5. Decide among several alternates(elif)
if (condition):
Statement(s)
elif (condition):
Statement(s)
elif (condition):
Statement(s)
else:
Statement((s)
z Example:
a=0
if ((a>=1) and (a<=100)):
print (“A is a Small Number”)
elif ((a>100) and (a<=1000)):
print (“A is a Medium Number”)
elif ((a>1000) and (a<=10000)):
print (“A is a Big Number”)
elif (a>10000):
print (“A is a High Number”)
else:
print (“A is either Zero or Negative Number”)
z
5. Execute a block of Statements when
more than one condition is true (Nested if)
syntax:
if (condition){
if (condition){
if (condition){
Statements
z
5. Execute a block of Statements when
more than one condition is true (Nested if)
syntax:
if (condition){
if (condition){
if (condition){
Statements
z
5. Execute a block of Statements when
more than one condition is true (Nested if)
a, b, c, d = 100, 90, 70, 500
if (a>b):
if (a>c):
if (a>d):
print (“A is a Big Number”)
else :
print (“A is Not a Big Number”)
z
Using Compound Condition
a, b, c, d = 100, 90, 70, 50;
if ((a>b) and (a>c) and (a>d)):
print (“A is a Big Number”)
else:
print (“A is Not a Big Number”)
z
Problem: Find the biggest variable (Integer variables)
among 4 variables
Use Compound Condition and else if…
a=100
b=90
c=70
d=50
z
Problem: Find the biggest variable (Integer variables)
among 4 variables
Use Compound Condition and else if…
if ((_____) and (______ and (_____)):
print (“__________”)
elif ((______) and (______) and (_____)):
print (“___________”)
elif ((_____) and (______) and (______)):
print (“___________”)
else:
print (“_____________”)
z
Problem: Find the biggest variable (Integer variables)
among 4 variables
Use Compound Condition and else if…
if ((a>b) and (a>c) and (a>d)):
print (“A is a Big Number”)
elif ((b>a) and (b>c) and (b>d)):
print (“B is a Big Number”)
elif ((c>a) and (c>b) and (c>d)):
print (“C is a Big Number”)
else:
print (“D is a Big Number”)