Bitpath

Lesson 2 · Unit 01

Variables

Names for values, types and f-strings

13 exercisesabout 3 min

What you practise

  • A name for a value, reassignment, int, float, str and bool, type(), converting with int() and str(), 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?

Python
price = 4
print(price + 1)

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.

Python
price = 4
item_count = 3
print(price * item_count)   # 12

Concept

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.

Python
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.

Python
name = "Ada"
age = 36
print(f"{name} is {age}")
# Ada is 36
print(f"Next year: {age + 1}")
# Next year: 37

9 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.

One email when Bitpath is ready to try. Nothing else. Privacy