Python Course-Part 04

Alexander Wagner
4 min readMar 9, 2021

Control Structures

Table of contents

  1. Python Course-Part 01-Getting Started with Python| Development environment
  2. Python Course-Part 02-Getting Started with Python| Basics
  3. Python Course-Part 03-Data Types | Data Structures
  4. Python Course-Part 04-Control Structures
  5. Python Course-Part 05-Functions
  6. Python Course-Part 06-Database Access

A program is a sequence of many instructions. The sequential processing of these instructions is controlled by some control flow structures such as loops and conditional statements.

1. Conditional Statements

Conditional statements are used to perform different actions depending on whether a condition evaluates to true or false.

1.1 If-Statement

Python if Statement is used for decision-making operations. The conditional statement consists of a check if a certain condition is true. If this is the case, then a block of instructions is processed. Otherwise this block is ignored and the program continues with processing the code after the conditional block.

Syntax:
if expr:
statement

The following code demonstrates the implementation of a if-statement in Python.

The elements are

  • the statement starts with the keyword if
  • he condition can be complex, but it must evaluate to either True or False
  • after the condition, a colon : indicates the end of the checking-line
  • the following indented block is processed only if the condition is True
Output:
The only available paymentoption for this customer is ['cash']
Customer is under 18
The only available paymentoption for this customer is ['cash']

1.2 If-Else-Statement

The instructions within the if block are processed, only if the condition is True. In the case that some instructions shall be performed if a condition is true and other instructions shall be performed if and only if the condition is False.

Syntax:
if expr:
statementelse: statement
Output:
Customer is under 18
The only available paymentoption for this customer is ['cash']
The only available paymentoption for this customer is ['cash', 'checks', 'debit cards', 'credit cards', 'mobile payments', 'electronic bank transfers']

1.3 If-elif-else Statement

In the case that more than two options shall be distinguished, Python provides else if (elseif) as a further option in an if-statement. The condition after the elif-keyword is tested if the previous condition-test/tests have been False. If the condition after an elif-block is True the following instructions within this elif-block are processed, otherwise this block is ignored. It is possible to integrate more than one elif-blocks within a conditional statement. The instructions within the optional else-block after the elif-blocks are processed only if all previous conditions have been False.

Syntax:
if expr:
statement
if else expr:
statement
if else expr:
statement
...
else:
statement
Output:
Weak customer sales
The only available paymentoption for this customer is ['cash', 'checks', 'debit cards', 'credit cards', 'mobile payments', 'electronic bank transfers']

1.4 Conditional Expressions

A simple form of conditional processing set assigns a different value to a variable, depending on the truth value of a condition:

Syntax:
if expr:
x=y
else:
x=z
Output:
{'firstname': 'Max', 'lastname': 'Power', 'gender': 'male',
'age': 17, 'sales': 1}
Customer is under 18
The only available paymentoption for this customer is ['cash']

2 Loops

Loops are applied if a block of instructions must be calculated repeatedly. The repeated processing of the block can either be terminated if a certain condition becomes False or if a defined number of iterations has been reached.

2.1 For Loop

For loops process a sequence of instructions multiple times. The number of iterations is given by the number of elements in the sequence over which the loop variable iterates.

Syntax:
for variable in Sequence:
statement

2.1.1 Basic For Loop

The following example implements a basic for loop. The loop iterates over the integers in the integer list range(5). In each iteration the integer value is printed.

Output:
Value: 0
Value: 1
Value: 2
Value: 3
Value: 4

2.1.2 For Loop break continue and else

The keywords break, continue and else can be applied for for loops in the same manner as for while loops. Break is used to exit a loop , where continue is used to skip the current block, and return to the statement.

syntax:
for variable in Sequence:
Block of instructions 1
if condition 2:
Block of instructions 2
continue
Block of instructions 3
if condition 3:
Block of instructions 4
break
Block of instructions 5
else:
Block of instructions 6
Output:
Value: 1
Value: 2
Value: 4
Output:
Value: 0
Value: 1
Value: 2
Value: 3
Value: 4

2.1.3 Iterating through a list

Output:
<class 'list'>
a
b
c

2.1.3.1 Iterating through dictionary keys

Output:
<class 'dict'>
a
b
c
a
b
c

2.1.3.2 Iterating through dictionary keys

Output:
1
2
3
1
2
3

2.2 List comprehension

Python supports the functional programming paradigm. Functional programming languages provide methods that allow a very efficient implementation of tasks, which are typically solved by loops.

List comprehensions create in only a single command a list of elements , where each element in this list is calculated by applying a unique function to each element of another list . The general form of a list comprehension is

Syntax:
outputlist = [ somefunction(i) for i in inputlist]
Output:
Value with List comprehension: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Or you can use a for loop to create a list of elements

Output:
Value with for loop: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

2.3 While Loop

In a while loop a condition is checked at the beginning of each iteration. If the condition is True the block of instructions inside the while loop is executed. As soon as the condition evaluates to False the loop terminates and the program continues with the instructions after the while loop.

Syntax:
while expr:
statement
Output:
Random number is: 0
Random number is: 8
Line after While Loop

The sourcecode is available at my GitHub repository.

--

--