Python Syntax
Learn indentation, comments, and how Python reads your code.
Why it matters: Indentation errors show up fast when you paste model code. Know the rules.
Basics
Indentation
Python uses indentation for blocks. Most languages use braces.
- A colon
:opens a block - Indent with four spaces
- Do not mix tabs and spaces
Common mistake: missing indent after a colon.
Tiny example
Indent under if. That block only runs when true.
score = 0.9
if score > 0.7:
print("keep")
print("done")Basics
Comments
# starts a comment to the end of the line.
Python has no block-comment syntax.
Comment the why, not the obvious what.
Tiny example
A comment explains why.
# offset because API is 1-indexed
print(0 + 1)Basics
Long Lines
Write one statement per line.
Long lines break cleanly inside (), [], or {}.
Prefer brackets over a backslash.
Tiny example
Break a dict across lines inside braces.
config = {
"model": "mini",
"temperature": 0.2,
}
print(config)Then AI / ML
AI / ML examples
Same idea, used in real AI work. Press Try in lab to run it.
AI / ML example
AI / ML: indented filter
Indented lines belong to the if.
In AI / ML: Score filters and training loops nest if and for the same way.
example.py
Python
score = 0.85
if score > 0.7:
print("high confidence")
print("always runs")AI / ML example 2
AI / ML: config on many lines
Break long configs inside braces.
In AI / ML: Model settings often use one key per line.
example.py
Python
config = {
"model": "gpt-4.1-mini",
"temperature": 0.2,
}
print(config["model"])Takeaways
- 1Indentation is syntax. Use four spaces.
- 2Comments start with #.
- 3Break long lines inside brackets.