🧠 How to Code in Python: Part 2 β€” If, Else, and Logic

In the last post, we built our first Python script, explored variables, and got user input. Now it’s time to make your programs think using if/else statements, comparison operators, and logic.

This is how your code decides what to do based on conditions β€” like whether a number is too big, a password is correct, or someone is allowed to log in.


πŸ”Ž Step 1: Understanding if Statements

An if statement lets your code make decisions.

age = 18

if age >= 18:
    print("You are an adult.")

If the condition age >= 18 is True, the indented code runs. If not, Python skips it and does nothing.

Output:

You are an adult.

If the condition is False and there’s no else, nothing happens. That’s perfectly valid β€” but sometimes you’ll want to handle the “what if it’s not true?” case too.


πŸ”„ Step 2: Adding else

Use else to define what should happen if none of the previous conditions are True.

age = 16

if age >= 18:
    print("You are an adult.")
else:
    print("You are underage.")

Output:

You are underage.

πŸ“Œ Note: else is optional β€” but if you use it, it must come last in your if-elif-else chain. If you try to put an elif after else, Python will give you a syntax error.


βš–οΈ Step 3: Using elif (short for “else if”)

elif lets you check multiple conditions in sequence:

score = 85

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
else:
    print("Grade: C or lower")

Output:

Grade: B

πŸ” Note: Python checks conditions from top to bottom and only runs the first one that is True. It skips the rest after finding a match.

You can have only if and elif β€” and leave out else entirely if you don’t care about unmatched cases:

score = 60

if score >= 90:
    print("A")
elif score >= 80:
    print("B")

Output:
(No output β€” and that’s OK)


πŸ”’ How Many elif Can You Use?

🧠 Python doesn’t limit how many elif blocks you can have β€” use as many as you need. Just remember:

  • If you have a long chain of conditions, your code might become harder to read.
  • Consider using a dictionary, a function, or match-case (introduced in Python 3.10) if the logic starts to look repetitive or bulky.

Example with several elif blocks:

day = "Wednesday"

if day == "Monday":
    print("Start of the week.")
elif day == "Tuesday":
    print("Keep going.")
elif day == "Wednesday":
    print("Halfway there!")
elif day == "Thursday":
    print("Almost done.")
elif day == "Friday":
    print("TGIF!")
else:
    print("Weekend mode.")

βœ… Works perfectly β€” just stay organized!


βž• Step 4: Comparison Operators

These are the tools you use to compare values in your conditions:

==  # equal to
!=  # not equal to
>   # greater than
<   # less than
>=  # greater than or equal to
<=  # less than or equal to

Example:

x = 5

if x != 10:
    print("x is not 10")

Output:

x is not 10

πŸ”— Step 5: Combining Conditions with and / or

You can check multiple things at once using logic operators.

age = 20
has_id = True

if age >= 18 and has_id:
    print("Access granted.")
else:
    print("Access denied.")

Output:

Access granted.

🧠 and = both conditions must be True
πŸ” or = at least one condition must be True


🎯 Step 6: Interactive Example with User Input

Let’s put it all together with input():

age = int(input("Enter your age: "))
has_id = input("Do you have an ID? (yes/no): ")

if age >= 18 and has_id.lower() == "yes":
    print("βœ… You can enter the venue.")
else:
    print("❌ Access denied.")

Sample run:

Enter your age: 22
Do you have an ID? (yes/no): yes
βœ… You can enter the venue.

πŸ” Note: input() always returns a string, even if the user types a number.
That’s why we use int() to convert age to a number so we can compare it.


βœ… Recap: What You Learned

  • if, elif, and else control the flow of logic in your program
  • else is optional, but if you use it, it must go last
  • Python runs only the first True condition
  • You can stack multiple elif blocks β€” no hard limit
  • Use and and or to combine conditions
  • Input from users is always a string, so use int() when needed
  • Python uses indentation instead of curly brackets to group code

You’ve now given your programs the power to think β€” making them responsive and dynamic!


⏭️ Coming Up Next: Loops and Repeating Tasks

In the next lesson, we’ll explore loops β€” how to repeat actions using while and for, how to loop through lists, and how to avoid infinite loops. It’s about to get fun πŸ”

Leave a comment