Learn Python in 30 Days — Day 6: Chaining Conditions + Nesting
Learn Python in 30 Days — Day 6: Chaining Conditions + Nesting
Welcome to Day 6 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 if, elif, and else to make decisions in your code.
Today, we’re taking that a step further by combining and nesting those conditions, this is where your programs start to make better decisions based upon the information given to them.
By the end of this lesson, you’ll understand:
-
How to chain multiple conditions together using
and/or -
How to nest if statements inside each other
-
Why indentation matters so much in Python
-
How to write clear, logical decision-making code
Step 1 – Think Before You Code
Every decision in Python starts with a question.
Write your logic as a series of simple “yes/no” questions before typing any code.
A simple example deciding if a user can drive:
💡 Habit 1: Always read your code aloud.
“If age is 18 or more and they have a license, print this.”
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.If it sounds clear in English, it’s probably written clearly in Python.
Step 2 – Chaining Conditions Properly
Use and, or, and not to combine logic, but keep expressions short and readable.
✅ Good:
❌ Bad:
💡 Habit 2:
Break long conditions into parts:
This forms a foundation for clean, maintainable code.
Step 3 – Indentation = Structure
Python doesn’t use {} or end — indentation defines blocks.
Always use 4 spaces per level.
Example – a nested decision:
💡 Habit 3: Never mix tabs and spaces.
Your editor should insert spaces automatically. Consistent indentation keeps logic crystal clear.
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.Step 4 – Avoid Deep Nesting
Too many indents make code hard to read. Flatten logic where possible.
Nested:
Flattened (Better):
💡 Habit 4: Handle exceptions or early exits first, then what’s valid, this keeps code flowing naturally.
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.Step 5 – Visualize the Flow
Imagine logic like a flowchart:
-
Check broad conditions first.
-
Narrow down specifics.
-
Keep the reader moving top-to-bottom.
Example – a clear order of logic:
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.Mini Practice – Smart Vending Machine
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.Step 6 – Golden Rules for Future Code
-
One idea per
if— keep checks small. -
Indent consistently.
-
Name conditions clearly. (
is_valid,has_access,is_empty) -
Plan logic before you code.
These habits seem small now, but they’ll make your future code cleaner, safer, and easier to debug, whether you’re writing a text adventure or a robotics controller.
Next Up → Day 7: Review & Mini Challenge
You’ll combine everything you’ve learned so far into a practical little project.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!






Comments
Post a Comment