Lesson 5 +10 XP

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 + 2 or "hi").
  • An expression can sit inside a statement: print(2 + 2) prints the value of the expression.

Simple statements

  • x = 10 is an assignment statement.
  • print(x) is a call to the built-in print function.
  • pass is a do-nothing statement used as a placeholder.
  • return value sends a value back out of a function.
  • break and continue change 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.