Every value in Python has a type, and the type decides what you can do with it. Knowing which type you are holding explains most confusing beginner errors.
print(type("hello")) # <class 'str'>
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type(True)) # <class 'bool'>str — text#
name = "Ayesha"
print(len(name)) # 6
print(name.upper()) # AYESHA
print(name[0]) # A
print("she" in name) # False (case matters)
print(name.replace("A", "@"))Strings cannot be changed in place. name.upper() gives you a new string; the original is untouched unless you reassign it.
int and float — numbers#
count = 7 # int
price = 19.99 # float
print(10 / 3) # 3.3333333333333335 — always a float
print(10 // 3) # 3 — whole-number division
print(10 % 3) # 1 — the remainder
print(2 ** 10) # 1024 — to the power ofThe % operator is more useful than it looks: n % 2 == 0 is how you test whether a number is even.
bool — True and False#
is_open = True
print(5 > 3) # True
print(bool("")) # False — empty things are falsy
print(bool([])) # False
print(bool(0)) # FalseEmpty strings, empty lists and zero all count as false in a condition. That is why if name: works as “did they type anything?”.
list, dict, tuple, set — collections#
scores = [10, 8, 9] # list — ordered, changeable
person = {"name": "Ada", "age": 36} # dict — labelled values
point = (3, 4) # tuple — ordered, fixed
unique = {1, 2, 2, 3} # set — no duplicates → {1, 2, 3}See Python lists and Python dictionaries for detail.
None — the absence of a value#
result = None
if result is None:
print("Nothing yet")Use is None, not == None. A function with no return statement returns None, which is behind a lot of “why is my variable empty” confusion.
Converting between types#
int("42") # 42
float("3.5") # 3.5
str(42) # "42"
list("abc") # ['a', 'b', 'c']
int(3.9) # 3 — chops off, does not round
round(3.9) # 4Conversion can fail loudly:
int("hello") # ValueError: invalid literal for int() with base 10Guard it when the value comes from a person:
raw = input("Enter a number: ")
if raw.isdigit():
number = int(raw)
else:
print("That was not a number.")Why 0.1 + 0.2 is not 0.3#
print(0.1 + 0.2) # 0.30000000000000004Floats are stored in binary, and some decimal fractions have no exact binary form — the same way 1/3 has no exact decimal form. This is not a Python bug; almost every language behaves this way.
For display, round it. For money, work in whole paisa or cents, or use the decimal module.
print(round(0.1 + 0.2, 2)) # 0.3Mutable versus immutable#
Some types can be changed in place; some cannot.
| Changeable | Fixed once created |
|---|---|
| list, dict, set | str, int, float, bool, tuple |
This is why b = a on a list means both names see the same data, but on a number it does not. Covered in Python variables explained.
Questions people ask#
How do I check a type in a condition?
Use isinstance(value, int) rather than comparing type(value) == int. It handles inheritance correctly and reads better.
What is the difference between a list and a tuple?
A tuple cannot be changed after it is created. Use one for fixed groupings such as coordinates or a database row, where accidental edits would be a bug.
Should I write type hints?
They are optional and Python does not enforce them, but editors use them for better autocomplete and error checking. Worth adopting once your files get past a hundred lines or so.