# Python for Machine Learning

> The slice of Python that ML code actually leans on: data structures, functions, comprehensions, classes and reading other people's code. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/python-for-ml
Last updated: 2026-11-28

The slice of Python that ML code actually leans on: data structures, functions, comprehensions, classes and reading other people's code.

Beginner · about 20 hours · difficulty 2/5

Almost every ML library exposes a Python API, so fluency here pays off in every later topic. You do not need to be a software engineer yet, but you must be able to read and write short, clean scripts without fighting the syntax.

### What to be comfortable with

- **Core types**: lists, dicts, tuples, sets, and when each is the right container.
- **Control flow and functions**: default and keyword arguments, `*args` / `**kwargs`, returning multiple values.
- **Comprehensions and generators**: `[f(x) for x in xs if cond]` is everywhere in data code.
- **Classes**: enough to read `class MyModel(nn.Module)` and understand `__init__`, methods and inheritance.
- **Modules and packages**: `import`, relative imports, and splitting a notebook into a reusable `.py` file.
- **Files and paths**: `pathlib`, reading CSV/JSON, context managers (`with open(...)`).

### Pitfalls

Mutable default arguments (`def f(x=[])`), confusing copies with references when you modify lists, and writing everything as one giant notebook cell. Also avoid pure Python loops over millions of rows; that is what NumPy and pandas are for.

**Good enough to move on** when you can write a script that reads a CSV with the `csv` module, computes per-group averages with a dict, and prints a sorted summary, without looking up basic syntax.

## Key concepts

- **Built-in data structures**
- **Functions, args and kwargs**
- **Comprehensions and generators**
- **Classes and inheritance**: Needed to read PyTorch model code
- **Modules, packages and imports**
- **pathlib and file I/O**
- **Type hints and dataclasses**

## Mini project

Write a command-line script that loads the Titanic CSV with the csv module, computes survival rate by passenger class and sex using plain dicts, and prints a sorted table.

## Check yourself

Why is \`def add_item(item, bucket=\[\])\` a bug, and how do you fix it?

## Free resources

- [The Python Tutorial](https://docs.python.org/3/tutorial/) (python.org)
- [Kaggle Learn: Python](https://www.kaggle.com/learn/python) (Kaggle)

Next: [Environments, Packages and Jupyter](https://www.internhack.in/roadmaps/tracks/machine-learning/environments-and-notebooks)

## More in Python and the Data Workbench

- [Python for Machine Learning](https://www.internhack.in/roadmaps/tracks/machine-learning/python-for-ml)
- [Environments, Packages and Jupyter](https://www.internhack.in/roadmaps/tracks/machine-learning/environments-and-notebooks)
- [NumPy and Array Thinking](https://www.internhack.in/roadmaps/tracks/machine-learning/numpy)
- [pandas for Tabular Data](https://www.internhack.in/roadmaps/tracks/machine-learning/pandas)
- [Visualization with Matplotlib and Seaborn](https://www.internhack.in/roadmaps/tracks/machine-learning/data-visualization)
- [Git for ML Projects](https://www.internhack.in/roadmaps/tracks/machine-learning/git-for-ml)
- [SQL for Pulling Training Data](https://www.internhack.in/roadmaps/tracks/machine-learning/sql-for-data)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
