Day 6: If Statements – Teaching Python to Make Decisions – 30 Days of Python Challenge



This content originally appeared on DEV Community and was authored by SAINAPEI LENAPUNYA

Welcome Back to Day 6!

Hey everyone! It’s Day 6 of my 30 Days of Python Challenge, and today we’re making our programs smart! 🧠

If you missed the previous days:

  • [Day 1: Print Statements]
  • [Day 2: Variables and Data Types]
  • [Day 3: Type Casting]
  • [Day 4: User Input]
  • [Day 5: Arithmetic Operators]

Today, we’re learning how to make decisions in our code using if statements. Let’s give Python the power to think!

Day 6: If Statements – Decision Making in Code 🤔

Today’s mission: If Statements. Until now, our code has been running line by line, doing the same thing every time. But what if we want different outcomes based on different conditions? That’s where if statements come in!

What I Learned

If statements are like giving your program a brain. They let you:

  • Check conditions and respond accordingly
  • Create different paths through your code
  • Make programs that adapt to user input
  • Build logic into your applications

This is where programming gets really powerful!

My Code

Here’s what I wrote for Day 6:

# Day 6 - If statements
num = int(input("Enter a number: "))
if num > 0:
    print("Positive")
elif num == 0:
    print("Zero")
else:
    print("Negative")


age = int(input("Enter your age: "))
if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

Breaking It Down 🔍

Let me walk you through each part:

Example 1: Checking if a number is positive, zero, or negative

  1. num = int(input("Enter a number: ")) – Remember Day 3 (type casting) and Day 4 (user input)? Here they come together! We get user input and immediately convert it to an integer.

  2. if num > 0: – This is our first condition. The > means “greater than.” If the number is positive, Python executes the indented code below.

  3. elif num == 0: – “elif” means “else if.” If the first condition is false, Python checks this one. Notice the double equals ==? That’s for comparison (single = is for assignment).

  4. else: – If none of the above conditions are true, this runs. It’s our catch-all.

Example 2: Checking if someone is an adult

  1. if age >= 18: – The >= means “greater than or equal to.” If someone is 18 or older, they’re an adult.

  2. else: – If they’re not 18 or older, they’re a minor. Simple!

Output

When you run this code, you’ll see something like:

Enter a number: 5
Positive
Enter your age: 23
You are an adult.

Or if you enter different values:

Enter a number: -3
Negative
Enter your age: 16
You are a minor.

Comparison Operators 🎯

Today I used several comparison operators. Here’s the complete list:

  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to
  • == Equal to (for comparison)
  • != Not equal to

Important Things to Remember 🔑

Indentation Matters!
Notice how the code under if, elif, and else is indented? Python uses indentation to know what code belongs to which condition. This isn’t just for style—it’s required!

if, elif, else Structure:

  • if – First condition to check
  • elif – Additional conditions (you can have multiple)
  • else – Runs if none of the above are true (optional)

Assignment vs Comparison:

  • = assigns a value: age = 18
  • == compares values: age == 18

Key Takeaways 💡

  • If statements let your program make decisions based on conditions
  • Use if for the first condition, elif for additional conditions, else for everything else
  • Comparison operators (>, <, >=, <=, ==, !=) let you compare values
  • Indentation is crucial in Python—it defines what code belongs to each condition
  • You can combine type casting with user input to work with numbers
  • Not every if statement needs an elif or else—use what you need!

What’s Next?

Tomorrow on Day 7, I’ll be building a simple calculator! We’ll combine everything we’ve learned so far—user input, type casting, arithmetic operators, and if statements—to create something functional and fun!

Let’s Connect!💬

I’d love to hear from you!

  • What’s the first decision you want your program to make?
  • Did the indentation requirement surprise you?
  • Any creative uses for if statements you can think of?

Drop a comment below! If you’re coding along, share what conditions you’re checking in your programs! 🎲

Don’t forget to follow me for daily updates. Day 7 is going to be super practical! 💪

Happy Coding!


This content originally appeared on DEV Community and was authored by SAINAPEI LENAPUNYA