Information
Section: Control flow statement
Goal: Understand the basic loops in Python.
Time needed: 10 min
Prerequisites: Curiosity
Control flow statement¶
Credits: Institute of Maritime Logistics – Hamburg University of Technology
The key thing to note about Python’s control flow statements and program structure is that it uses indentation to mark blocks. Hence the amount of white space (space or tab characters) at the start of a line is very important. This generally helps to make code more readable but can catch out new users of python off guard. Do not include white space unless needed for the command.
1 Conditionals ¶
Below are a list of conditional commands with their syntax. These conditional statements are If, If-else, Else If, and various Loop statements.
1.1 If ¶
If a given condition is true then perform the action below.
if some_condition:
code block```
x = 12
if x > 10:
print("Hello")
Hello
1.2 If-else ¶
If a condition is true then perform action below, if the condition is false then perform the else action.
if some_condition:
code block
else:
code block```
x = 12
if 10 < x < 11:
print("hello")
else:
print("world")
world
1.3 Else if ¶
Similar to If-else but using elseif allows for addional if statements if the others above them prove false.
if some_condition:
code block
elif some_condition:
code block
else:
code block```
x = 10
y = 12
if x > y:
print("x>y")
elif x < y:
print("x<y")
else:
print("x=y")
x<y
if statement inside a if statement or if-elif or if-else are called as nested if statements.
x = 10
y = 12
if x > y:
print( "x>y")
elif x < y:
print( "x<y")
if x==10:
print ("x=10")
else:
print ("invalid")
else:
print ("x=y")
x<y
x=10
2 Loops ¶
2.1 For ¶
for variable in something:
code block```
For every variable in something the code block is run. This is called looping.
When looping over integers the **range()** function is useful which generates a range of integers:
* range(n) = 0, 1, ..., n-1
* range(m,n)= m, m+1, ..., n-1
* range(m,n,s)= m, m+s, m+2s, ..., m + ((n-m-1)//s) * s (the last number is the highest number in the form m+xs that is smaller than n)
for ch in 'abc':
print(ch)
a
b
c
for i in range(5):
print(i)
0
1
2
3
4
In the above example, i iterates over the 0,1,2,3,4. Every time it takes each value and executes the code block inside the loop. It is also possible to iterate over a nested list illustrated below.
total = 0
for i,j in [(1,2),(3,2)]:
print(i, '**', j, '=', i**j)
total += i**j
print("total =",total)
1 ** 2 = 1
3 ** 2 = 9
total = 10
list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
total=0
for list1 in list_of_lists:
for x in list1:
total = total+x
print(total)
45
list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for list1 in list_of_lists:
j = 0
for i in list1:
j += i
print(j, '= sum of elements of', list1)
6 = sum of elements of [1, 2, 3]
15 = sum of elements of [4, 5, 6]
24 = sum of elements of [7, 8, 9]
2.2 While ¶
while loops a function until a statement is met
while some_condition:
code block```
i = 1
while i < 4: # this statement must become false to stop the loop
print(i ** 2) # output
i = i+1 # this will help our statement move towards its goal
print('Bye')
1
4
9
Bye
2.3 Break ¶
As the name says, it is used to break out of a loop. Usually it is used inside an ‘if’ condition that is in another loop. When the ‘if’ condition becomes ‘true’, the break is executed. This causes the loop condition to stop:
for i in range(100):
print(i)
if i >= 7:
break
0
1
2
3
4
5
6
7
2.4 Continue ¶
With continue, the current run of the loop is ended and the next run of the loop begins:
for i in range(3):
print(i)
continue
print('I will never print this')
0
1
2
for i in range(10):
if i>4:
print("Ignored",i)
continue
print("Processed",i) # this statement is not reached if i > 4
Processed 0
Processed 1
Processed 2
Processed 3
Processed 4
Ignored 5
Ignored 6
Ignored 7
Ignored 8
Ignored 9