Python
Beginner 12 min read Python

Python Fundamentals Review

The 20% of Python you use 95% of the time

The data-science-relevant slice of Python: dynamic typing, built-in types, operators, truthiness and the iteration protocol — enough to read and write pandas and numpy idiomatically.

pythontypestruthinessiterationbuiltinsdunder methods
Prerequisites: Set Up an Env
Before we start

What you will learn

  • Distinguish immutable vs mutable types
  • Use truthiness and operator quirks without surprises
  • Understand the iteration/for-loop protocol
Introduction

What is this?

Data science leans on a narrow slice of Python: numbers, strings, booleans; the container types (list, dict, set, tuple); dynamic typing; and iteration. Getting the core semantics — what's mutable, what's `True`, what `.copy()` actually copies — prevents the silent bugs that plague notebooks.

Motivation

Why do you need it?

  • pandas and numpy are Python objects; Python semantics leak through (e.g., in-place mutation surprises).
  • Half of all 'wait, why did my column change?' mysteries are aliasing/mutation bugs.
  • List/dict comprehension fluency is the day-to-day speed diff between engineers.
  • Truthiness rules are why `if df['col']:` breaks mysteriously.
Think about it

Mental model

Imagine

A toolbox where a few tools do 90% of the work

Think of

Python builtins for data science

list, dict, set, tuple, range, zip, enumerate, sorted, len, sum — every notebook is built from these plus the scientific libraries.

Mechanics

How it works

  • Everything is an object with a type; `type(x)` and `isinstance(x, bool)` reveal it.
  • Immutable (int, float, str, tuple, frozenset) vs mutable (list, set, dict): mutation changes identity in place.
  • Assignment copies references, not data: `b = a` aliases; `b = a.copy()` duplicates.
  • Truthiness: 0, 0.0, '', [], {}, None are falsy; everything else truthy.
  • `for x in seq` calls `iter(seq)` then `next` repeatedly — the protocol pandas and numpy build on.

Python object basics.

Object
Immutable
Mutable
References
Behavior
Hands-on

Examples

Aliasing vs copying traps

Aliasing vs copying traps
a = [1, 2, 3]
b = a # alias — same object
c = a.copy() # new object
b.append(4)
print(a) # [1, 2, 3, 4] → b mutated a
print(c) # [1, 2, 3] → copy safe

`b = a` created a reference, so mutating b changed a. In pandas-land this is why you sometimes mutate frames unexpectedly — always confirm whether an API operates in place or returns a copy.

Real world

In production

  • A notebook bug where a shared list was appended to by a loop 'corrupted' all lookups — an aliasing issue.
  • A data pipeline uses tuple keys for immutability in a dict of aggregates.
  • Deep vs shallow copies of nested lists cause silent edits when preprocessing data.
Guidelines

Best practices

  • Prefer tuples for fixed records and dicts for lookup tables.
  • Use .copy() explicitly whenever you plan to mutate a passed-in container.
  • Test truthiness-of-empty containers instead of `is not None`.
Watch out

Common mistakes

  • Using a mutable default argument (`def f(x=[])`) — the default is shared across calls.
  • Mutating a list while iterating over it.
  • `if list:` checks non-empty — mixing that with `if x is not None` semantics.
When things break

Debugging tips

  • id(x) reveals aliasing: same id = same object.
  • For 'phantom mutation', check where a reference to the object escaped.
  • Test with `==` for values and `is` for singletons (None).
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

Which of these is IMMUTABLE in Python?

2

`b = a.copy()` and `b = a` are equivalent.

Go deeper

Official documentation