Lesson 3
Types
Every value has a type: what kind of thing it is. The four you will use most:
str
Text: "Ahoy!". Always in quotes.
int
Whole numbers: 42, -3.
float
Decimal numbers: 3.14, 1.5.
bool
Yes or no: True, False.
You can tell Python what type you mean with a type hint: age: int = 12. Python does not need it, but ty uses it to catch
mistakes like adding text to a number before you run the program.
Lesson 3 · Try it
Play with types
Press Run, then try to:
- What does
print(age + "1")do? Read the error message carefully. - What does
print(7 / 2)print? Andtype(7 / 2)? - What is
type(True)? Andtype("True")?
Press ▶ Run to see what happens.
Lesson 3 · Exercise
Fix the bug
This program crashes. Read the error, find the line, and fix it so it prints You have 12 apples. Only change the line with the 💥.
Press ▶ Run to see what happens.
💡 Need a hint?
apples is text ("10"), not a number. Turn it into a number with int(apples) before adding.
Lesson 3
See ty in action
Paste the broken program from the exercise into a file in Zed, before you fix it.
apples = "10"
more = 2
total = apples + moreZed underlines the third line in red, and hovering over it explains why. That is ty, and it is why we set it up: it finds this kind of mistake without running anything.