Skip to main content
Albero Academy

Python Data Types

Deep dive into Python's data types — integers, floats, strings, booleans, None, type conversion, and how they work in memory.

Chapter 3Python18 min readBeginner friendly
  • #Python
  • #Data Types

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

CategoryTypes
Numericint, float, complex
Textstr
Sequencelist, tuple, range
Mappingdict
Setset, frozenset
Booleanbool
Binarybytes, bytearray
NoneNoneType

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

Python
a = 10 # int
b = 3.14 # float
c = 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 precision
huge = 2 ** 1000
print(len(str(huge))) # 302 digits — no overflow
Python
from decimal import Decimal
print(Decimal("0.1") + Decimal("0.2")) # 0.3 — exact

Strings (str)

Python
single = 'hello'
double = "hello"
triple = """spans
multiple lines"""
raw = r"C:\Users\Aanya" # backslashes are literal
print(len(single)) # 5
print(double[0]) # 'h'
print(double[-1]) # 'o' — negative indexing from the end
print(double[1:4]) # 'ell' — slicing

Common string methods:

Python
s = "Albero Academy"
print(s.lower()) # albero academy
print(s.upper()) # ALBERO ACADEMY
print(s.replace("a", "@")) # Albero @c@demy
print(s.startswith("Alb")) # True
print(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:

Python
results = [True, False, True, True, False]
print(sum(results)) # 3 — counts the Trues
# Truthiness — what counts as False
print(bool(0)) # False
print(bool("")) # False — empty string
print(bool([])) # False — empty list
print(bool(None)) # False
print(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.

Python
def first_greater_than(numbers, threshold):
for n in numbers:
if n > threshold:
return n
return None # explicit "not found"
result = first_greater_than([1, 2, 3], 100)
if result is None:
print("nothing matched")

Type checking & isinstance

Python
x = 42
# type() returns the exact class
print(type(x)) # <class 'int'>
print(type(x) is int) # True
# isinstance() respects subclassing — preferred
print(isinstance(x, int)) # True
print(isinstance(True, int)) # True — bool is a subclass of int!
# Check multiple types
print(isinstance(x, (int, float))) # True

Type conversion in depth

FunctionConverts toExample
int(x)integerint("42") → 42
float(x)floatfloat("3.14") → 3.14
str(x)stringstr(42) → "42"
bool(x)booleanbool(0) → False
list(x)listlist("abc") → ["a","b","c"]
tuple(x)tupletuple([1,2,3]) → (1,2,3)
set(x)setset("abca") → {"a","b","c"}
Python
# Common pitfalls
int("3.14") # ValueError — int() can't parse decimals from strings
int(3.14) # 3 — but it can truncate from a float
# Safe parsing
def to_int(value, default=0):
try:
return int(value)
except (ValueError, TypeError):
return default
print(to_int("42")) # 42
print(to_int("oops")) # 0

Mutable vs immutable

This is the most important distinction in Python and trips up most beginners:

MutableImmutable
listint, float, complex
dictstr
settuple
bytearrayfrozenset, bytes
(custom classes)NoneType, bool
Python
# Immutable: every "change" creates a new object
s = "hello"
s += " world" # creates a new string, rebinds s
print(s) # hello world
# Mutable: same object, modified in place
nums = [1, 2, 3]
nums.append(4) # modifies the list in place
print(nums) # [1, 2, 3, 4]
# This causes the most-common Python bug: shared mutable defaults
def add_item(item, basket=[]): # ⚠️ default is shared!
basket.append(item)
return basket
print(add_item("apple")) # ['apple']
print(add_item("banana")) # ['apple', 'banana'] ← surprising!
Python
def add_item(item, basket=None):
if basket is None:
basket = []
basket.append(item)
return basket

Key takeaways

  1. 01Python has three numeric types: int (unlimited precision), float (IEEE 754), complex.
  2. 02For money, use Decimal — never float — to avoid 0.1 + 0.2 ≠ 0.3 issues.
  3. 03Strings are immutable; every operation returns a new string.
  4. 04bool is a subclass of int — True == 1, False == 0. You can sum booleans.
  5. 05Use isinstance() not type() == — it respects inheritance.
  6. 06Memorise mutable vs immutable — and never use a mutable default argument.