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, and else

  • 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.

age = 23 if age >= 18: print("You can vote.") else: print("You're too young.")

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.

How it works:

  • Python checks the condition age >= 18.

  • If it’s True, it runs that block.

  • Otherwise, it skips to the else part.

Step 2 – Adding More Options (elif)

Use elif to check another condition if the first isn’t true.

temperature = 25 if temperature > 30: print("It's really hot!") elif temperature > 20: print("Nice and warm.") else: print("A bit chilly today.")

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.

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.

name = input("What's your name? ") if name == "Matty": print("Hey, that’s me!") else: print("Nice to meet you, " + name + "!")
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.

score = int(input("Enter your test score (0-100): ")) if score >= 90: print("Grade: A") elif score >= 80: print("Grade: B") elif score >= 70: print("Grade: C") elif score >= 60: print("Grade: D") else: print("Grade: F")
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:

if score > 100 or score < 0: print("Error: Please enter a score between 0 and 100.")

You can also download the challenge code from my GitHub here and run it yourself.

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.

age = 20 has_id = True if age >= 18 and has_id: print("You can enter the club.") else: print("Sorry, you can’t enter.")

Both must be true: the person is 18+ and has ID.

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.

Using or

Only one condition needs to be True.

day = "Saturday" if day == "Saturday" or day == "Sunday": print("It’s the weekend!") else: print("It’s a weekday.")

Great for situations where there are multiple valid answers.

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.

Using not

Flips True to False (and vice versa).

is_raining = False if not is_raining: print("Let’s go outside!") else: print("Better grab an umbrella.")

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:

age = 22 has_ticket = True is_vip = False if (age >= 18 and has_ticket) or is_vip: print("Access granted!") else: print("Access denied.")

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.

Here’s how that works:

  • The part in brackets is checked first: (age >= 18 and has_ticket)

  • If that’s True, or if is_vip is 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, and else

  • How to compare values and text

  • How to combine multiple conditions using and, or, and not

  • 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

Popular posts from this blog

Math Behind Logic Gates

6502 - Part 2 Reset and Clock Circuit

Building a 6502 NOP Test