Python for JS Devs · Fundamentals

Control flow & comprehensions

Loops are different, and comprehensions replace half your .map()/.filter() muscle memory.

Lesson 04Time ~13 min Win Rewrite a map+filter as one comprehensionPrereq Lesson 03

1 · Loops: there is no C-style for

Python's for is always "for-each". To loop over numbers you iterate a range. To get an index, you use enumerate — never manual counters.

JavaScript
for (let i = 0; i < 3; i++) {}
for (const x of xs) {}
xs.forEach((x, i) => {});
Python
for i in range(3): ...       # 0,1,2
for x in xs: ...             # for-of
for i, x in enumerate(xs): ...  # index + value

Two more you'll use daily: zip(a, b) pairs up iterables, and iterating a dict yields its keys (use .items() for pairs):

for name, score in zip(names, scores): ...
for key, value in user.items(): ...

2 · Conditionals & the ternary

JavaScript
if (a) {} else if (b) {} else {}
const label = ok ? "y" : "n";
switch (kind) { ... }
Python
if a: ...
elif b: ...
else: ...
label = "y" if ok else "n"   # value-first ternary
match kind:                    # structural, since 3.10
    case "a": ...
    case _: ...              # default
Note the shape The ternary reads value-first: X if cond else Y. And Python has no &&/||/! — it's the words and, or, not. (a or b still returns the first truthy value, like JS ||.)

3 · Comprehensions — the idiom that defines Python

A comprehension builds a list/dict/set in one expression. It replaces most .map() and .filter() chains, and idiomatic Python strongly prefers it.

JavaScript
xs.map(x => x * 2)

xs.filter(x => x > 0)
  .map(x => x * 2)

Object.fromEntries(
  pairs.map(([k,v]) => [k, v*2]))
Python
[x * 2 for x in xs]

[x * 2 for x in xs if x > 0]
# filter + map in one pass

{k: v * 2 for k, v in pairs}
# dict comprehension

The mental template: [ expression for item in iterable if condition ]. Swap the brackets for {} to get a set or (with k: v) a dict comprehension.

Taste Comprehensions are for building a collection. If you're doing side effects (printing, calling an API), use a plain for loop — don't abuse a comprehension for its effect. And if it needs two ifs and a nested loop, a normal loop is more readable.

🎯 Your win

Given nums = [5, -2, 8, -1, 3], in the REPL write a single comprehension that squares only the positive numbers → [25, 64, 9]. Then rewrite label = "pass" if score >= 50 else "fail" for a score you pick. Nailing the comprehension template is the win.

Check yourself

Q1. How do you get the index while looping a list?
for i, x in enumerate(xs) is the idiom — never a manual counter.
Q2. Python's ternary is written as:
Python's conditional expression is value-first: A if cond else B.
Q3. The best replacement for .filter().map() is:
[f(x) for x in xs if cond] does filter+map in one readable pass — the Pythonic choice.
Primary source Python docs — List Comprehensions. Short and canonical.
💬 Ask your teacher: when is a generator expression (x for x in xs) better than a list comprehension? Worth asking before Lesson 10.