Python Conditions
& Selection
Preparatory Year Program PYP-002
Announcement
Quiz 02
Week 09 (21 Oct – 24 Oct) (During class time)
Chapter 02 and 03 from LAB Manual
Computer Networks and Cybersecurity
Blockchain Networks, IoT and Immersive
Technologies
MCQ’s
Presentation 02
Week 10 (28 Oct – 31 Oct)
Same groups assigned
Presentation topics posted in blackboard
2
Commenting in python
Comments are written in programming languages
to explain, clarify, or collaborate in your code.
In python, the symbol ‘#’ is used to write
comments.
3
Lists in python
A list is a linear data structure that contains some
related data
If all the data is of the same type it is called an
array
temp = [35, 15, 45, 28]
List indexing starts from 0
To access second element
temp[1]
4
Example
What will be the output of the following
temp = [35, 15, 45, 28]
temp[0] = temp[2] + 6
temp[3] = temp[3] / 4
temp[2] = temp[1] / 3
temp 51 0
15 1
5 2
7 3
5
Conditional Statements in
python
In programming languages conditional
statements change the sequential flow of an
algorithm
Sequential Flow Conditional statements
introduce branches
6
if condition
In algorithm, the general form of the if condition is
as follows:
7
if condition
if keyword in python is case-sensitive (cannot write a condition with
capital letters i.e. IF)
“condition” is boolean expression that evaluates to TRUE or FALSE
“Statement 1”, “Statement 2” , “Statement 3”….., are part of the
BLOCK that will be executed if the condition is TRUE
All the statements in the block must be indented with the same
number of spaces (minimum one space required)
Google colab inserts two spaces by default
Offline python inserts four spaces by default
8
Example
What will be the output of the following block of
codes
var= 2
if var < 4: Statement 1
print ('Statement 1') Statement 2
print ('Statement 2')
var= 6
if var < 4:
print ('Statement 1’) Statement 3
print ('Statement 2')
print ('Statement 3')
var= 7
if var < 4: error
print ('Statement 1')
print ('Statement 2')
After if there must be at
least one statement with
indentation
9
Boolean Expressions
Relational Operators Python Example
representation
Greater than > x>y
Less than < x<y
Equals == x == y
Greater than or equal to >= x >= y
Less than or equal to <= x <= y
Not Equals != x != y
10
Logical Operators
Boolean expressions output are either TRUE or
FALSE
Logical operators have both inputs and output in
the form of TRUE or FALSE
Logical operators can be used to build more
complicated conditions
A B A and B A or B not A
False False False False True
False True False True True
True False False True False
True True True True False
11
Logical Operators (Example)
x=15
y=20
z=30
print((x>y or y<z))
print((x>y and y<z))
print((x>y or y>z)and(x<z))
print(not(x>y) or y<z)
print(y<x or z!=40)
Expression Value
print((x>y or y<z)) True
print((x>y and y<z)) False
print((x>y or y>z)and(x<z)) False
print(not(x>y) or y<z) True
print(y<x or z!=40) True
12
Exercise 1
Write a program in python that reads grade and
absence of a student. If the absence is greater
than zero, the script must subtract the absence
from the grade. Then, the updated grade should
be displayed.
13
Exercise 1
grade=int(input('Enter grade:'))
abs=int(input('Enter number of absences:'))
if abs > 0:
grade=grade-abs
print('The updated grade is:',grade)
14
if-else condition
In algorithm, the general form of the if-else
condition is as follows:
15
Exercise 2
Write a python program that reads a student's
grade. If the grade is greater than or equal to
sixty, the program displays the message “PASS”.
Otherwise, it displays the message “FAIL”.
16
Exercise 2
grade=int(input('Enter your grade:'))
if grade >= 60:
print('Pass')
else:
print('Fail')
17
Exercise 3
Write a program that asks for an integer number
and finds out if the number is even or odd. The
program should print a message saying the
number is Even or Odd
18
Exercise 3
number=int(input('Enter a number:'))
if number % 2 == 0:
print('The number’,number,’is even')
else:
print('The number’,number,’is odd')
19
Nested if-else (Exercise 4)
Write a program that asks to enter three numbers
and prints the maximum of them.
x=int(input('Enter x'))
y=int(input('Enter y'))
z=int(input('Enter z')) Start
if x>y:
if x>z:
print('maximum=',x) Input x, y, x
else:
False True
x>y
False True False True
y>z x>z
maximum = z maximum = y maximum = z maximum = x
20
elif condition
To avoid indentation errors of nested if-else
python introduces the elif construct
21
Exercise 5
Use a nested if-else construct to write a program
that reads a student’s GPA out of 4.0. The script
should display a message according to the
following conditions:
Condition Message
GPA<0 or GPA>4.0 OUT OF RANGE
GPA >= 3.5 EXCELLENT
3.5 > GPA >= 3.0 VERY GOOD
3.0 > GPA >= 2.5 GOOD
2.5 > GPA >= 2.0 FAIR
GPA < 2.0 POOR
22
Exercise 5
Solution
GPA=float(input('Enter GPA:'))
if GPA >4 or GPA <0:
print('Out of Range')
elif GPA >=3.5:
print('Excellent')
elif GPA >=3.0:
print('Very Good')
elif GPA >=2.5:
print('Good')
elif GPA >=2.0:
print('Fair')
else:
print('Poor')
23
Exercise 6
Write a program in python that calculates the
shipping cost of a package based on the weight
(w) and destination (d).
Destination Weight in KG Cost (SAR)
w<1 30
UAE 1 <= w < 3 50
w >=3 100
w<1 20
Bahrain 1 <= w < 3 30
w >=3 80
w<1 10
Saudi Arabia 1 <= w < 3 20
w >=3 50
24
Exercise 6
Complete the following program
w=float(input('Enter weight:'))
d=input('Enter destination')
if d == 'UAE':
if w < 1:
print('cost 30')
elif w < 3:
print('cost 50')
else:
print('cost 100')
elif d == 'Bahrain':
25