Learn Python in 30 Days — Day 5: Conditionals (if/elif/else)
Learn Python in 30 Days — Day 5: Conditionals (if/elif/else)
Welcome to Day 5 of the Learn Python in 30 Days series!
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
Yesterday, you learned how to use operators to compare values and make simple expressions.
Today, we’re taking that a step further by using conditionals, the logic that lets your programs make decisions.
By the end of this lesson, you’ll understand:
-
How to use
if,elif, andelse -
How to make multi-step decisions
-
How to combine multiple conditions using logical operators
-
How to build a mini-project: Grade Calculator
Step 1 – What Are Conditionals?
Conditionals let your program choose between different actions depending on the situation.
Example:
If someone’s over 18, they can vote. Otherwise, they can’t.
You can also download this example from my GitHub here and run it yourself.How it works:
-
Python checks the condition
age >= 18. -
If it’s
True, it runs that block. -
Otherwise, it skips to the
elsepart.
Step 2 – Adding More Options (elif)
Use elif to check another condition if the first isn’t true.
You can have as many elif statements as you like. Python checks them in order, top to bottom, and stops when one is true.
Step 3 – Comparing Strings
Conditionals don’t just work with numbers — you can compare text too.
Try typing this into the IDLE IDE and running it and you should see the following output.You can also download this example from my GitHub here and run it yourself.Remember:
-
==checks if values are equal. -
=assigns a value to a variable.
Step 4 – Mini Project: Grade Calculator
Let’s write a simple program that turns a test score into a grade.
Try typing this into the IDLE IDE and running it and you should see the following output.You can also download this example from my GitHub here and run it yourself.Try different numbers and see how it changes.
Bonus challenge: handle invalid input like this:
Step 5 – Combining Conditions
Sometimes you need to check more than one thing at once, that’s where combined logic comes in.
You can use the operators and, or, and not together with your if statements.
Using and
All conditions must be True for the block to run.
Using or
Only one condition needs to be True.
Great for situations where there are multiple valid answers.
You can also download this example from my GitHub here and run it yourself.Using not
Flips True to False (and vice versa).
This is useful for checks like “if not logged in”, “if not empty”, etc.
Try typing this into the IDLE IDE and running it and you should see the following output.You can also download this example from my GitHub here and run it yourself.Combine Them Together
You can chain logical operators to create more complex checks:
Here’s how that works:
-
The part in brackets is checked first:
(age >= 18 and has_ticket) -
If that’s True, or if
is_vipis True, the user gets access.
Parentheses () make your logic easier to read and help Python know which part to check first.
Summary
Today, you learned:
-
How to control your program with
if,elif, andelse -
How to compare values and text
-
How to combine multiple conditions using
and,or, andnot -
Built a Grade Calculator project
Next up: Day 6 – Chaining Conditions + Nesting
You’ll learn how to structure more complex decision trees using multiple levels of logic.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days








Comments
Post a Comment