Loading lessons...
Statements
Statements
A statement is one complete instruction for Python. Programs are made of statements run in order, from top to bottom.
x = 5
print(x)
x = x + 1
print(x)
This runs the four statements one after another.
Statements and expressions
- A statement does something (assigns, prints, loops).
- An expression produces a value (like
2 + 2or"hi"). - An expression can sit inside a statement:
print(2 + 2)prints the value of the expression.
Simple statements
x = 10is an assignment statement.print(x)is a call to the built-inprintfunction.passis a do-nothing statement used as a placeholder.return valuesends a value back out of a function.breakandcontinuechange loop behavior.
Compound statements
A compound statement has a header and an indented block, like if, while, for, and def.
if x > 0:
print("Positive")
The header line ends with a colon, and the block follows.
Blank lines
Python ignores blank lines. Use them to make your code easier to read.
TL;DR
- A statement is one instruction.
- An expression produces a value.
- Simple statements: assignment, print, pass, return.
- Compound statements: if, while, for, def, with a colon plus block.