Module 7 and 8 Python
Module 7 and 8 Python
Module No. 7 - 8
DECISION MAKING
I. Overview
In selection control structures, conditional statements are features of a
programming language which perform different computations or actions
depending on whether a programmer-specified Boolean condition evaluates to
true or false
In Python, the selection statements are also known as Decision control
statements or branching statements.
The selection statement allows a program to test several conditions and
execute instructions based on which condition is true.
.
II. Intended Learning Outcome (ILOs)
At the end of the lesson, the student should be able to:
a. understands the different types of selection statement
b. apply logical operators on selection structure
c. practice nested conditions
Page 1 of 4
Tuguegarao Archdiocesan Schools System
Saint Joseph’s College of Baggao, Inc.
Baggao, Cagayan, Philippines
Transforming Lives, Shaping the Future
Statement_3
...
age=20
if age>=18: #condition; asking if age is greater or equal
to 18
print('Legal age')
output: Legal age
Syntax 2:
if condition: statement
age=20
if age>=18:print('Legal age')
output: Legal age
NOTE: You can only use syntax 2 if you need to execute only one statement
for the condition
if-else statement
The else keyword catches anything which isn't caught by the preceding
conditions.
This is use if the results of all the conditions are false.
Syntax 1:
if condition:
Statement_1
Statement_2
Statement_3
...
else:
Statement_1
Statement_2
...
age=12
if age>=18: #condition; asking if age is greater or equal
to 18
print('Legal age') #if true; display Legal age
else:
print(‘Under age’) #if false; display Under age
output: Under age
Syntax 2:
Statement for TRUE if condition else statement for FALSE
age=12
print('Legal age') if age>=18 else print(‘Under age’)
output: Under age
if-elif statement
• "if the previous conditions were not true, then try this condition"
• This statement is use if there are 2 or more conditions.
Synatx
if condition_1:
Statement_1
Statement_2
Statement_3
...
elif condition_2:
Page 2 of 4
Tuguegarao Archdiocesan Schools System
Saint Joseph’s College of Baggao, Inc.
Baggao, Cagayan, Philippines
Transforming Lives, Shaping the Future
Statement_4
Statement_5
Statement_6
...
else:
Statement_7
Statement_8
...
a=200
b=33
c=300
if a>b or a>c: #condition;
print(‘one of the condition is TRUE’) #if true;
sample output: one of the conditions is TRUE
Nested Condition
This statement happens when a selection structure is place inside another
selection structure
x=41
if x>0 and x<=100: #ask if the value of x is
from 1-100
print(‘the value of x is within the range’) #if true;
if x>20: #then ask again if the
value of x > 20
print(‘and also, above 20’) #if true
else:
print(‘but not above 20’) #if false
sample output:
the value of x is within the range
and also, above 20
V. Learning Activities
Exercise 1. Write a python program that allows the user to enter two
numbers. Perform addition if the first number is lesser than the second
number. Otherwise, perform subtraction
Exercise 2. Create a python program that allow the user to enter his/her
age. Identify if the input age is valid or not. If not valid display “invalid
age” otherwise categorized the user according to his/her input age.
0-14 years old: Child
15-24 years old: Youth
25-64: Adult
Page 3 of 4
Tuguegarao Archdiocesan Schools System
Saint Joseph’s College of Baggao, Inc.
Baggao, Cagayan, Philippines
Transforming Lives, Shaping the Future
65- above: Senior
Page 4 of 4