Phase 1 — Core Foundations
for, range() & while
Looping in Python — one of the bigger syntax changes from JS/TS
for loop
This is one of the biggest syntax changes from JS.
JavaScript:
const users = ['John', 'David'];
for (const user of users) {
console.log(user);
}Python:
users = ["John", "David"]
for user in users:
print(user)Much cleaner — no const, no of keyword.
range()
Python commonly uses range() for number-based loops.
for i in range(5):
print(i)0
1
2
3
4Important: range(5) stops before 5 — similar to for (let i = 0; i < 5; i++).
Start and end
for i in range(2, 6):
print(i)2
3
4
5Step
for i in range(0, 10, 2):
print(i)0
2
4
6
8Think of it as range(start, stop, step).
while
JavaScript:
let count = 0;
while (count < 5) {
console.log(count);
count++;
}Python:
count = 0
while count < 5:
print(count)
count += 1Notice: count++ is not valid Python syntax. Use count += 1 instead.