liveBook · Manning

Statement

As explained in The Quick Python Book, Fourth Edition

A statement is a syntactic unit representing an executable instruction in a programming language.

A statement is an executable Python instruction. Simple statements include assignments, function calls, print, pass, and del. A block contains one or more statements, usually separated by newlines. section 8.10

Python uses indentation to delimit blocks. Compound statements such as if-elif-else, while, and for contain clauses followed by indented blocks; compound statements can also nest inside other blocks. section 8.10

Keep these syntax rules in mind:

  • Indent every statement belonging to a clause at the same level. section 8.10
  • Use spaces consistently; mixing tabs and spaces can raise indentation errors. section 8.10
  • Separate ordinary statements with newlines; semicolons can place multiple statements on one line, though this is usually poor style. section 8.10
  • Use pass when a required statement should perform no action. section 8.1

A statement inside a conditional block

guess = 4 target = 7 if guess < target: print("Low") elif guess == target: print("Correct!") else: print("High") # Low section 8.1

Statements as a basic script

def main(): print("this is our first test script file") main() section 11.1

compound statement clause:
   block                  
compound statement clause: 
   block                  

Use this compact pattern to see how a compound statement pairs each clause with an indented block. section 8.10

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines statements, blocks, indentation, compound clauses, and errors, but stops short of implementation internals.
Examples
Uses assignments, function calls, print, pass, del, conditional branches, nested blocks, and a small file script.
What you can do after
Build basic Python scripts and diagnose indentation errors in simple and compound statements.
How it connects
Connects statements with blocks, indentation, compound statements, control flow, functions, and scripts.

Also explained in

Compare how 3 books explain statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.