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:

age = 18 has_license = True if age >= 18 and has_license: print("You can drive.") else: print("Sorry, you can’t drive yet.")

💡 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:

if temp > 0 and temp < 30: print("Nice weather.")

❌ Bad:

if (temp > 0 and temp < 30 and not raining and humidity < 50 or has_jacket):

💡 Habit 2:
Break long conditions into parts:

temp = 25 raining = False
is_warm = 0 < temp < 30 comfortable = is_warm and not raining if comfortable: print("Go outside!")

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.

This forms a foundation for clean, maintainable code.

Step 3 – Indentation = Structure

Python doesn’t use {} or endindentation defines blocks.
Always use 4 spaces per level.

Example – a nested decision:

age = 19
if age >= 18: if has_id: print("Welcome.") else: print("You need ID.") else: print("Too young.")

💡 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:

logged_in = True banned = False
if logged_in: if not banned: print("Access granted.")

Flattened (Better):

logged_in = True banned = False
if not logged_in: print("Please log in.") elif banned: print("Access denied.") else: print("Access granted.")

💡 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:

  1. Check broad conditions first.

  2. Narrow down specifics.

  3. Keep the reader moving top-to-bottom.

Example – a clear order of logic:

if not has_ticket: print("Buy a ticket.") elif age < 18: print("Too young.") else: print("Welcome!")
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

item = input("Enter item (chips/drink): ") money = float(input("Insert money: ")) if item == "chips" and money >= 1.5: print("Enjoy your chips!") elif item == "drink" and money >= 2.0: print("Here’s your drink!") elif money < 1.5: print("Not enough money.") else: print("Invalid item.")
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

Popular posts from this blog

Math Behind Logic Gates

6502 - Part 2 Reset and Clock Circuit

Building a 6502 NOP Test