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

Variables and text

Names for values: store a piece of text, change it once, and drop it into a sentence with an f-string.

Part 2 of 15

Beginner13 min readBack to Courses

A name for a value

A variable is a name for a value. name = "Ada" reads as "name gets Ada": the equals sign puts the text on the right into the name on the left. From then on, name can stand anywhere the text itself could.

The name is not a copy of the text so much as a label on it. Change what the label points at with another line, name = "Grace", and every later line that uses name uses the new value. The old one is not erased — it simply no longer has anything pointing at it.

Storing, then using

name = "Ada"
print(name)
print("Hello, Ada!")

Text with holes: f-strings

Sentences built by hand repeat themselves: change the name in one line and you have to hunt for it in the next. An f-string puts the name straight into the sentence.

The f before the opening quote says "fill in the holes", and each pair of braces holds a name to fill with: f"Hello, {name}!". Put several in one sentence and they are all filled in order. Change the variable once and every sentence that mentions it follows.

A greeting that follows the variables

The two variables are ready, but the sentences below ignore them and say the values by hand. Rewrite both lines as f-strings so changing a variable changes what the program says.

Output
Click "Run" to execute your code

Text and numbers are different

36 is a number and "36" is text — the quotes change what it is, not how it looks. The difference shows the moment you use them: 3 + 6 adds up to 9, while "3" + "6" joins the two pieces of text into "36".

Python can glue text together with +, and that is genuinely useful. It just does not convert between the two on its own. When a sentence refuses to join, check whether one side is a number wearing no quotes.

Naming things well

A variable name may not contain spaces, and should not start with a digit. The convention in Python is lowercase with underscores between words: first_name, daily_total, sourdough_starter.

Names are for the person reading the program, and that person is usually you at a later date. x tells you nothing an hour from now; temperature_in_degrees tells you everything.

When a sentence comes out wrong, print the pieces on their own first. Seeing each value by itself is usually faster than guessing which one is the problem.

The right side is calculated first

An assignment has an order. Python works out everything on the right of the = first, then gives that finished value the name on the left. total = price * quantity multiplies before it stores, so the expression never needs the name it is defining.

The same is true inside an f-string’s braces: f"{price * quantity:.2f}" calculates the product and formats it to two decimals before the sentence is built. The colon introduces the format, and .2f means two digits after the point — useful for money, where 19.9 should read 19.90.

Text is immutable

Text values have methods: name.upper() returns a copy in capitals, name.strip() returns a copy without surrounding spaces, and len(name) counts characters. None of them change name itself.

Strings are immutable, which means a method that seems to edit one is really building a second string. To keep the change, assign it back: name = name.upper(). Forgetting that assignment is why a program can print the old text after an operation that looked like it worked.

Printing a name is not the same as reading it

When a value looks wrong, print the name at the moment it is used rather than reading upward through the file and guessing. The value at that line is the only one the next line will see, whatever the earlier lines intended. A name that looks wrong in the output was usually given the wrong value several lines above, and the print narrows the search to the lines in between.

The quieter trap is a name reused for two jobs. total that starts as a count and later holds a price is not an error to Python — it is a new value under an old label — but the reader now has to hold both meanings at once. A fresh name costs nothing and says which one you mean.

Three traps: a name used before its first assignment raises NameError; adding text to a number raises TypeError; and calling name.upper() without assigning the result leaves name exactly as it was.

Try these next

  • Build a sentence from three variables — a name, a city and a job — with one f-string.
  • Print the same price with and without :.2f and compare the two lines.
  • Join two pieces of text with +, then try to join text and a number and read the error.

A price that formats itself

The item and price are ready. Print a label with the name, the price to two decimal places, and a total for three of them — all built with f-strings.

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 the line name = "Ada" do?