Every value in Python has a type. Understanding the built-in types — and their mutability — is the difference between code that works in toy examples and code that works in production.
The full type tree
| Category | Types |
|---|---|
| Numeric | int, float, complex |
| Text | str |
| Sequence | list, tuple, range |
| Mapping | dict |
| Set | set, frozenset |
| Boolean | bool |
| Binary | bytes, bytearray |
| None | NoneType |
Numbers — int, float, complex
Python has three numeric types. int for whole numbers (unlimited precision), float for 64-bit decimals, and complex for complex numbers (rarely used outside scientific code).
a = 10 # intb = 3.14 # floatc = 2 + 3j # complex (the j is the imaginary unit)print(type(a), type(b), type(c))# <class 'int'> <class 'float'> <class 'complex'># int has unlimited precisionhuge = 2 ** 1000print(len(str(huge))) # 302 digits — no overflow
from decimal import Decimalprint(Decimal("0.1") + Decimal("0.2")) # 0.3 — exact
Strings (str)
single = 'hello'double = "hello"triple = """spansmultiple lines"""raw = r"C:\Users\Aanya" # backslashes are literalprint(len(single)) # 5print(double[0]) # 'h'print(double[-1]) # 'o' — negative indexing from the endprint(double[1:4]) # 'ell' — slicing
Common string methods:
s = "Albero Academy"print(s.lower()) # albero academyprint(s.upper()) # ALBERO ACADEMYprint(s.replace("a", "@")) # Albero @c@demyprint(s.startswith("Alb")) # Trueprint(s.split(" ")) # ['Albero', 'Academy']print("-".join(s.split())) # Albero-Academy
Booleans (bool)
bool is technically a subclass of int — True equals 1 and False equals 0. This lets you do tricks like sum a list of booleans:
results = [True, False, True, True, False]print(sum(results)) # 3 — counts the Trues# Truthiness — what counts as Falseprint(bool(0)) # Falseprint(bool("")) # False — empty stringprint(bool([])) # False — empty listprint(bool(None)) # Falseprint(bool("False")) # True — non-empty string is truthy
NoneType
None is Python's "nothing" value. It's the default return of any function that doesn't explicitly return.
def first_greater_than(numbers, threshold):for n in numbers:if n > threshold:return nreturn None # explicit "not found"result = first_greater_than([1, 2, 3], 100)if result is None:print("nothing matched")
Type checking & isinstance
x = 42# type() returns the exact classprint(type(x)) # <class 'int'>print(type(x) is int) # True# isinstance() respects subclassing — preferredprint(isinstance(x, int)) # Trueprint(isinstance(True, int)) # True — bool is a subclass of int!# Check multiple typesprint(isinstance(x, (int, float))) # True
Type conversion in depth
| Function | Converts to | Example |
|---|---|---|
| int(x) | integer | int("42") → 42 |
| float(x) | float | float("3.14") → 3.14 |
| str(x) | string | str(42) → "42" |
| bool(x) | boolean | bool(0) → False |
| list(x) | list | list("abc") → ["a","b","c"] |
| tuple(x) | tuple | tuple([1,2,3]) → (1,2,3) |
| set(x) | set | set("abca") → {"a","b","c"} |
# Common pitfallsint("3.14") # ValueError — int() can't parse decimals from stringsint(3.14) # 3 — but it can truncate from a float# Safe parsingdef to_int(value, default=0):try:return int(value)except (ValueError, TypeError):return defaultprint(to_int("42")) # 42print(to_int("oops")) # 0
Mutable vs immutable
This is the most important distinction in Python and trips up most beginners:
| Mutable | Immutable |
|---|---|
| list | int, float, complex |
| dict | str |
| set | tuple |
| bytearray | frozenset, bytes |
| (custom classes) | NoneType, bool |
# Immutable: every "change" creates a new objects = "hello"s += " world" # creates a new string, rebinds sprint(s) # hello world# Mutable: same object, modified in placenums = [1, 2, 3]nums.append(4) # modifies the list in placeprint(nums) # [1, 2, 3, 4]# This causes the most-common Python bug: shared mutable defaultsdef add_item(item, basket=[]): # ⚠️ default is shared!basket.append(item)return basketprint(add_item("apple")) # ['apple']print(add_item("banana")) # ['apple', 'banana'] ← surprising!
def add_item(item, basket=None):if basket is None:basket = []basket.append(item)return basket
Key takeaways
- 01Python has three numeric types: int (unlimited precision), float (IEEE 754), complex.
- 02For money, use Decimal — never float — to avoid 0.1 + 0.2 ≠ 0.3 issues.
- 03Strings are immutable; every operation returns a new string.
- 04bool is a subclass of int — True == 1, False == 0. You can sum booleans.
- 05Use isinstance() not type() == — it respects inheritance.
- 06Memorise mutable vs immutable — and never use a mutable default argument.
