Stop 03 · Python for Data ScienceTopics · 1/9

Python Fundamentals

Part of the Data Science 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.

Variables, control flow, functions, list/dict comprehensions and basic object-oriented concepts — the general-purpose Python fluency every data science library sits on top of.

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. 1Complete a beginner Python course focused on general programming fundamentals.
  2. 2Practice writing functions and using list/dict comprehensions instead of manual loops.
  3. 3Learn to read Python tracebacks and debug from the error message, not by guessing.

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