Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

argument
A value you put inside the parentheses when you call a function, which the function then receives.
block
A group of lines that belong together under one opening line, such as the body of an if or a loop.
branch
A path a program can take when a condition sends it one way or another.
call site
The place in the code where a function is called, as opposed to where it is defined.
class
A template that bundles data with the code that works on it; each copy has its own values.
dictionary
A collection of key and value pairs, where a key is used to look up its value.
exception
An error that stops the normal flow of a program unless some code catches it.
function
A named piece of code that does one job and can be called whenever you need it.
function object
A function treated as a value, so it can be stored in a variable, passed to another function, or returned.
indentation
The spaces at the start of a line that show which block it belongs to.
integer
A whole number with no decimal point, such as 3, 0 or -12.
list
An ordered collection of values that you can grow, shrink and reach into by position.
parameter
A name in a function definition that stands for the value the caller will supply.
return value
The result a function hands back to the code that called it.
scope
The part of a program where a name can be used; a name made inside a function is not visible outside it.
traceback
The report Python prints when a program fails, listing the calls that led to the error.
variable
A name that refers to a value, so you can use that value again later or change it.

Python from the ground up

Making decisions

if, elif and else: the program asks a question and follows the first answer that is true.

Part 3 of 15

Beginner14 min readBack to Courses

A question with two answers

Not every line should run every time. An if line asks a question, and the indented lines under it run only when the answer is yes. An else gives the other answer a home, so both paths are written down and neither is an accident.

Read it out loud like a sentence: if the temperature is greater than 18, take a light jacket; otherwise, take a warm coat. The code below is that sentence, written for Python.

if, else, and the advice

temperature = 21

if temperature > 18:
    print("Take a light jacket.")
else:
    print("Take a warm coat.")

The colon and the spaces

Two pieces of punctuation carry the whole idea. The colon at the end of the if line means "a block follows". The four spaces in front of the lines under it are how Python knows they belong to that block rather than to the line after it.

This is not a style choice — Python uses the indentation as grammar. Four spaces is the convention, and consistent spaces matter more than the exact count: a block whose lines are not indented the same is an IndentationError, and the message names the line that lost its way.

Decide what to wear

Write an if/else below the greeting: a light jacket when the temperature is above 18, a warm coat otherwise. Then change the number, run again, and watch the advice follow.

Output
Click "Run" to execute your code

More than two paths: elif

elif means "otherwise, if". It adds another question after the first, and Python walks down the list, running the first block whose question is true and skipping the rest.

Order matters, because the first true question wins. Test the coldest range first if the answers overlap, or a mild question will answer for a freezing day before the freezing one is ever asked.

Asking and assigning look alike

= gives a value a name, and == asks whether two values are the same. One character apart, and it is the most common mistake a beginner makes — writing if temperature = 18: where the question was meant.

When it happens, Python says something about a syntax error rather than answering wrongly, which is its way of being kind. Read the line number, and count the equals signs.

The branch you did not take

An if chooses one path and skips the other entirely. The skipped lines are not run slowly or partially; they are never executed, so a mistake inside them stays hidden until the condition changes. That is why a program can look correct for weeks and fail on the first cold morning: the warm-weather branch was the only one being tested.

When a decision misbehaves, print the condition itself before the branch, not the value you think it used. print(temperature > 18) answers True or False and shows which path the program is about to take. Then print the value. Two prints, one line apart, separate the value from the comparison and show which of the two surprised you.

If the advice never changes, print the value just before the if. The program is only ever asking about the value it actually holds.

Conditions can be combined

A comparison produces a value, True or False, and if simply asks whether that value is true. That is why a condition can be a single comparison or several joined with and, or and not.

and is true only when both sides are; or is true when at least one is; not flips the answer. They bind in that order — not tightest, then and, then or — so when the sentence in your head has a comma in it, write brackets to say which part belongs together.

Not every question needs a comparison

An if does not require == or >. It asks whether the value is truthy: empty text, the number 0, an empty list and None count as false; almost everything else counts as true.

That makes if name: a short way to ask whether text was supplied. It also makes if temperature: a poor test for a real temperature of 0, which is a value like any other — be explicit when zero is meaningful.

Three slips: if temperature > 18 and < 30: is a syntax error because each side of and needs its own comparison; testing age == "18" against the number 18 quietly gives False rather than an error; and an if with no colon, or a body with inconsistent spaces, stops the file before anything runs.

Try these next

  • Add a fourth band to the advice — freezing below 0, warm below 12, mild below 20, hot above.
  • Rewrite if not temperature > 18: as a direct comparison, then check both give the same advice.
  • Print the result of 18 < temperature < 25 for three different temperatures.

Three ticket prices

Complete the price ladder: free under 5, half price from 5 to 17, and full price from 18. Run it for ages 3, 12 and 30 by changing the variable.

Output
Click "Run" to execute your code

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What does if temperature > 18: do?