Stop 02 · Python for Machine LearningTopics · 1/7

Python Essentials for ML

Part of the Machine Learning Roadmap

In short

Python reads almost like English, which is why data people love it. Get comfortable with lists, dictionaries, loops and functions, and every library after this feels familiar.

You need comfortable Python before ML: variables, lists, dictionaries, loops, functions and reading errors. Most ML code is ordinary Python that calls library functions, so these basics show up in every notebook.

The mascot ticket examiner walks along train berths holding a clipboard.

Think of it like this

The TTE and the reservation chart

The chart maps each berth to a passenger name, like a dictionary. The TTE walks down the coach checking every berth one by one, like a loop. At each berth he asks 'ticket valid?', like an if. His checking routine, repeated every trip, is a function.

Watch it happen

Looping over marks, keeping passes

72
0
45
1
88
2
30
3

1/5A dictionary maps names to marks: Asha 72, Ravi 45, Meena 88, Kabir 30.

The big ideas

What you need to know

Lists keep order

A list like [72, 45, 88] holds items in order. Index from 0, and slice with marks[1:3].

Dicts look things up

A dictionary maps keys to values, like name to marks. Lookups by key are very fast.

Loops and comprehensions

for goes through items one by one. A list comprehension builds a new list in one readable line.

Functions package logic

def grade(score): wraps a rule once so you can reuse it everywhere instead of copy-pasting.

See it in code

Walk through the code

python
1marks = {"Asha": 72, "Ravi": 45, "Meena": 88, "Kabir": 30}2 3def grade(score):4    if score >= 75:5        return "A"6    elif score >= 50:7        return "B"8    return "Fail"9 10for name, score in marks.items():11    print(name, score, grade(score))12 13passed = [n for n, s in marks.items() if s >= 50]14print(passed)   # ['Asha', 'Meena']

1/4A dictionary: each student's name is a key, their marks the value.

Dictionary, function, loop and list comprehension: four building blocks you'll use in every data script.

Save yourself the pain

Common mistakes

Oops

Mixing tabs and spaces, or indenting randomly.

Do this instead

Use four spaces per level. Indentation is part of Python's grammar, not decoration.

Oops

Using a list and searching it again and again to find one student.

Do this instead

Use a dictionary keyed by name. One lookup instead of scanning the whole list.

Your mission

Your own marks report

~20 min

Quick check

What does [x * 2 for x in [1, 2, 3] if x > 1] give?

Go deeper

  1. 1Solve 15 easy Python problems on HackerRank or LeetCode without looking at solutions.
  2. 2Write a function that takes a list of marks and returns the average, highest and lowest.
  3. 3Practise reading tracebacks: break your code on purpose and find the line that failed.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge