Control Structures: Selection Statement
Control Structures: Selection Statement
DAY 3
CONTROL STRUCTURES
SELECTION STATEMENT
PYTHON PROGRAMMING ESSENTIALS
Summary
DAY 2 PYTHON PROGRAMMING ESSENTIALS 2
Variables
Datatype
Type Conversion
Operators & Expressions
String
Numbers
Random
selectively (branch) - making a choice
repetitively (iteratively) - performing a statement over and over, also
called a
Control Structures
DAY 3 PYTHON PROGRAMMING ESSENTIALS 3
A computer can proceed:
in sequence
loop
Some statements are executed only if certain conditions are
met
A condition is represented by a logical (Boolean) expression
that has a value of either true or false
A condition is met if it evaluates to true
Control Structures
DAY 3 PYTHON PROGRAMMING ESSENTIALS 4
statements
will be executed or not i.e if a certain condition is
true then a block of
if Statement
DAY 3 PYTHON PROGRAMMING ESSENTIALS 6
if Statement :
Example DAY 3 PYTHON PROGRAMMING ESSENTIALS 7
if val1 >= 5:
print(f"{val1} is greater than 5") print("I\'m usual statement")
Result
I’m usual statement
if-else Statement
DAY 3 PYTHON PROGRAMMING ESSENTIALS 8
ESSENTIALS 9
print("I’m true")
print("I’m False")
print("I’m outside of if-else statement") Result
3. Sample Output
Enter number: 38
Even number!
Practice 1
DAY 3 PYTHON PROGRAMMING ESSENTIALS num1 = int(input("Enter number: "))
11
result = num1 % 2
if result == 0:
print("Even number!")
else:
print("Odd number!")
1. Create a program that will allow the user to enter 5
digit
2. Determine if the inputted is numerical palindrome.
3.
Numerical palindrome is a number that is the same
forward
positive number.
DAY 3 PYTHON PROGRAMMING ESSENTIALS 12
Exercise 1
Filename: Day3_Exe1_Lastname_Firstname.py
elif (condition):
false
true
conditio
Statement 2
n2
statement
false
n3
. . else: false
true
conditio Statement 4
Statement 3
statement
elif (i == 15):
elif (i == 20):
PROGRAMMING ESSENTIALS 14
i = 20
if (i == 10):
print ("i is 10")
its equivalent
word.
Sample Output
Enter number 1-10: 8
Practice 2
num = int(input("Enter number 1-10: "))
if num == 1:
print("One")
elif num == 2:
print("Two")
DAY 3 PYTHON PROGRAMMING ESSENTIALS 16
elif num == 3:
print("Three")
elif num == 4:
print("Four")
elif num == 5:
print("Five")
elif num == 6:
print("Six")
elif num == 7:
print("Seven")
elif num == 8:
print("Eight")
elif num == 9:
print("Nine")
elif num == 10:
print("Ten")
else:
print("Invalid Range")
Nested if Statement
DAY 3 PYTHON PROGRAMMING ESSENTIALS 17
statement. Nested
if statements means an if statement inside
another if statement. Syntax
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
if (i < 15): # Nested - if statement
PROGRAMMING ESSENTIALS 18
i = 10
if (i == 10): # First if statement
20
Result
True
Compound Conditions
DAY 3 PYTHON PROGRAMMING ESSENTIALS 21
PROGRAMMING ESSENTIALS 22
Sample Output
Enter salesman sale25000
Practice 3
sales = int(input("Enter salesman sale: "))
DAY 3 PYTHON PROGRAMMING ESSENTIALS 23
isupper( )
islower( )
if str1.isupper():
print(str1.lower())
isdigit( )
else:
isalpha( )
print(str1.upper())
d1 = "12"
if d1.isdigit():
print("Its digit")
else:
print("Its text")
Write a program that requests the call number of a book as input and
display the location of the book. Given a table that shows
the location of the book.
DAY 3 PYTHON PROGRAMMING ESSENTIALS 25
Sample Output
Enter book call number 600
Upper Floor
Exercise 2
Filename: Day3_Exe2_Lastname_Firstname.py
if-elif statement
Summary
DAY 3 PYTHON PROGRAMMING ESSENTIALS 26
conditional expressions
simple if statement
if-else statement
nested if
short hand if statement
shorthand if-else statement
compound condition