You are on page 1of 29

Python Basic Course

Session 3
‫هيئة االرصاد‬
Python Conditional Statements: If_else, Elif, Nested If Statement

• What You Will Learn: 


• Python If Statement Video Tutorials
• Installation Of Pycharm
• Conditional Statements In Python
• #1) if statements
• #2) if-else statements
• #3) elif statements
• #4) Nested if-else statements
• #5) elif Ladder
• Python If Statement In One Line
• If-else Statements In One Line
• Elif Statements In One Line
• Multiple Conditions In If Statements
#1) if statements
• Python if statement is one of the most commonly used conditional
statements in programming languages. It decides whether certain
statements need to be executed or not. It checks for a given
condition, if the condition is true, then the set of code present inside
the ” if ” block will be executed otherwise not.
• The if condition evaluates a Boolean expression and executes the
block of code only when the Boolean expression becomes TRUE.
Syntax:

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”

You might also like