Lesson 01 · foundations
Same ideas you already have — for, if, functions — new punctuation.
You already know what a variable, a loop, a conditional, and a function are. This lesson is not about those concepts — it's a fast map from concepts you have to Python's specific syntax for them, so you can write and run a real script by the end.
Two ways you'll use constantly:
# save as hello.py, then:
python3 hello.py
# or start an interactive shell (REPL) — evaluates line by line:
python3
Languages with { } or begin/end use punctuation to mark a block. Python uses a colon and indentation — the whitespace is the syntax, not a style choice.
if (x > 0) {
print(x);
}
if x > 0:
print(x)
No int x = 5;. A name is just bound to a value, and can be rebound to a different type later (usually a code smell, but legal):
age = 27 # int
name = "Ada" # str
pi = 3.14 # float
is_ready = True # bool — capitalized, no quotes
print(f"{name} is {age}") # f-string: {expr} interpolated inline
snake_case, not camelCase. This isn't optional style — every library you'll read (including pandas) follows it, so code that doesn't looks foreign.
One block, all the pieces from above in context:
def classify(n):
if n < 0:
return "negative"
elif n == 0:
return "zero"
else:
return "positive"
numbers = [-2, 0, 5, 17]
for n in numbers:
print(n, "→", classify(n))
Notice: no return type declared, no semicolons, the for loop iterates directly over a collection (no index bookkeeping unless you ask for it), and def needs no parameter types.
Write and run a real script — don't just read the one above:
# save as fizzbuzz.py
def fizzbuzz(n):
if n % 15 == 0:
return "FizzBuzz"
elif n % 3 == 0:
return "Fizz"
elif n % 5 == 0:
return "Buzz"
else:
return str(n)
for i in range(1, 21):
print(fizzbuzz(i))
Run it: python3 fizzbuzz.py. If it prints 20 lines ending in 19, FizzBuzz — indentation, function, loop, and conditional all landed correctly.
Primary source — read this one: The Python Tutorial §4, "More Control Flow Tools". Covers if, for, range, and def in the authoritative detail this lesson compressed.