Python if Statement
Table of Contents + −
In the last lesson you learned about Identity Operators. Those operators check whether two names point to the very same object. The checks give you back a True or a False. But a True or a False on its own does nothing useful yet. Things get useful when your program can look at that answer and decide to act on it. That is exactly what the if statement is for.
🤔 Why Do We Need the if Statement?
So far your programs have run every line, top to bottom. There was no choice. Every line ran whether it made sense or not.
But real programs need to make choices. Think about it:
- Amazon should only let Alex place the order if the item is in stock.
- Netflix should only start the show if the account is paid.
- A bank app should only allow the purchase if the balance is enough.
See the pattern? Each one says “do this thing, but only when something is true.” That little word only is the key. Without it, your code does the same thing every single time.
The if statement is the fix. It lets your program run a block of code only when a condition is true. If the condition is false, that block is skipped completely.
🧩 What Is an if Statement?
An if statement is a way to say to Python: “check this condition first. Only run the next lines if it turns out to be true.”
Here is a simple real-world way to picture it. Imagine a security guard at a gate. The rule is simple. If your name is on the list, you may enter. The guard checks the list. Name on it? You go in. Name not on it? You are turned away, and nothing else happens. The guard does not run any of the “let them in” steps.
Your if statement is that guard. The condition is the list check. The indented block is the “let them in” steps.
🛠️ The Syntax
The shape of an if statement looks like this.
if condition: # this line runs only when the condition is True do_something()These details matter a lot, and people trip on both of them. Let us name them clearly:
- The colon (
:) at the end of theifline. It tells Python “the block starts now.” - The indentation of the lines below. The spaces in front are not decoration. They are how Python knows which lines belong to the
if.
We will come back to both of these. They are the parts that break most often.
🔢 A Simple Example
Let us start with the smallest possible example. We check if a number is positive. If it is, we print a message.
number = 7
if number > 0: print("This number is positive.")
print("Done checking.")Run that and you get:
Output
This number is positive. Done checking.
Now let us read it line by line.
number = 7just stores the value7in a variable callednumber.if number > 0:asks a yes-or-no question. Isnumbergreater than0? Here7 > 0isTrue, so the block runs.print("This number is positive.")is indented, so it belongs to theif. Because the condition was true, this line runs.print("Done checking.")is not indented. It sits outside theif. So it always runs, no matter what the condition was.
The condition number > 0 is just a comparison. It is the same kind you saw in the comparison and boolean lessons. It gives back True or False, and the if reads that answer to decide what to do.
💳 A More Real Example
A made-up number being positive is fine for learning. Now let us do something that feels like a real app. This is the bank example. Only allow the purchase if the balance is enough.
balance = 500price = 350
if balance >= price: balance = balance - price print("Purchase approved.") print(f"Your new balance is {balance}.")
print("Thank you for shopping with us.")This prints:
Output
Purchase approved. Your new balance is 150. Thank you for shopping with us.
Here is what happened, step by step.
- We start with a
balanceof500and apriceof350. if balance >= price:asks: is the balance at least as big as the price?500 >= 350isTrue, so we go inside the block.- Notice the block has three lines, and all three are indented the same amount. So all three belong to the
if. We subtract the price, then print the approval, then print the new balance. - The last
printis not indented, so it runs every time. A friendly thank-you message makes sense whether or not the purchase went through.
This is the heart of it. One condition guards a whole block of related steps.
🚫 What Happens When the Condition Is False?
This is the part people forget, so let us be very clear about it. When the condition is false, Python skips the entire indented block. It does not run a single line inside it. It just jumps past the block and keeps going.
Let us take the same bank example, but this time the balance is too small.
balance = 200price = 350
if balance >= price: balance = balance - price print("Purchase approved.") print(f"Your new balance is {balance}.")
print("Thank you for shopping with us.")Now the output is:
Output
Thank you for shopping with us.
See what is missing? None of the “approved” lines printed. Here is why:
200 >= 350isFalse.- So Python skips the whole indented block. The balance is never changed, and neither approval line prints.
- The only line that runs is the last
print. It sits outside theif, so it always runs.
That is the skip behaviour. A false condition means the guarded block simply does not happen. There is no error, no warning. The program just moves on.
⚠️ Common Mistakes
A few small slips cause most of the early pain with if. Watch out for these.
Forgetting the colon. The if line must end with a colon. Leave it out and Python stops with a syntax error.
# ❌ Avoid: no colon at the end of the if lineif number > 0 print("Positive")
# ✅ Good: colon tells Python the block is startingif number > 0: print("Positive")Forgetting to indent the block. The lines inside the if must be indented. If they are not, Python does not know what belongs to the if.
# ❌ Avoid: the print is not indented, so Python raises an errorif number > 0:print("Positive")
# ✅ Good: the indented line belongs to the ifif number > 0: print("Positive")Using = instead of ==. A single = means “assign a value.” A double == means “are these equal?” In a condition you almost always want ==.
# ❌ Avoid: a single = is assignment, not a comparisonif age = 18: print("Just turned adult")
# ✅ Good: == compares the two valuesif age == 18: print("Just turned adult")Mixing spaces and tabs for indentation. Pick one. Most people use four spaces. If you mix tabs and spaces, Python can get confused and complain even when the code looks lined up.
✅ Best Practices
A few simple habits will keep your if statements clean and easy to read.
- Use four spaces for each level of indentation. It is the standard, and most editors do it for you when you press Tab.
- Keep the condition easy to read.
if balance >= price:reads almost like an English sentence, and that is a good sign. - Write the condition so a true result means “yes, do the thing.” It matches how we think and keeps the code clear.
- Only put inside the block the lines that truly depend on the condition. Anything that should always run belongs outside, not indented.
🧩 What You’ve Learned
✅ The if statement runs a block of code only when its condition is true.
✅ A condition is just something that gives back True or False, like a comparison.
✅ The if line must end with a colon, and the block under it must be indented.
✅ The indentation is what tells Python which lines belong to the if.
✅ When the condition is false, Python skips the whole block with no error and keeps going.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does an `if` statement do when its condition is True?
Why: When the condition is True, Python runs the indented block that belongs to the if.
- 2
What must go at the end of the `if` line?
Why: The colon at the end of the if line tells Python that the block is about to start.
- 3
What tells Python which lines belong to the `if` block?
Why: Python uses the indentation to know exactly which lines are inside the if block.
- 4
If the condition is False, what happens to the indented block?
Why: A False condition means Python skips the whole block, with no error, and moves on.
🚀 What’s Next?
Right now your if only handles the “true” case. But what if you also want to do something when the condition is false? That is where the next lesson comes in.