Vid2Book.com

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

TypeExampleUsed for
str"Bro"text
int25whole numbers
float10.99numbers with a decimal point
boolTrueyes/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")
One variable of each type, then an f-string

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

7:00โ€“8:40Building f-strings with several variables

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. 1. Which value is a float?

  2. 2. What does print(f"Hi {name}") do when name = "Ana"?