DAY 3 PYTHON PROGRAMMING ESSENTIALS 1
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
Selection Structure (Decision
Statement) DAY 3 PYTHON PROGRAMMING ESSENTIALS 5
Decision making statements in programming languages decides
the direction of flow of program execution. Decision making
statements available in python are:
if statement
if..else statements
if-elif ladder
nested if statements
Short Hand if statement
Short Hand if-else statement
No switch statement in python
It is used to decide whether a certain statement or block of
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 is the most simple decision making statement.
statement is executed otherwise not.
Syntax
if condition:
# Statement1
# Statement2
if Statement :
Example DAY 3 PYTHON PROGRAMMING ESSENTIALS 7
# python program to illustrate If statement val1 = 4
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
If the value of the
expression is true,
statement on true is
executed otherwise
statement on false is executed
Syntax
if (condition):
# statement if TRUE
else:
# statement if FALSE
num = 20;
if num < 15:
print("num is smaller than 15")
else:
print("num is greater than 15")
if-else Statement : Example DAY 3 PYTHON PROGRAMMING
ESSENTIALS 9
print("I’m true")
print("I’m False")
print("I’m outside of if-else statement") Result
num is greater than 15
I’m False
I’m outside of if-else statement
1. Create a program that will determine if the
DAY 3 PYTHON PROGRAMMING ESSENTIALS 10
inputted positive number
2. Display the correct message.
is even or odd number.
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
and backward such as 13431
Enter 5 digit number: 12321
It’s a NUMERICAL PALINDROME
Exercise 1
Filename: Day3_Exe1_Lastname_Firstname.py
elif (condition):
if-elif-else Statement DAY 3 PYTHON PROGRAMMING ESSENTIALS
conditio
13
Here, a user can decide among multiple options. The if statements are executed from
the top down. As soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the ladder is bypassed. If none of the
conditions is true, then the final else statement will be executed.
Syntax true
Statement 1
if (condition): statement n1
false
true
conditio
Statement 2
n2
statement
false
n3
. . else: false
true
conditio Statement 4
Statement 3
statement
elif (i == 15):
elif (i == 20):
if-elif-else Statement : Example DAY 3 PYTHON
PROGRAMMING ESSENTIALS 14
i = 20
if (i == 10):
print ("i is 10")
print ("i is 15")
print ("i is 20")
else:
print ("i is not present")
Result
i is 20
Let the user enter a number from 1 to 10 then display
DAY 3 PYTHON PROGRAMMING ESSENTIALS 15
its equivalent
word.
Sample Output
Enter number 1-10: 8
The equivalent word is Eight
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
A nested if is an if statement that is the target of another if
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
Nested if Statement : Example DAY 3 PYTHON
PROGRAMMING ESSENTIALS 18
i = 10
if (i == 10): # First if statement
print ("i is smaller than 15")
if (i < 12):
print ("i is smaller than 12 too")
else:
print ("i is greater than 15")
Result
i is smaller than 15
i is smaller than 12 too
Short Hand if Statement DAY 3 PYTHON PROGRAMMING ESSENTIALS 19
Whenever there is only a single statement to be executed inside
the if block then shorthand if can be used. The statement can be put
on the same line as
the if statement. Syntax
if condition: statement
Example
# Python program to illustrate short hand if i = 10
if i < 15: print("i is less than 15") Result
i is less than 15
print(True) if i < 15 else print(False)
Short Hand if-else Statement DAY 3 PYTHON PROGRAMMING ESSENTIALS
20
This can be used to write the if-else statements in a single line
where there is
only one statement to be executed in both if and else
block. Syntax
statement_when_True if condition else
statement_when_FalseExample
# Python program to illustrate short hand if i = 10
Result
True
Compound Conditions
DAY 3 PYTHON PROGRAMMING ESSENTIALS 21
Have more than 1 conditional expression
The result of the compound expression depends on the individual
result of each condition
Use logical operators on these conditional expressions if
value1 >= 1 and value <=10: print(“Valid range”)
Sale Value Commission Up to $100 Zero
over $100 to $1000 2%
over $1000 3%
The commission is 750
A salesman is paid a commission on the following basis DAY 3 PYTHON
PROGRAMMING ESSENTIALS 22
Sample Output
Enter salesman sale25000
Practice 3
sales = int(input("Enter salesman sale: "))
DAY 3 PYTHON PROGRAMMING ESSENTIALS 23
if sales >= 1 and sales <=100:
commission = 0
elif sales >=101 and sales <=1000:
commission = float(sales * 0.02)
else:
commission = float(sales * 0.03)
print(f"The commission is {commission}")
String Methods for Conditions DAY 3 PYTHON PROGRAMMING ESSENTIALS
str1 = "ROGIE"
24
isupper( )
islower( )
if [Link]():
print([Link]())
isdigit( )
else:
isalpha( )
print([Link]())
d1 = "12"
if [Link]():
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
Call Numbers Location 100 to 199 Basement 200 to 500 and
over 900 Main Floor 501 to 900 except 700 to 750 Upper
Floor 700 to 750 Archives
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