For the complete documentation index, see llms.txt. This page is also available as Markdown.

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 y

    • Test if any of the conditions are true x or y

    • Test if a condition is not true not z

  • Conditional evaluation

    • if statement

    • if-else

    • if-elif-else

    • Ternary operator

      • true_value if condition else false_value

Conditional Statements

Input:

Output:

Loops

  • Repeat a block of code a specified number of times or until some condition is met

  • while loop

  • for loop

  • Use break to terminate loop

Input:

Output:

Comparison Operators and Functions

Operator
Example

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

Last updated