Professional Documents
Culture Documents
Session 3
هيئة االرصاد
Python Conditional Statements: If_else, Elif, Nested If Statement
If ( EXPRESSION == TRUE ):
Block of code
else:
Block of code
if statements: flowchart
Example1: if statement
#2) if-else statements
• The statement itself says if a given condition is true then execute the
statements present inside the “if block” and if the condition is false
then execute the “else” block.
• The “else” block will execute only when the condition becomes false.
It is the block where you will perform some actions when the
condition is not true.
• if-else statement evaluates the Boolean expression. If the condition is
TRUE then, the code present in the “ if “ block will be executed
otherwise the code of the “else“ block will be executed
#2) if-else statements
#2) if-else statements
#2) if-else statements
#2) if-else statements
#3) elif statements
#3) elif statements
Nested if-else Syntax:
#5) elif Ladder
In Line if-Statement
In Line if:Else-Statement
IF and List operations
test_list = [ 1, 6, 3, 5, 3, 4 ]
# Checking if 4 exists in list using (in)
if (4 in test_list):
print ("Element Exists")
Accept entries from users
• When input() function executes program flow will be stopped until the user has
given an input.
• The text or message display on the output screen to ask a user to enter input
value is optional i.e. the prompt, will be printed on the screen is optional.
Example 1
print('Enter your name:')
x = input()
print('Hello, ' + x)
Example 2
x = input('Enter your name:')
print('Hello, ' + x)
Assignment
# Write a program that can accept a score of student from user, then
print a result according to the following rule:
90 to 100 “Excellent”
75 and less than 90 “v Good”
60 and less than 75 “Good”
Greater than 50 and less than 60 “Pass”
less than 50 “Fail”