You are on page 1of 36

Python - Decision

Making

04/04/24 Python Programming 1


Review
Write a python program that declares variables to
represent the principal, time, rate and interest of a
simple interest loan. Use any name of your choice.
Accept input from user. Compute and display the
simple interest of the loan to read as follows:

Your loan of …. cedis at a rate of …% per annum, and


over a period of … years has an interest of … cedis.

Note: Interest = (principal * time * rate) / 100

04/04/24 Python Programming 2


Reading Keyboard Input
• Python provides two built-in functions to
read a line of text from standard input, which
by default comes from the keyboard.
• These functions are:
• raw_input
• input

04/04/24 Python Programming 3


The raw_input Function
• The raw_input([prompt]) function reads one line
from standard input and returns it as a string
(removing the trailing newline):

• This would prompt you to enter any string and it


would display same string on the screen.

04/04/24 Python Programming 4


The input Function
• The input([prompt]) function is equivalent to
raw_input, except that it assumes the input is a
valid Python expression and returns the evaluated
result to you

• This would produce following result against the


entered input:

04/04/24 Python Programming 5


Decision making
• It is anticipation of conditions
occurring while execution of
the program and specifying
actions taken according to the
conditions.
• Decision structures evaluate
multiple expressions which
produce TRUE or FALSE as
outcome.

04/04/24 Python Programming 6


Types of decision making
statements
S/N Statement & Description
1 if statements
An if statement consists of a boolean expression
followed by one or more statements.
2 if...else statements
An if statement can be followed by an optional else
statement, which executes when the boolean expression
is FALSE.
3 nested if statements
You can use one if or else if statement inside
another if or else if statement(s).
04/04/24 Python Programming 7
Python IF Statement
• The if statement contains a logical expression
using which data is compared and a decision
is made based on the result of the
comparison.
• Syntax:

04/04/24 Python Programming 8


Python IF...ELIF...ELSE
Statements
• An else statement can be combined with an
if statement.
• An else statement contains the block of code
that executes if the conditional expression in
the if statement resolves to 0 or a FALSE
value.
• Syntax:

04/04/24 Python Programming 9


The elif Statement
• The elif statement allows you to check multiple
expressions for TRUE and execute a block of code
as soon as one of the conditions evaluates to TRUE.
• Similar to the else, unlike else, for which there can
be at most one statement, there can be an arbitrary
number of elif statements following an if.
• Syntax:

04/04/24 Python Programming 10


Python nested IF statements
• In a nested if construct, you can have an
if...elif...else construct inside another if...elif...else
construct.
• Syntax:

04/04/24 Python Programming 11


Python - Loops

04/04/24 Python Programming 12


Loops
• In general, statements are executed sequentially:
The first statement in a function is executed first,
followed by the second, and so on.
• There may be a situation when you need to
execute a block of code several number of times.
• Programming languages provide various control
structures that allow for more complicated
execution paths.
• A loop statement allows us to execute a
statement or group of statements multiple times.

04/04/24 Python Programming 13


S/N Loop Type & Description

1 while loop

Repeats a statement or group of statements while a given condition


is TRUE. It tests the condition before executing the loop body.

2 for loop

Executes a sequence of statements multiple times and abbreviates


the code that manages the loop variable.
3 nested loops

You can use one or more loop inside any another while, for or
do..while loop.

04/04/24 Python Programming 14


Python while Loop Statements
• Syntax:

• The continue-condition must appear inside the


parentheses. It is always evaluated before the
loop body is executed.
• If condition is true, the loop body is executed,
otherwise the loop terminates
04/04/24 Python Programming 15
loop False
continuation
condition

True

Statement(s)
(loop body)

04/04/24 Python Programming 16


04/04/24 Python Programming 17
The Infinite Loop
• A loop becomes infinite loop if a condition
never becomes FALSE.
• This results in a loop that never ends. Such a
loop is called an infinite loop.
• An infinite loop might be useful in
client/server programming where the server
needs to run continuously so that client
programs can communicate with it as and
when required.
04/04/24 Python Programming 18
04/04/24 Python Programming 19
Using else Statement with While
Loop
• Python supports to have an else statement
associated with a loop statement
• If the else statement is used with a while loop, the
else statement is executed when the condition
becomes false.

04/04/24 Python Programming 20


Single Statement Suites
• if your while clause consists only of a single
statement, it may be placed on the same line
as the while header.

04/04/24 Python Programming 21


04/04/24 Python Programming 22
04/04/24 Python Programming 23
Python for Loop Statements
• Syntax:

• If a sequence contains an expression list, it is


evaluated first. Then, the first item in the sequence is
assigned to the iterating variable iterating_var.
• Next, the statements block is executed. Each item in
the list is assigned to iterating_var, and the
statement(s) block is executed until the entire
sequence is exhausted.
04/04/24 Python Programming 24
initial action

loop False
continuation
condition?

True
Statement(s)
(loop body)

action after each iteration

04/04/24 Python Programming 25


04/04/24 Python Programming 26
Iterating by Sequence Index
• An alternative way of iterating through each
item is by index offset into the sequence
itself.

04/04/24 Python Programming 27


Using else Statement with For
Loop
• Python supports to have an else statement
associated with a loop statement.
• If the else statement is used with a for loop,
the else statement is executed when the
loop has exhausted iterating the list.
• The following example illustrates the
combination of an else statement with a for
statement that searches for prime numbers
from 10 through 20.
04/04/24 Python Programming 28
04/04/24 Python Programming 29
Python nested loop
• Python programming language allows to use
one loop inside another loop.

04/04/24 Python Programming 30


04/04/24 Python Programming 31
Loop Control Statements
• Loop control statements change execution from its
normal sequence.
S/N Control Statement & Description

1 break statement

Terminates the loop statement and transfers execution to the statement


immediately following the loop.
2 continue statement

Causes the loop to skip the remainder of its body and immediately retest its
condition prior to reiterating.
3 pass statement

The pass statement in Python is used when a statement is required


syntactically but you do not want any command or code to execute.
04/04/24 Python Programming 32
break statement

04/04/24 Python Programming 33


continue statement

04/04/24 Python Programming 34


pass statement

04/04/24 Python Programming 35


Assignment
• Write a program that reads an unspecified number
of integers, determines how many positive and
negative values have been read, and computes the
total and average of the input values (not counting
zeros). Your program ends with the input 0.

• Display the average as a floating-point number.


Here is a sample run:

04/04/24 Python Programming 36

You might also like