Defining, Calling And Blocks
Two ideas cause more early confusion than anything else, and both came up live in the first class.
Defining something is not running it. And indentation is what marks a block, where other languages use braces.
Get these two and most beginner bewilderment disappears. There is more on blocks in No Braces: Indentation Is The Syntax.
Defining a function does not run it
Someone in the first class typed this, ran it, and got nothing at all:
def add(a, b):
return a + bThey assumed something was broken. Nothing was.
Those two lines define the function. They tell Python that the name add now refers to this block. They do not execute it.
To run it, call it:
def add(a, b):
return a + b
print(add(4, 5))9Defining and calling are separate events, often far apart in your file. Holding that distinction is what later makes decorators, callbacks and graph nodes make sense — in all three you hand over a function now that something else calls later.
The parts
def add(a, b):
return a + bdef— defineadd— the namea, b— the arguments, the inputsreturn— sends back one value
Several inputs, a single return value. Same shape as every language you know.
The same thing in Java
int add(int a, int b) {
return a + b;
}What Python drops: the return type, the parameter types, the braces, the semicolons.
What it adds: def and a colon.
That is the entire difference.
Indentation is the block
In Java, C# or JavaScript a block is what sits between { and }. Python has no braces. The block is whatever is indented under the colon.
for student in students:
if student.startswith("A"):
print(student)
print("checked", student)Read it by column:
print(student)runs only for names starting with Aprint("checked", ...)runs for everyone
That is brace-inside-brace, expressed as spacing.
The stray space
The error people hit on their first function:
def add(a, b):
return a + b
print(add(4, 5))IndentationError: unexpected indentOne space before print and Python thinks the line belongs to a block. Outer-level lines start at column zero.
The editor indents correctly for you after a colon. It goes wrong when you add or delete a space by hand.
Case matters
Print("hello")NameError: name 'Print' is not definedprint and Print are different names, exactly as name and Name are different variables. This catches several people in every first class.
In a notebook, order is what you ran
A notebook does not run top to bottom unless you make it. It runs cells in the order you pressed Shift + Enter.
So a variable can exist because of a cell you have since deleted, and code that works for you can fail for someone opening it fresh.
When a notebook behaves strangely: Runtime → Restart and run all. Do this before sharing one.
Practise this
- Define a function and run the cell. Confirm you get nothing. Then call it.
- Write a function returning the product of two numbers, and call it with 6 and 7.
- Add a single space before an outer-level line and read the error.
- Type
Printwith a capital P on purpose, then fix it.