The if Statement
The simplest of the three, and the one to reach for when there are many possibilities but only one outcome. The value being tested can be anything at all — any bill, any age, any mark — but there is only one extra thing your program might do about it. It happens, or the program carries on as though the if were not there.
1How it is written
if condition:
statement
statement
next statementThe header names the condition and ends in a colon. The indented lines under it are the block. The line at the margin below is not part of the block, and that line is the one worth watching in everything that follows.
2A program that decides
A shop gives free delivery on bills over ₹500. Every customer is told their total; only some are told about the delivery.
- the bill amount
- is the bill more than 500?
- the free-delivery message, but only when it is
- the amount to pay, always
# a big order earns free delivery
bill = float(input('Bill amount: '))
if bill > 500:
print('You get free delivery!')
print('Amount to pay:', bill)Bill amount: 750 You get free delivery! Amount to pay: 750.0
Now the same program, run again by a customer who spent less:
# the same program, a different customer
bill = float(input('Bill amount: '))
if bill > 500:
print('You get free delivery!')
print('Amount to pay:', bill)Bill amount: 200 Amount to pay: 200.0
One program, two runs, two different outputs. Nothing was rewritten between them. That is the whole point of selection — and notice what did not happen on the second run: no error, no warning, no blank line where the message would have been. The block was skipped, and the program went straight on to the line below it.
3Watch the condition decide
Drag the bill through the boundary and watch two things: the block, which appears and disappears, and the last line, which never does.
Drag the bill the customer typed. Watch the block below the if — and watch the last line, which never moves.
bill > 500750.0 > 500True → the block runs, then the program carries on with the line below it.
Bill amount: 750
You get free delivery!
Amount to pay: 750.0The last line prints either way. That is the whole difference an if makes: one line more, or one line less.
False, Python skips the block and carries on with the next statement outside it. The program does not stop, nothing is reported, and the rest of the program runs exactly as usual.4Many possibilities, one outcome
The bill could be any amount — that is thousands of possibilities. But the program only ever does one extra thing about them: it prints the free-delivery message. There is no second message for small bills, and none is wanted. That is what makes a plain if the right shape here.
The condition can be as complicated as the rule needs. Here two questions are joined with and, so several scenarios are being tested at once — and there is still only one outcome:
# one outcome: you are told about the club, or nothing is said
age = int(input('Your age: '))
marks = float(input('Your marks: '))
if age >= 16 and marks >= 60:
print('You can join the coding club')
print('Thank you for applying')Your age: 17 Your marks: 72 You can join the coding club Thank you for applying
# the same program, an applicant who is too young
age = int(input('Your age: '))
marks = float(input('Your marks: '))
if age >= 16 and marks >= 60:
print('You can join the coding club')
print('Thank you for applying')Your age: 15 Your marks: 90 Thank you for applying
Excellent marks, and still nothing — because and needs both halves to be True. Everybody gets the thank-you either way.
if is no longer the right shape. That is the next lesson.5The two mistakes everybody makes once
Both are punctuation, both stop the program before it runs a single line, and both name themselves clearly once you have seen them.
The missing colon.
age = 19
if age >= 16
print('You are eligible to drive!') File "no_colon.py", line 3
if age >= 16
^
SyntaxError: expected ':'The missing indentation.
age = 19
if age >= 16:
print('You are eligible to drive!') File "no_indent.py", line 4
print('You are eligible to drive!')
^
IndentationError: expected an indented block after 'if' statement on line 3Both are syntax errors, so nothing runs at all — not even the correct line above. Python does not need to know the age to spot them; it cannot read the file. And the second message is unusually helpful: it names the statement, the line number and what it wanted.
= in a condition. = is an order (put 16 in age), == is a question (is age 16?). Writing if age = 16: gives SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='? — a rare case of Python guessing your mistake correctly.6Try it
Run it with a bill over 500, then with one under. Then try moving the last line four spaces to the right and run it again — you should get nothing at all from a small bill.
7Recap
if gives your program one outcome: the block runs when the condition is True, and is skipped when it is False. Skipped means nothing happens — no error, no message — and the program carries on with the first line back at the margin. Header ends in a colon; block is indented by 4 spaces.bill = 200. What does 'if bill > 500:' followed by a print, then a print at the margin, produce?
Which of these is the right shape for: 'warn the student if attendance is below 75%, and say nothing otherwise'?
What is wrong with: if marks >= 40 print('Pass')