Home About ContentOthers
Thynk Unlimited
Conditional
Statements
(If, Elif, Else, Nested
Ifs)
3.
In Python, conditionalstatements are used
to execute a specific block of code only if a
certain condition is met. They enable your
program to make decisions, control its flow,
and respond dynamically to different inputs
and data.
Home About Content Others
What are conditional Statements?
4.
Thynk Unlimited HomeAbout Content Others
If Statement
The if statement is the simplest conditional
statement. It checks a condition and, if True,
executes the indented block of code that follows.
Syntax: if condition:
5.
Thynk Unlimited HomeAbout Content Others
If-else Statement
The if...else statement adds an
alternative code block that runs if
the initial if condition is False.
Synatax:
if condition:
#code to execute
else:
#code to execute
6.
Thynk Unlimited HomeAbout Content Others
If-elif-else Statement
allows you to check multiple conditions in sequence. If the
first if condition is False, Python checks the next elif
condition, and so on. If all if and elif conditions are False,
the code in the else block is executed.
Synatax:
if condition1:
# Code to execute if condition1 is True
elif condition2:
# Code to execute if condition1 is False and condition2 is True
else:
# Code to execute if all conditions are False
7.
Thynk Unlimited HomeAbout Content Others
Nested if statements
You can place if statements inside other if, elif, or else
blocks to create more complex decision-making structures.
Logical operators
Python's logical operators (and, or, and not) allow you to create more
complex boolean expressions for your conditions.
• and: Returns True if both conditions are True.
• or: Returns True if at least one condition is True.
• not: Reverses the truth value of a condition.
8.
Thynk Unlimited HomeAbout Content Others
Synatax:
if condition1:
# Code to execute if condition1 is True
if condition2:
# Code to execute if condition1 is False and condition2 is True
else:
# Code to execute if all conditions are False
Thynk Unlimited HomeAbout Content Others
Let’s Practice!
Create a program that checks if a student passed.
• If grade 75 “Passed”
≥ →
• If grade 90 “Passed with honors!”
≥ →
• If grade < 75 “Failed”
→
PROBLEM:
11.
Home About ContentOthers
YOUR TURN:
Problem Situation
You are tasked to simulate a simple ATM Machine using Python. The program should
allow the user to enter their account balance and the amount they want to withdraw.
The ATM should decide whether the transaction can proceed or not based on the
following conditions:
1.If the withdrawal amount is greater than the balance display “Insufficient
→
funds.”
2.If the withdrawal amount is greater than ₱10,000 display “Transaction limit
→
exceeded.”
3.If the withdrawal amount is positive and less than or equal to the balance, deduct
the withdrawal from the balance and display “Transaction successful. Your
remaining balance is: ₱_____.”
4.If the withdrawal amount is 0 or negative, display “Invalid withdrawal amount.”
12.
Thynk Unlimited HomeAbout Content Others
THANK YOU
THANK YOU
THANK YOU
www.reallygreatsite.com