Computer Systems & Programming (Th)
(CS-1202 )
Lecture 7
Engr. Mansoor Ali Zaheer
Assistant Professor
Department of Mechanical, Industrial &
Energy Systems
University of Sargodha
Operators Used in Decision Making
Arithmetic operators are incapable of generating TRUE or
FALSE. For this we need operators that can result in YES
and NO. In other words, operators that can produce Boolean
output. There are two such operators in Python.
i. Relational or comparison Operators
ii. Logical Operators
Relational or comparison Operators
Operator Name Example
= Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Relational or comparison Operators
Logical Operators
The logical operators apply logic functions (NOT, AND, and inclusive OR) to boolean
arguments. These are helpful to take decisions based on multiple conditions. The following
tables summarize these operators.
Logical Operators
The if() Statement
The if statement evaluates the test expression inside parenthesis. If test expression is evaluated to true,
statements inside the body of if is executed. If test expression is evaluated to false, statements inside the body
of if is skipped. This can be demonstrated with the following flow-chart.
The if() Statement
The if() – else Statements
The if else executes the codes inside the body of if statement if the test expression is true and skips the codes
inside the body of else. If the test expression is false, it executes the codes inside the body of else statement
and skips the codes inside the body of if. The following flow chart and program will help you understand the
idea.
The if() – else Statements
The if() – elif Statement
The elif keyword is Python's way of saying "if the previous conditions were
not true, then try this condition".
The if() – elif () – else() Statement
Occasionally, programs need to decide between a set of given conditions to
perform an operation. This is called ‘Multiple Selection’. In our calculator
program, we performed multiple selection by using multiple if() statements.
This is an improper method. Multiple selection can be performed by using if()
– else if () – else () statement. The following example and flow chart will be
helpful to understand.
The if() – elif () – else() Statement
Example 1 : Write a python program to build a basic calculator with four operators for two
float numbers. Use if()-elif()-else() statements.
The if() – elif () – else() Statement
The if() – elif () – else() Statement
Example 2 : Write a python program to compare two numbers a = 5000 and b = 8000. Use
if()-elif()-else() statements.
The if() – elif () – else() Statement
Example 3 : Write a python program to classify a government college exam scores into
letter grades. It may allow the user to enter their exam score and uses a series of if, elif, and
else statements to determine the corresponding grade based on the score. The grading
criteria of college is as follows.
Percentage Marks Letter Grade
90-100 A
70-89 B
50-69 C
Below 50 F
The if() – elif () – else() Statement
Lab Exercise 5
Task 1: Write a program that asks the user to enter two numbers and then
compare them by using if()-elif()- else() statement.
Task 2: Write a python program to classify UOS exam scores into letter
grades. It may allow the user to enter their exam score and uses a series of if,
elif, and else statements to determine the corresponding grade based on the
score. The UOS grading criteria for Session Fall 19 is as follows.
Percentage Marks Letter Grade
80-100 A
65-79 B
50-64 C
40-49 D
Below 40 F
Lab Exercise Solution 5
Task 1. Write a program that asks the user to enter two numbers and then compare them
by using if elif else statement.
Lab Exercise Solution 5
Task 2. Write a python program to classify UOS exam scores into letter grades. It may
allow the user to enter their exam score and uses a series of if, elif, and else statements to
determine the corresponding grade based on the score. The UOS grading criteria for
Session Fall 19 is as follows.
Percentage Marks Letter Grade
80-100 A
65-79 B
50-64 C
40-49 D
Below 40 F
Lab Exercise Solution 5
Lab Exercise Solution 5