Variables and data types in Python
What a variable is, and what int, str, list and dict are for.
All lessons · Python lessons: learn from scratch
What is a variable?
A variable is a name for a value. The “=” sign stores the value under that name.
age = 15 name = "Aysel" price = 4.5 print(name, age, price)
Main data types
- int — whole number: 7, -3
- float — decimal number: 3.14
- str — text: "hello"
- bool — True or False
- list — changeable list: [1, 2, 3]
- dict — key: value pairs: {"name": "Aysel"}
fruits = ["apple", "pear"]
fruits.append("plum")
print(len(fruits), fruits[0])
user = {"name": "Aysel", "age": 15}
print(user["name"])
print(type(user))Converting types
input() always returns text. Convert it before doing maths:
x = int(input("Number: "))
print(x * 2)The most common mistake: "5" + 3 — text and a number cannot be added (TypeError).