Conditions and loops

Precap

The default order of execution is sequentially (= from top to bottom, one statement after the other).

Sometimes it is required to skip statements under certain conditions or to repeat statements a number of times. This can be done by using the built-in keywords

Sequential order of execution

By default code will be executed sequentially, i.e. one statement after the other.

Statements are executed one after the other (top-down)

Conditional execution

if statement

Sometimes it is necessary that not all but only selected statements have to be executed based on certain conditions. This type of control can be realized with the built-in keywords if, elif, and else. The basic structure of conditional code is:

Pseudo code showing the basic structure of conditional code

Conditional code always starts with the if keyword, followed by a condition (= expression that resolves to True or False) and a colon :. Note the mandatory indentation (= preceeded with usually 4 spaces ot 1 tab) of the following code block (= one ore more statements). Optionally alternative conditions can be considered using one or more elif keywords, or, at the end and without any condition an else keyword.
NOTE: The pseudo code above will throw an error and syntax coloring also fails.

Example: Test if a whole number is even or not

If the condition whole_number % 2 == 0 in line 2 evaluates to True, line 3 will be executed. If it evaluates to False line 5 will be executed.
NOTE: The modulo % operator in this exampe has the remainder as the result of a division between two numbers. E.g. 123 % 2 has 1 as a result, because 123 = 61 * 2 + 1.

Repeated execution

Sometimes it is necessary that selected statements are executed multiple times. This type of control can be realized with teh built-in keywords for, break, continue, while, and else. There are two options for repeated code: using a sequence with for or a condition with while.

for loops

The basic structure of a for loop is:

Pseudo code showing the basic structure of a for loop

Any sequence data type can be used as sequence (e.g. str, tuple, list).

Iterating over a squence of characters in a string

Iterating over a squence of items in a tuple

A sequence of numbers can be created with the built-in range() function.

Iterating over a squence of numbers using the range() function

Note that the numbers start at 0 and stop before 5. The range() function can be used in three ways:

  • Only a stop value: range(stop)

  • A start and a stop value: range(start, stop)

  • A start, stop and a step value: range(start, stop, step)

Example: A very basic loop using the range() function with start and stop values

Example: A very basic loop using the range() function with start, stop, and step values

Depending on the number of function arguments (= the values within the round brackets ()) the range() function can be controlled in different ways.

If you have already have a data type that is a sequence, You can directly use that:

Example: Iterating (= looping) over a list

Note that the indentation of the code decides which statements are part of the loop and which are not.

while loops

Pseudo code showing the basic structure of a while loop

References

https://docs.python.org/3/tutorial/controlflow.html