Reference · module 1
First lines
Everything this module explains, on one page.
Make a program say something, and know why the quotes are there.
- print("Hello") shows Hello on the screen. The name print says what to do, the brackets hold what to do it to, and that pair — name plus brackets — is how everything in Python is called.
- The quotes are not decoration. "Hello" is text and Hello without them is a name Python will go looking for and fail to find. Quotes are how you say: this is not a name, it is letters.
- A program runs top to bottom, one line at a time, and each print starts a new line of output. Two prints give two lines, always in the order they are written.
The mistake you are about to make
print(Hello)
print("Hello")
The quotes look like decoration and the word inside looks like a word. Without them Hello is a name: Python goes looking for it, fails, and stops with a NameError.
Variables
Store a value under a name and change it.
- x = 5 stores five under the name x. The equals sign is not a claim that they are equal — it is an instruction: work out the right side, then put the result in the name on the left.
- Which is why x = x + 1 makes sense and is not a contradiction. The right side is worked out first with the old x, and only then does the name point at the new value.
- A name may hold letters, digits and underscores, and may not start with a digit. Use words: total, user_name, price. A program is read far more often than it is written.
The mistake you are about to make
x + 1
x = x + 1
In maths writing x + 1 is enough — that is the new value. In Python an expression on its own stores nothing: the result is computed and thrown away.
Numbers
Do arithmetic, and know which division you asked for.
- The four you expect work as written: 2 + 3, 7 - 1, 4 * 5, 9 / 2. Multiplication is a star, and the usual order applies — 2 + 3 * 4 is fourteen, not twenty.
- There are two divisions. 9 / 2 gives 4.5, always with a fractional part. 9 // 2 gives 4 — it throws the remainder away. And 9 % 2 gives that remainder, 1.
- A number with no dot is an int, a number with one is a float. Ordinary division always produces a float, which is why 4 / 2 is 2.0 rather than 2.
The mistake you are about to make
4 / 2 → 2
4 / 2 → 2.0
Two whole numbers ought to give a whole number. Ordinary division in Python always produces a float, even when it divides exactly. The whole one is //.
Strings
Join text together and put values inside it.
- A plus between two strings joins them: "Hello" + " " + "world". Nothing is added for you, so the space in the middle has to be written or the words run together.
- An f before the quotes lets a value go inside: name = "Anna" and then f"Hi {name}" gives Hi Anna. Whatever stands in the braces is worked out and dropped into the text.
- len("hello") is 5 — how many characters the text holds. Spaces count, because a space is a character like any other.
The mistake you are about to make
"Hello" + "world"
"Hello" + " " + "world"
Plus joins exactly what it is given and adds nothing of its own. The space between the words is a character like any other, and you have to type it.
Types
See why "5" and 5 are different, and convert between them.
- 5 is a number and "5" is text that happens to look like one. type(5) says int, type("5") says str, and almost every confusing error a beginner meets is this difference.
- "5" + 5 is an error, and that is Python refusing to guess. Should it be 10, or "55"? Both are reasonable, so it stops and asks you to say which.
- You say which by converting: int("5") turns the text into a number, str(5) turns the number into text. So int("5") + 5 is 10 and str(5) + "5" is "55".
The mistake you are about to make
"5" + 5
int("5") + 5
There is a five on both sides, so surely Python can work it out. It refuses on purpose: 10 and "55" are equally reasonable, so it stops and asks you.