Introduction to Statements
Every program you have written so far has run top to bottom, once each. That is one way for a program to behave, and it is the only one you have needed. This chapter is about the others — the statements that let a program choose, repeat, and leave early.
1First, what a statement is
A statement is one instruction that Python carries out. That is the whole definition. You have been writing them since the first lesson without needing the word:
marks = 40
marks = marks + 10
print(marks)50
Three lines, three statements: store 40, work out 50 and store that, show it. Python did them in the order they were written, each exactly once, and then the program ended.
2 + 3 is 5, marks > 40 is True. A statement is something Python does. Expressions usually live inside statements: marks = 2 + 3 is one statement containing one expression.2The five kinds of statement
Python organises that order with five kinds of statement. You have been using one of them all along without knowing its name; the other four are the lessons that follow this one.
Tap any one to see the shape it makes. Only the first is due today — the rest are the lessons after this.
Sequential
you know this one“Do this, then this, then this.”
One statement after another, top to bottom, each one exactly once. This is what Python does unless you tell it otherwise — which is why every program you have written so far is made only of these.
no keyword — it is the default| Kind | It answers | Written with |
|---|---|---|
| Sequential | Do this, then this, then this. | nothing — it is the default |
| Selection (conditional) | Should I run this at all? | if · elif · else |
| Iterative (looping) | Should I do that again? | for · while |
| Empty | What if I want to do nothing? | pass |
| Jump | Can I leave early? | break · continue |
3If that felt like a lot, read this
Nobody expects you to be able to write an if or a while after reading this page. You have not been taught them, and you are not meant to know them yet.
For now, one thing is enough: Python has five kinds of statement — sequential, selection, iterative, empty and jump. That is the whole take-away. Each of them gets a lesson to itself in the sections that follow, with examples you can run, and every one of them will be explained from the beginning when you get there. Come back to this page after those lessons and you will find it reads as a summary rather than as a preview.
Which kind of statement have you been writing since your very first program?
Another name for a selection statement is: