DEV Community

SameerQaisar17
SameerQaisar17

Posted on

If-Else Statements: When I Finally Got It

📌 Quick Info

  • Topic: If-Else Statements in Python
  • Target Audience: Upper beginners who know basic Python
  • Goal: Help someone understand if-else in 5 minutes

1. Introduction

"When I first started learning Python, I didn't understand why I needed if-else. I just wrote code that ran line by line. Then I wanted to build a program that made decisions — like checking if someone was old enough to vote. That's when I discovered if-else, and everything clicked."


2. The Problem (Without If-Else)

Imagine you want to check if someone can vote:

age=18
How do I check if they're old enough?
Without if-else, my code can't make this decision
Enter fullscreen mode Exit fullscreen mode
age = 18

if age >= 18:
    print("You can vote!")
else:
    print("Too young to vote")
Enter fullscreen mode Exit fullscreen mode

Output:
You can vote!

5. What I Learned

  • If-else lets my code make decisions
  • The condition must be TRUE for the if block to run
  • The else block runs when the condition is FALSE
  • I can use comparison operators: >=, <, <=, ==, !=, >, <

6. Conclusion

If-else seemed confusing at first. But once I practiced with simple examples like voting and marks, it became my favorite tool for building interactive programs.

Top comments (0)