Skip to content

Module 2 · Python Syntax and Fundamentals — topic 3 of 14

Statements

A statement is a complete instruction that Python executes, such as an assignment, a print call or an if. Learn simple versus compound statements and how Python runs them.

Beginner 12 min read·Lesson 17 of 306 in Python Programming

Learning objectives

  • Define a statement and give examples
  • Distinguish simple and compound statements
  • Trace the order in which statements run

Definition

A statement is a complete instruction that Python executes, such as an assignment, a print call or an if. Learn simple versus compound statements and how Python runs them.

Introduction

A statement is one complete instruction: “store this value”, “print this”, “repeat this block”. A Python program is a sequence of statements, and the interpreter carries them out one after another.

Why it matters

Thinking in statements helps you read code like a recipe: each step does one thing. When something goes wrong, the traceback points to the statement that failed, so knowing where statements begin and end makes debugging faster.

Detailed explanation

Simple statements fit on one logical line. Examples: assignment (x = 5), a function call used for its effect (print(x)), import, return, pass and break.

Compound statements contain other statements in an indented block. They begin with a header line ending in a colon: if, for, while, def, class, try and with are compound statements.

Execution normally goes top to bottom. Compound statements change the flow: an if may skip its block, a loop may repeat its block, and def stores a block to run later.

You can put several simple statements on one line separated by semicolons, but it hurts readability, so style guides advise against it.

Syntax

Text
name = value # simple statement
header: # compound statement header
statement_1
statement_2
  • A simple statement is one complete action on one line.
  • A compound statement has a header ending in a colon and an indented body.
  • The body can contain simple or compound statements.

Code examples

Simple statements in sequence

Python
count = 3
count = count + 1
print("count is", count)
Output
count is 4

A compound statement controls others

Python
for step in [1, 2, 3]:
print("step", step)
Output
step 1
step 2
step 3

More examples

Tracing statement order

Python
def announce(item):
print("Packing", item)
items = ["laptop", "charger"]
print("Start")
for item in items:
announce(item)
print("Bag ready with", len(items), "items")
Output
Start
Packing laptop
Packing charger
Bag ready with 2 items

Step-by-step explanation

  • The def statement runs first but only creates the function; nothing is printed.
  • items = [...] is a simple assignment statement.
  • print("Start") is the first statement that produces output.
  • The for statement runs its body twice, and each body statement calls announce.
  • The final print runs after the loop has finished.

Expected output

Running “Tracing statement order” prints exactly:

TextOutput
Start
Packing laptop
Packing charger
Bag ready with 2 items

Practical applications

  • Debuggers step through a program one statement at a time.
  • Error messages name the line of the statement that failed.
  • Code coverage tools report which statements your tests actually ran.

Best practices

  • Keep each statement focused on one action.
  • Avoid semicolons to join statements.
  • Read programs statement by statement when tracing what they do.

Common mistakes

Cramming statements onto one line

Avoid

a = 1; b = 2; print(a + b)

Prefer

a = 1
b = 2
print(a + b)

It is valid Python but harder to read and to debug, because one line now holds three actions.

Expecting a def body to run immediately

Avoid

def greet():
print("hi")
# expecting "hi" to appear here

Prefer

def greet():
print("hi")
greet()

def only defines the function. Its statements run when you call it.

Key takeaways

  • A statement is a complete instruction Python executes.
  • Simple statements fit on one line; compound statements have a header and an indented body.
  • Execution is top to bottom unless a compound statement changes the flow.

Try it yourself

  1. 1
    Label each line of the intermediate example as simple or compound.
  2. 2
    Write three simple statements that compute and print the area of a 7 by 3 rectangle.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

10 questions cover the “Python Syntax and Fundamentals” module. Answers and explanations are revealed after you submit.

  • BeginnerWhat happens when this code runs?
  • BeginnerWhich line is a comment in Python?
  • BeginnerWhich of these is a Python keyword?
Practise this module