Have you ever made a choice like:
"If it rains, I will take an umbrella. Otherwise, I will wear my cap."
Congratulations! ๐ You already understand conditions — and Python uses them too! In this blog, we’ll learn how to teach Python to make decisions using conditional statements.
๐ค What Are Conditional Statements?
Conditional statements help Python make decisions — like:
-
What to do if something is true
-
What to do if it’s not
Just like humans! ๐
๐ ️ Basic Conditional Keywords in Python
Python has three main conditional keywords:
Keyword | What It Means |
---|---|
if | Do something if it's true |
elif | Else if (another condition) |
else | Do this if nothing else is true |
๐ Let's Start With an if
Statement
๐ง What It Means: If the age
is more than 10, Python will say something.
➕ Add else
for More Options
๐ก Python checks the condition. If it’s not true, it runs the else
part.
๐ Add More Choices with elif
๐ Python checks each if
or elif
one by one. It stops when one is true.
๐จ Don’t Forget the Indentation!
Python cares about spaces. Code inside if
, elif
, or else
should be indented like this:
❌ This will cause an error:
✅ Correct way:
๐งช Try This Fun Example
Try changing color = "green"
and see what happens!
๐น️ Practice Time!
Change the values in this program and predict the output:
๐ง Summary
Statement | Use it when… |
---|---|
if | You want to check one condition |
elif | You want to check more conditions |
else | You want a backup if others are false |
๐ฏ Final Thought
Python is like a smart robot — it can follow instructions, but it also knows how to choose what to do based on your conditions.
So next time you're writing Python code, remember:
“If this happens… then do that!”
No comments:
Post a Comment