Control Flow
In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. [1]
This page provides syntax for some of the common control flow methods in Python. Each section includes an example to demonstrate the described methods
Use Cases and Syntax
Test if a specified expression is true or false
Short-circuit evaluation
Test if all of the conditions are true
x and yTest if any of the conditions are true
x or yTest if a condition is not true
not z
Conditional evaluation
ifstatementif-elseif-elif-elseTernary operator
true_value
ifconditionelsefalse_value
Conditional Statements
Input:
Output:
Loops
Repeat a block of code a specified number of times or until some condition is met
whileloopforloopUse
breakto terminate loop
Input:
Output:
Comparison Operators and Functions
Equality
x == y
Inequality
x != y
Less than
x < y
Less than or equal to
x <= y
Greater than
x > y
Greater than or equal to
x >= y
Input:
Output:
Resources
Python Documentation: Control Flow
Python Wiki: For Loops
W3 Schools: Python For Loops
W3 Schools: Python Conditionals and If Statements
Last updated
