Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Module 7 and 8 Python

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module 7 and 8 Python

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Tuguegarao Archdiocesan Schools System

Saint Joseph’s College of Baggao, Inc.


Baggao, Cagayan, Philippines
Transforming Lives, Shaping the Future

Module No. 7 - 8
DECISION MAKING

Course Title: Python Programming Course IT316


Code:
Instructor: JEIZEL P. MARCELO Term & AY: 1st Sem, AY 2024-
2025
Email Contact No:
Address:

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

III. Learning Resources & References


1. Book References-APA Style
 University of Cape Town and individual contributors, Object-Oriented
Programming in Python, 2013
2. Internet
 Object-Oriented Programming in Python — Object-Oriented
Programming in Python 1 documentation (python-
textbok.readthedocs.io)
 Python Tutorials - Selection Statements | Decision Making | Flow
Controls (btechsmartclass.com)
IV. Lecture Content/Summary of Lesson
In Python, the selection statements are also known as decision making
statements or branching statements. The selection statements are used to
select a part of the program to be executed based on a condition. Python
provides the following selection statements.
 if statement
 if-else statement
 if-elif statement
if statement
This statement executes the program if and only if the condition is true.
And if the condition is false the block of statement is being ignored.
Syntax 1:
if condition:
Statement_1
Statement_2

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
...

Selection Structure with Logical Operator


AND Operator
- used to combine conditional statements and execute the statement if
all the conditions are TRUE
a=200
b=33
c=300
if a>b and c>a: #condition;
print(‘both conditions are TRUE’) #if true;
sample output: both statements are TRUE
OR Operator
- used to combine conditional statements and execute the statement if
one of the conditions is TRUE

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

Program Program Most of the code is All the code is appropriate


Code code is
appropriately indented so that logic is
(Computer) poorly indented so that easily followed
written and logic is easily Focus and tab order
difficult tofollowed. appropriate set.
read. 2pts Most constant and Constant and variable
variable definitions definitions are at the top
are at the top of of procedures
procedures, but not 4pts
all.
Focus and tab
order are mostly
correct, but with
some error.
3pts.
Functionality The program Most of the All the processes function
functions processes function correctly. 6pts
poorly. 2pts correctly but with
some error.
4pts.
VI. Assessment
Quiz 1. Write a python program asks the user to input two positive
numbers and choose one arithmetic operator from the list.
NOTE: use these letters for the operators
A- Addition
B- Subtraction
C- Multiplication
D- Division

Program Program Most of the code is All the code is appropriate


Code code is
appropriately indented so that logic is
(Computer) poorly indented so that easily followed
written and logic is easily Focus and tab order
difficult tofollowed. appropriate set.
read. 2pts Most constant and Constant and variable
variable definitions definitions are at the top
are at the top of of procedures
procedures, but not 4pts
all.
Focus and tab
order are mostly
correct, but with
some error.
3pts.
Functionality The program Most of the All the processes function
functions processes function correctly. 6pts
poorly. 2pts correctly but with
some error.
4pts.

Page 4 of 4

You might also like