Variables and text
Names for values: store a piece of text, change it once, and drop it into a sentence with an f-string.
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.
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
:.2fand 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.
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?
