Execution Model¶
Just-in-time precompiled bytecode Interpreter
Python executes a module, that is program code, written into a text file, one statement (~=line) after the other. Usually an application consists of many modules. External modules are imported into the current module.
The statements are executed sequentially until it finds a conditional statement, a loop or a function call.
Statements which change the control flow include the usual supects like if..else, while, for..in, try..except. All these statements have in common that they introduce blocks. A block is a sequence of statements, which is indented. In many other languages, blocks are defined by curly braces or by begin..end statements. Python uses a colon ‘:’ and indentation to identify blocks.
Conditions and blocks¶
An if statement is coded like this:
cond = True
if cond: # the colon starts a block
print("yes") # the block is indented
print("done") # startement after the block
yes
done
cond = False
if cond: # the colon starts a block
print("yes") # the block is indented
else:
a = 3
print(a * 'no ', end='')
print("\ndone") # startement after the block
no no no
done
There is no ‘case’ statement, Python uses elif’s. The conditions are of course far more flexibe than a plain ‘case’.
cond = 7
if cond == 1: # the colon starts a block
print("one") # the block is indented
elif cond == 10: # the colon starts a block
print("ten") # the block is indented
elif cond == 7: # the colon starts a block
print("seven") # the block is indented
else:
print('other')
print("done") # startement after the block
seven
done
Who comes from the C language might miss a cond?x:y construct. Its here, and there are cases, when its really useful
num = 4
# this:
if num > 5:
num += 3
else:
num += 6
print(num)
# same as:
num += 3 if num > 5 else 6
print(num)
10
13
Loops - while¶
‘while’ loops observe a condition to be True. The block may contain ‘continue’ or ‘break’ statements.
items = [5,4,6,2,8,3,4,5,10,7,6] # food for a while block
while items:
item = items.pop(0)
if item >= 10:
print("found break contdition")
break
if item%2: # modulo division
continue
print("even number:", item)
print("done")
even number: 4
even number: 6
even number: 2
even number: 8
even number: 4
found break contdition
done
There is an ‘else’ block for loops that did not ‘break’
items = [5,4,6,2,8,3,4,5,1,7,6] # food for a while block
while items:
item = items.pop(0)
if item >= 10:
print("found break condition")
break
if item%2: # modulo division
continue
print("even number:", item)
else:
print("reached end of the data")
print("done")
even number: 4
even number: 6
even number: 2
even number: 8
even number: 4
even number: 6
reached end of the data
done
‘for’ is for loops¶
The for loop works on all kind of ‘iterable’ data sources. An ‘iterable’ is an object, which provides a sequence of objects one by one. gives me back individual This goes far beyond static lists or tuples and there are many different kinds of ‘iterables’. Here an (incomplete) collection of examples.
print("--string--")
for ch in 'abc':
print("char:", ch)
--string--
char: a
char: b
char: c
print("--range()--")
for num in range(3):
print("number:", num)
--range()--
number: 0
number: 1
number: 2
print("--more range()--")
for num in range(30,10,-6):
print("number:", num)
--more range()--
number: 30
number: 24
number: 18
number: 12
print("--enumeration--")
names = ['Jack', 'Mary', 'Fred', 'Rosie', 'Mike', 'Janet', 'Sunny']
for ndx, name in enumerate(names):
print(ndx, ':', name)
--enumeration--
0 : Jack
1 : Mary
2 : Fred
3 : Rosie
4 : Mike
5 : Janet
6 : Sunny
print('--file--')
with open('jupiter.json', 'r') as fi: # an open file is iterable
for line in fi:
print(line, end='')
if '}' in line:
break
--file--
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},