Chapter 1 · Foundations
Names and numbers
Step 1 of 22 · Concept
A name points at a value
By the end of this lesson you can name values, compute with numbers, and change what a name points at.
age = 15
= gives a name to a value: the name age points at the object 15. Python's word is binds: age is bound
to 15.
Step 2 of 22 · Concept
Four kinds of value
Every object has a type (lesson 2). These four come first:
| type | values | what it is |
|---|---|---|
int | 15, 0, -3 | a whole number |
float | 2.5, 3.0 | a number with a point |
str | "Ada", 'hi' | text, in quotes |
bool | True, False | yes or no |
type(x) gives the type of a value: print(type(15)) prints <class 'int'>. An int and a float together give
a float: 3 * 2.5 is 7.5.
Step 3 of 22 · Quiz
Which kind is it?
A program runs this line. What is the type of the object price points at?
price = 3.0
Step 4 of 22 · Concept
Python does arithmetic
| you write | means | gives |
|---|---|---|
7 + 2 | add | 9 |
7 - 2 | subtract | 5 |
7 * 2 | multiply | 14 |
7 ** 2 | 7 to the power 2 | 49 |
(1 + 2) * 3 | brackets first | 9 |
As in maths, ** goes first, then *, then + and -: 1 + 2 * 3 is 7. Brackets go before all of them.
Dividing has a part of its own in this lesson.
Step 5 of 22 · Quiz
What will it print?
Type exactly what this program prints.
width = 4
height = 3
print("Area:", width * height)
print("Perimeter:", 2 * (width + height))
Step 6 of 22 · Demo
See it run
Run it and compare with your answer. Then change width to 10 and run it again.
Step 7 of 22 · Concept
Ask Python directly: the REPL
Open the Terminal tab, type python3 and press Enter. Its first start takes a few seconds. This is the
REPL (read, evaluate, print, loop): it reads a line, works it out, and shows the value.
>>> 2 ** 10
1024
>>> width = 4
>>> width * 3
12
>>> type(2.5)
<class 'float'>
>>> exit()
>>> is the REPL waiting for you, and exit() leaves it. The REPL shows the value of each line that is a value;
width = 4 shows nothing. A program in main.py shows only what print prints.
Step 8 of 22 · Try it
Try it: add first
Run it: it prints 14, because * goes before +. Add brackets so that the + goes first and it prints 20.
Put the brackets around what Python must do first: 2 + 3.
Step 9 of 22 · Concept
= moves the arrow
age = 15
age = age + 1
Python works out the right side first: it reads age, 15, and makes a new object, 16. Then = points age at
it. That is rebinding: the arrow moves, and the 15 does not change.
Step 10 of 22 · Trace
Watch the arrows
Two names can point at one object. Press Next and watch the arrows.
score = 10
bonus = score
score = score + 5
print(score, bonus)- The
<module>frame has no names yet. Line 1 is next. - Line 1 made the object
10and boundscoreto it. Line 2 is next. bonus = scoregavebonusthe same arrow: two names, one object. Line 3 is next.score + 5made a new object,15, and onlyscore's arrow moved.bonusstill points at10. Line 4 is next.- Line 4 printed
15 10. The program is finished, and the stack is empty.
Step 11 of 22 · Quiz
Which name changed?
After line 3 has run, what does bonus point at?
score = 10
bonus = score
score = score + 5
print(score, bonus)Step 12 of 22 · Concept
+= is a shortcut
| shortcut | is short for |
|---|---|
score += 5 | score = score + 5 |
score -= 2 | score = score - 2 |
score *= 3 | score = score * 3 |
Each one makes a new object and moves the arrow, as = does.
Step 13 of 22 · Demo
The whole program
Run it. Each shortcut rebinds coins to a new number. The last line prints three types.
Step 14 of 22 · Exercise
Your turn: a receipt
Write a receipt for pencils.
- Bind
countto3andpriceto2.5. - Print
count, the wordpencils:and the total,count * price. - Add 2 to
countwith+=, then print the same line again.
The output must be exactly:
3 pencils: 7.5
5 pencils: 12.5
Three parts, from the top: bind the two names, print one line, then change count and print again.
One print with three values, commas between them: count, the text "pencils:" and count * price.
count += 2
Step 15 of 22 · Concept
/ always gives a float
| you write | gives | what it is |
|---|---|---|
7 / 2 | 3.5 | divide: always a float |
6 / 3 | 2.0 | still a float, even when it divides evenly |
7 // 2 | 3 | how many whole 2s fit in 7 |
7 % 2 | 1 | what is left over |
/, // and % go before + and -, as * does: 1 + 6 / 2 is 4.0.
Step 16 of 22 · Quiz
Predict the division
Type exactly what this line prints.
print(9 / 3, 9 // 2, 9 % 2)
Step 17 of 22 · Demo
See it run
Run it and compare with your answer.
Step 18 of 22 · Concept
Floats are close; whole numbers are exact
A float keeps about 16 significant digits, stored in binary. Most decimals, such as 0.1, are a tiny bit off in it:
print(0.1 + 0.2)
print(0.1 + 0.2 == 0.3)
print(2 ** 100)
0.30000000000000004
False
1267650600228229401496703205376
Python prints the shortest digits that read back as the same float. == asks "equal?" (lesson 6 explains it).
An int is exact: a whole number grows to fit. Try these lines in the REPL.
Step 19 of 22 · Exercise
Your turn: the average
Four scores are bound to names.
- Add them up into a name
total. - Print the average,
totaldivided by 4, with/. - Print its whole part with
//.
The output must be exactly:
Average: 8.5
Whole part: 8
/ goes before +, so a + b + c + d / 4 divides only d. Add first.
Bind total to the sum. Then two print lines: one with total / 4, one with total // 4.
total = a + b + c + d
Step 20 of 22 · Check yourself
Check yourself: a game score
No shape this time.
A player has 120 points and wins a bonus of 35. Every 50 points is a level.
- Bind
pointsto120, then add the bonus with+=. - Print the points.
- Print the level (how many whole 50s fit) and the points left over.
The output must be exactly:
Points: 155
Level: 3
Left over: 5
Change points once, before you print anything. Then each line of the output is one print with two values.
Step 21 of 22 · Challenge · optional
Challenge: swap two names
Optional.
Swap the two names: left must point at 8 and right at 3. Do not type the numbers again. The program must
print:
8 3
Try left = right, then right = left. It prints 8 8: after the first line both names point at 8, and nothing points at 3.
Keep the 3 first, in a third name: old = left. Or let Python move both arrows at once: write two names on the left of =, two values on the right.
Python can bind two names in one line: this binds x to 1 and y to 2. It works out the whole right side first
(lesson 11 explains how). Write the swap that way.
x, y = 1, 2
Step 22 of 22 · Recap
Recap
age = 15binds the nameageto an object.int,float,str,bool:print(type(15))shows<class 'int'>.age = age + 1makes a new object and moves only that arrow.+=is short for it.9 / 3is3.0, always afloat;9 // 2is4,9 % 2is1.0.1 + 0.2is not exactly0.3; anintis exact at any size.
Next: lesson 4 joins and formats text, and reads what the user types.
Review
4 questions from this lesson, in a new order each time. Answer from memory: 3 of 4 right on the first try passes. On a day after you finish the lesson, a pass earns 15 XP.
The review needs JavaScript.
width = 4
height = 3
print("Area:", width * height)
print("Perimeter:", 2 * (width + height))
print(2 + 3 * 4)
coins = 10
coins += 5
print("After a gift:", coins)
coins -= 3
print("After a snack:", coins)
coins *= 2
print("Doubled:", coins)
print(type(coins), type(2.5), type(True))
# 1. bind count (3) and price (2.5)
# 2. print the count and the total
# 3. add 2 to count with +=, print again
print(9 / 3, 9 // 2, 9 % 2)
a = 8
b = 9
c = 7
d = 10
# total, then the average with / and with //
# write your program here
left = 3
right = 8
# swap them here
print(left, right)