Code Bytes · "Intro to Programming" · Episode 7 · Decisions and loops
If statements
An if statement lets code make a decision, just like "if it's raining, take an umbrella". In Python you write if, a condition and a colon, then indent the lines that belong to it; else handles the no answer, and a line that isn't indented runs either way.
Code decides like you do
Think about your morning. If it's raining, you take an umbrella. If it's not, you don't. That's a decision with two paths, and code makes decisions the same way: it checks something, and the answer picks which lines run.
True means yes
In Python, we can store the answer in a variable:
``python raining = True ``
True, with a capital T, is a special value that means yes. Its partner is False, which means no. They must be capitalised: true with a small t is not the same thing, and Python would not recognise it.
if, a colon and an indent
Now the decision itself:
``python raining = True if raining: print("Take an umbrella") ``
`` Take an umbrella ``
Read it as "if raining, then...". The line ends with a colon, and the line under it is pushed in by four spaces. That push is called indenting, and it tells Python that this line belongs to the if. Because raining is True, the answer is yes, so the indented line runs.
else: the no answer
Add else: for when the answer is no, and change True to False:
``python raining = False if raining: print("Take an umbrella") else: print("Wear sunglasses") print("Have a nice day") ``
`` Wear sunglasses Have a nice day ``
Python checks raining, finds False, skips the if part and runs the else part instead. else gets a colon and an indented line too.
Lines that aren't indented run either way
Look at the last line, print("Have a nice day"). It starts at the left edge, so it doesn't belong to the if or the else. It runs whatever the answer is. Set raining = True and the same program prints:
`` Take an umbrella Have a nice day ``
Try it yourself
Open any free online Python editor (search "online Python") and run:
``python hungry = False if hungry: print("Eat a snack") else: print("Keep coding") print("Done") ``
You should see:
`` Keep coding Done ``
Now change False to True and predict the output before you run it. Then try removing the four spaces in front of print("Eat a snack") and see what Python says.
Good to know
- Every snippet on this page was run with Python 3 and printed exactly what is shown.
- Forget the indent and Python stops with an error:
IndentationError: expected an indented block after 'if' statement on line 2. - Four spaces is the standard style (from PEP 8, Python's style guide). Any consistent indentation works, but four spaces is what most code uses, and most editors add them for you when you press Tab.
- Forget the colon and you get a
SyntaxError. TrueandFalsehave their own type,bool. You'll meet that name in a later episode.- An if can also check a comparison, like
temperature > 20. That's the next episode. For more than two choices, Python also haselif.
Answer to the comment question
What does this print?
``python raining = False if raining: print("A") print("B") ``
It prints just B. raining is False, so Python skips the indented print("A"), which belongs to the if. print("B") isn't indented, so it doesn't belong to the if and runs either way.
Test your knowledge
Quick quiz: If statements
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Python Software Foundation, "More Control Flow Tools": if statements: if, else and elif
- Python Software Foundation, "Compound statements": the if statement: a block runs only when its condition is true
- Python Software Foundation, "Lexical analysis": Indentation: indentation decides which lines belong to a block
- Python Software Foundation, "Built-in Constants": True and False
- Python Software Foundation, PEP 8, "Style Guide for Python Code": Indentation: use 4 spaces per indentation level
Video transcript
How does code make a decision?
Think about your morning. If it's raining, you take an umbrella. If it's not, you don't.
Code decides the same way. In Python, we type raining equals True. True, with a capital T, is a special value that means yes.
Next, if raining, colon. The line under it is pushed in by four spaces. That's called indenting, and it means this line belongs to the if.
Run it. The answer is yes, so it prints Take an umbrella.
Now add else, colon, for the no answer. Change True to False, and Python skips the if part and runs the else part.
The last line isn't indented, so it runs either way.
What does this print? Comment your answer below!
That's your byte.
More from "Intro to Programming"
- "Intro to Programming" EP4Variables
- "Intro to Programming" EP5Data types
- "Intro to Programming" EP9For loops
- "Intro to Programming" EP10While loops