Python Variable Types: str, int, float and bool
Every Value Has a Type
Every value in Python has a type: the kind of value it is. Text is one type, whole numbers are another, decimal numbers another.
Think of a kitchen: water goes in a bottle, rice in a jar, eggs in a box. Each kind of thing has its own container, and you handle it in its own way. Python treats each type of value in its own way too.
Python figures out the type for you
In some languages, like C or Java, you must say the type when you create a variable. In Python, you don't:
city = "London"
score = 100
print(city, score)London 100
Python looks at the value and knows: "London" is text, 100 is a whole number. This is called dynamic typing.
Why types matter
The type decides what you can do with a value. You can multiply numbers, but not names:
print(5 * 2)
print("5" * 2)10 55
The same * 2 does two different things. With the number 5, it multiplies. With the text "5", it repeats it. Same symbols, different types, different results. That's why knowing a value's type helps you predict what your code will do.
The four basic types
| Type | Name in Python | Example |
|---|---|---|
| Text | str (string) | "Sunny" |
| Whole number | int (integer) | 25 |
| Decimal number | float | 19.99 |
| True or False | bool (boolean) | True |
You'll meet each one in the next lessons.
Every value has a type, and Python works it out from the value.