Python Introduction¶
Python is a simple, readable programming language that's great for beginners and powerful enough for production systems. It's used everywhere — websites, AI, data science, scripting, automation.
Why Python?¶
- Easy to read — looks almost like English.
- No semicolons or curly braces — uses indentation (spaces) instead.
- Huge library ecosystem — there's a library for almost anything.
- Free and open-source.
Your first Python program¶
Click ▶ Run to see it execute in your browser:
Expected output:
print(...) is a built-in function. Anything inside the brackets gets shown on screen.
Print multiple things¶
print separates each argument with a space by default.
Change the separator with sep=:
Change what's at the end of the line with end=:
Indentation matters¶
Unlike most languages, Python uses indentation (the spaces at the start of a line) to group code. No curly braces.
if 5 > 3:
print("Five is bigger")
print("This is also inside the if")
print("This is outside the if")
Rule: indentation must be consistent — most people use 4 spaces. Mixing tabs and spaces causes errors.
Wrong indentation gives an IndentationError:
Run that — it will fail with IndentationError. Now fix it (add 4 spaces in front of print) and re-run.
Practice¶
Make the loading message print on one line
Expected: Loading...done
Quiz — Quick check¶
What you remember
Q1. What separates Python blocks of code (e.g. the body of an if)?
- Curly braces
{ } - Semicolons
; - Indentation (spaces or tabs)
- Parentheses
( )
Why: Python uses indentation to define blocks. Most other languages use
{}— Python's choice makes code visually consistent.
Q2. What does print("a", "b", "c", sep="|") produce?
-
a b c -
a|b|c -
abc -
a, b, c
Why:
sepcontrols what's placed between the arguments. The default is a single space.
Q3. Why does the buggy snippet if 5 > 3:\nprint("hi") fail?
-
printis misspelled - Missing
;afterif 5 > 3: - The body of
ifmust be indented -
5 > 3returnsNone
Why: Python requires the body of any block (
if,for,def, …) to be indented. Without indentation, Python doesn't know what's inside theif.
Common doubts¶
Do I need to install anything to start writing Python?
For real projects, yes — download Python from python.org. For these chapters, you don't need to install anything; the ▶ Run button uses Pyodide (Python compiled to WebAssembly) right in your browser.
Does Python need semicolons at the end of each line?
No. Python uses newlines to end a statement. You can put multiple statements on one line with ; (e.g. a = 1; b = 2) but it's considered bad style — write one statement per line.
Why 4 spaces specifically for indentation?
The Python style guide (PEP 8) recommends 4 spaces. The language only cares that indentation is consistent within a block — any consistent number works, but 4 spaces is what almost every Python codebase uses. Don't mix tabs and spaces in the same file; mixing causes IndentationError.