0% found this document useful (0 votes)
3 views

08 If Statements

The document is an educational resource from UNESCO UNITWIN focused on teaching conditional statements in Python programming, specifically the 'if', 'if-else', and 'if-elif' constructs. It includes definitions, examples, and exercises to illustrate how to use these statements effectively in coding. Additionally, it emphasizes the importance of indentation and structure in Python syntax.

Uploaded by

Thae Thae Aung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

08 If Statements

The document is an educational resource from UNESCO UNITWIN focused on teaching conditional statements in Python programming, specifically the 'if', 'if-else', and 'if-elif' constructs. It includes definitions, examples, and exercises to illustrate how to use these statements effectively in coding. Additionally, it emphasizes the importance of indentation and structure in Python syntax.

Uploaded by

Thae Thae Aung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

UNESCO UNITWIN

Online Course
Supported by

Handong Global University


Ministry of Education, Korea

* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational materials including the video and other
relevant materials for commercial and for-profit use.
If Statement
08
Global Leadership School
Handong Global University
Learning Objectives
• Learn when to use conditionals

• If statement
• if
• if ~ else
• if ~ elif

• Learn 3 types of if statements using simple


examples
What are Conditional Statements?
• When writing a program,
• It is used when the process to be implemented under
certain conditions should be different
if x > 0 :
print("x is positive“ )

• Statement that comes after conditional statement is called


“condition”
• In python,
• Does not need parentheses in conditional statements
• Colon(:) is used to mark the end of a condition
• After conditional statement, next lines should be indented
• IDLE does indentation automatically
Types of Conditionals
• Only execute when a condition is true
if condition:
statement
• Only execute when a condition is either true or false
if condition:
statement
else:
statement
• Handle multiple situations with three or more possibilities
If condition:
statement
elif condition:
statement
else:
statement
if Statement
• Ask a condition and executed if it’s true like
seen in the flow chart
• If True, indented lines are executed
• If False, nothing happens

N Condition Y
?

statements
How to use if statement
• If statements are made of a “block” that contains
a conditional clause and execution statement
• Colon must always follow after a condition
• Executed statements must be written on the next line
• The execution statement can be one or multiple lines
• If the execution statement is multi-line, the
indentation must match
• Indented executed statements that come after a
conditional statement is called “block”
• The block of a conditional is also called its body
if Statement, Example
>>> count = 10
>>> if count >=10 : ## Ends with colon

count = 0
Indent print(“=“ * 25)
print(“Restart from the beginning”)
print(“=“ * 25)

Indented execution statement block


if ~ else Statement
• Different statement is executed depending on
the condition
• Depending on the condition
• If True, statement1 is executed
• If False, statement2 under else is executed

Y Condition N
?

statement1 statement2
If ~ else Statement, Example
count = 10
if count >=10 :
count = 0
print("=" * 25)
print(”Restart from beginning")
print("=" * 25)

else :
count = count + 1
print(”Proceeding to next stage")
If ~ else Statement
if x%2 == 0 :
print( x, "is even“)
else :
print( x, "is odd“)

• When divide x by 2, if the remainder is 0, x is an even num


ber
• It prints “is even”
• If x is odd,
• When x is divided by 2, remainder is 1. Therefore, the clause is fal
se.
• It prints “is odd”
• Colon(:) must come after “else” and “if”
Conditional Statement Example
# variables for if statement
a=4
b=5
c=6

# Simple comparison
if a < b :
print("a is less than b")

if a > b :
print("a is greater than b")

if a <= b :
print("a is less than or equal to b")
else :
print("a is greater than b")

if a == b :
print("a is equal to b")
else :
print("a and b are not equal")
If ~ elif Statement
• Used when a condition can be divided into
more than two cases if condition :
statement
• Conditions are divided into sections elif condition :
• Grades, commission rate in real-estate statement

• Conditions are listed sequentially elif condition :
• elif short for “else if” statement
else :
• Can use multiple elif in the if statement statement
• Exactly one block is executed
• Else is only used for the final block
If ~ elif Statement
x=5 • If x is less than y
y = 10
• ‘5 is less than 10’
if x < y :
print( x, "is less than", y) • If x is greater than y
elif x > y :
print( x, "is greater than", y) • ‘10 is greater than 5’
else :
print( x, "and", y, “is equal to“)
• If x is equal to y
• ‘5 is equal to 5’
If ~ elif Statement, Example(1)
# Enter Grade
score = 75

if score >= 90 :
print(“Your grade is A”)
elif score >=75:
print(“Your grade is B”)
elif score >=60:
print(“Your grade is C”)
else:
print(“Your grade is F”)
If ~ elif Statement, Example(2)
# brokers commission
amount= int(input(“Enter your transaction amount : “))

if amount > 1000000 :


comm = amount * 0.5
elif amount > 500000 :
comm = amount * 0.7
elif amount > 200000 :
comm = amount * 1.0
else :
comm = 15000

print(“Transaction amount = “, amount, “and commission = “, comm)


If ~ elif Statement, Example(3)
# Fruits lists

fruits = ['apple', 'banana', 'grape', 'orange', 'strawberry']

fruit_input = input(”Enter your favorite fruit: ")

if fruit_input in fruits :
print(”The fruit is in the list.")

elif fruit_input == ”none" :


print(”Try the fruit! You will like it.")

else :
print(”The fruit is not in the list.")
Nested conditional statements
• Conditional statement can be used in other
conditional statements
if x == y :
print (x, "and", y, "are equal“)
else :
if x < y :
print( x, "is less than", y)
else :
print( x, "is greater than", y)

• The outer conditional statement has two branches


• Second branch is considered as a separate if statement,
• It has two branches
• Both branches have a print statement
Nested conditional statements
• Nested conditional statements can be written
as one conditional statement
if 0 < x :
if x < 10 :
print( "x is a positive single digit.“)

# operator and

if 0 < x and x < 10 :


print( "x is a positive single digit.“)
Exercise
• After receiving the current temperature,
• If the temperature is above 30 degrees, "It's
hot".
• If it's above 35 degrees, print "It's very hot”

• Use if, elif


Exercise, Code
temperature = int(input(”What is the current temperature "))

if temperature > 35 :
print(”It’s very hot")
elif temperature > 30 :
print(”It’s hot")
Lecture Summary
• When to use an conditional statement
• If the output of a program should change depending on
the conditions

• Conditional Statement
• if statement: If the condition is true
• if ~ else statement: Ran either condition is true and false
• if ~ elif statement: Ran depending on the condition

• Exercise conditional statement using practice


questions
Practice Question
• Which of the descriptions of the conditional
statements is not appropriate?
• Conditional clauses must end with colon
• Conditional clauses must be used with
parentheses
• Execution statement must start with a new line
• Execution statements after conditional clauses
must be indented
Practice Question, Answer
• Which of the descriptions of the conditional
statements is not appropriate?
• Conditional clauses must end with colon
• Conditional clauses must be used with
parentheses
• Execution statement must start with a new line
• Execution statements after conditional clauses
must be indented
Practice Question
• In python programming, what is the wrong way to
express 0 < x < 10?
• x > 0 and x < 10
• x > 0 or x < 10
• 0 < x < 10
• 10 > x > 0
Practice Question, Answer
• In python programming, what is the wrong way to
express 0 < x < 10?
• x > 0 and x < 10
• x > 0 or x < 10
• 0 < x < 10
• 10 > x > 0
Thank you
08 If Statement
Contact Info
[email protected]
https://ecampus.handong.edu/

* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational
materials including the video and other relevant materials for commercial and for-profit use.
CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik.

You might also like