Chapter 25:49โ16:05
Variables โ
A variable is a named container for a value. Python has four basic kinds of value: strings, integers, floats and booleans.
A variable gives a name to a value so you can use it later. You create one with the assignment operator =: the name goes on the left and the value on the right.
The four basic data types
| Type | Example | Used for |
|---|---|---|
str | "Bro" | text |
int | 25 | whole numbers |
float | 10.99 | numbers with a decimal point |
bool | True | yes/no values |
python
first_name = "Bro"
age = 25
price = 10.99
is_student = True
print(f"Hello {first_name}")
print(f"You are {age} years old")The f before the opening quote makes an f-string. Inside it, anything in curly braces is replaced by the value of that variable, which is much easier to read than joining strings with +.
โถ Video clip 7:00โ8:40 โ https://youtu.be/ix9cRaBkVe0?t=420
Key takeaways
- Assign with = : name on the left, value on the right.
- str, int, float and bool cover most everyday values.
- f-strings insert variables into text with {curly braces}.
- Variable
- A name that refers to a value.
- f-string
- A string prefixed with f that substitutes {expressions} into the text.
- Boolean
- A True or False value.
Check your understanding
1. Which value is a float?
2. What does print(f"Hi {name}") do when name = "Ana"?