Skip to content

Basics & Syntax

Python was designed around a single guiding principle: code should read like plain English. Guido van Rossum, Python’s creator, made explicit trade-offs in favour of readability — even when that meant restricting programmer freedom.

The philosophy is captured in PEP 20, known as The Zen of Python. Run import this in any Python session to see it. Three lines that shape everything:

  • Beautiful is better than ugly.
  • Explicit is better than implicit.
  • There should be one — and preferably only one — obvious way to do it.

That last point matters most for learning: when you look up how to do something in Python, there is almost always a canonical answer.

Python uses indentation (4 spaces per level, by convention) to define blocks. There are no curly braces and no semicolons.

def is_even(n: int) -> bool:
if n % 2 == 0:
return True
return False
for i in range(5):
if is_even(i):
print(i, "is even")

Mixing tabs and spaces causes a TabError. The community standard (PEP 8) is 4 spaces — configure your editor once and forget about it.

LessonCore concept
Variables & TypesDynamic typing, built-in types, truthiness
Control Flowif/elif/else, for, while, walrus :=
Functionsdef, parameters, return values, LEGB scope
StringsMethods, slicing, f-strings, immutability

Each lesson is self-contained and ends with a runnable playground, a callout, a quiz, and a progress checkpoint.

The script below demonstrates the core syntax you will encounter in every lesson: variables, a function, a loop, and an f-string.

def describe(value: int) -> str:
"""Return a short description of a number."""
if value < 0:
label = "negative"
elif value == 0:
label = "zero"
else:
label = "positive"
return f"{value} is {label}"
numbers = [-3, 0, 7, 42]
for n in numbers:
print(describe(n))
total = sum(numbers)
print(f"Sum: {total}")
print("First program complete.")
What does PEP 20 ('The Zen of Python') emphasise most strongly?
How does Python delimit code blocks such as function bodies and if-statements?
Which PEP defines Python's community style guide (4-space indentation, line length, etc.)?
In Python, what is true of integers, strings, and functions?