Python in 15 Minutes for JavaScript Developers
Python syntax maps directly to JavaScript syntax — indentation replaces braces, list comprehensions replace map/filter, and everything else is a 1:1 translation.
You already know how to program. You think in functions, loops, conditionals, and data structures every day. Python isn't a new language to learn — it's a new accent on concepts you already own.
Every ML tutorial, research paper, and reference implementation you'll encounter is written in Python. You don't need to become a Python developer. You need to read Python the way you read TypeScript even if you mostly write JavaScript — fluently enough to understand what's happening.
Learning Objectives
- ○Read Python syntax and mentally translate it to JavaScript
- ○Understand indentation-based scoping vs. brace-based scoping
- ○Map list comprehensions to Array.map/filter chains
- ○Recognize Python classes, type hints, and import patterns
Braces vs. Indentation
The single biggest visual difference: Python uses indentation where JavaScript uses braces.
Frontend
JavaScript Syntax
const items = arr.filter(x => x > 0).map(x => x * 2)Machine Learning
Python Syntax
items = [x * 2 for x in arr if x > 0]function processSignal(data, threshold = 0.5) {
const results = [];
for (const value of data) {
if (value > threshold) {
results.push(value * 2);
}
}
return results;
}def process_signal(data, threshold=0.5):
results = []
for value in data:
if value > threshold:
results.append(value * 2)
return resultsLine for line, the logic is identical. The differences are cosmetic: function becomes def, braces become colons plus indentation, push becomes append, and camelCase becomes snake_case.
Variables and Types
Python has no const, let, or var. You just assign. Type hints exist but are optional — like TypeScript annotations that never get enforced at runtime.
# Python
name: str = "tensorcraft" # type hint (optional)
count: int = 42
values: list[float] = [1.0, 2.0, 3.0]
config: dict[str, int] = {"epochs": 10, "batch_size": 32}
# Equivalent JavaScript
# const name: string = "tensorcraft";
# const count: number = 42;
# const values: number[] = [1.0, 2.0, 3.0];
# const config: Record<string, number> = { epochs: 10, batchSize: 32 };List Comprehensions vs. map/filter
This is the pattern you'll see most in ML code. Python list comprehensions are compact, and once you see the template, you can read them instantly.
# Python list comprehension
squares = [x ** 2 for x in range(10)]
evens = [x for x in data if x % 2 == 0]
pairs = [(x, y) for x in rows for y in cols]
# JavaScript equivalents
# const squares = Array.from({length: 10}, (_, x) => x ** 2);
# const evens = data.filter(x => x % 2 === 0);
# const pairs = rows.flatMap(x => cols.map(y => [x, y]));Classes
Python classes look different but work the same way. self is explicit (like writing this everywhere), and __init__ is the constructor.
class DataProcessor:
def __init__(self, batch_size: int = 32):
self.batch_size = batch_size
self.history = []
def process(self, data: list[float]) -> list[float]:
result = [x / max(data) for x in data]
self.history.append(len(data))
return result
# JavaScript equivalent
# class DataProcessor {
# constructor(batchSize = 32) {
# this.batchSize = batchSize;
# this.history = [];
# }
# process(data) {
# const result = data.map(x => x / Math.max(...data));
# this.history.push(data.length);
# return result;
# }
# }Imports
Python's import system maps cleanly to JavaScript ES modules.
# Python
import numpy as np # import * as np from 'numpy'
from torch import nn # import { nn } from 'torch'
from torch.optim import Adam # import { Adam } from 'torch/optim'
from pathlib import Path # import { Path } from 'pathlib'
# JavaScript equivalents
# import * as np from 'numpy';
# import { nn } from 'torch';
# import { Adam } from 'torch/optim';
# import { Path } from 'pathlib';Challenge
Time to put the translation to work. Read Python code and write the JavaScript equivalent.
Exercise
Translate Python to JavaScript
You're reading a Python ML utility module. Translate the Python class below into its JavaScript equivalent. The Python code defines a DataNormalizer class with fit() and transform() methods. Implement the same class in JavaScript using standard array methods. Focus on producing identical behavior — the logic is the same, only the syntax changes.
Key Takeaways
- ✓Python syntax is a 1:1 translation of JavaScript — same logic, different punctuation
- ✓Indentation replaces braces, def replaces function, self replaces this
- ✓List comprehensions are compact map/filter chains — read them right to left
- ✓You don't need to write Python — you need to read it fluently