Lesson 2 · Unit 01
Variables
Names for values, types and f-strings
What you practise
- A name for a value, reassignment,
int,float,strandbool,type(), converting withint()andstr(), f-strings, names that say what they hold
Try a question from this lesson
Pick one
The first line gives the number 4 a name. What does the program show?
price = 4
print(price + 1)Correct answer
5
Why
After the first line, the name price stands for 4. So price + 1 is 4 + 1, and the program shows 5. There are no quote marks, so price is a name, not text.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Variables
A variable is a name for a value. price = 4 is an instruction: "from now on, price refers to 4". Read = as "becomes".
After that line, using the name gives you the value.
Pick names that say what they hold: price, not p. Names cannot contain spaces, so use an underscore: item_count.
price = 4
item_count = 3
print(price * item_count) # 12Concept
Types
Every value has a type. int is a whole number, float is a number with a decimal point, str is a string, and bool is one of the two values True and False.
type(...) tells you which one you have.
print(type(7)) # <class 'int'>
print(type(7.5)) # <class 'float'>
print(type("7")) # <class 'str'>
print(type(True)) # <class 'bool'>Concept
f-strings
Put an f in front of the opening quote mark and the string becomes a template. Python replaces each pair of curly brackets with the value of what is inside them.
Numbers need no converting, and you can even put a calculation in the brackets.
name = "Ada"
age = 36
print(f"{name} is {age}")
# Ada is 36
print(f"Next year: {age + 1}")
# Next year: 379 more exercises in the app
- 2× type the answer
- 3× multiple choice
- 1× fill the blanks
- 1× match the pairs
- 1× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.