Control
Control
▪ input()
▪ if-elif-else
▪ while-break-continue
input
▪ The builtin function input(message)prints message,
and waits for the user provides a line of input and presses return.
The line of input is returned as a str
▪ If you e.g. expect input to be an int, then remember to convert the
input using int()
name-age.py
name = input('Name: ')
age = int(input('Age: '))
print(name, 'is', age, 'years old')
Python shell
> Name: Donald Duck
> Age: 84
| Donald Duck is 84 years old
Branching – do either this or that ?
Code before
make decision ?
boolean expression
True False
do this do that
Code after
Basic if-else
if boolean expression:
code
identical if-else.py
indentation code
if x % 2 == 0:
code print('even')
else: else:
print('odd')
code
identical
indentation code
code
Identical indentation for a sequence of lines = the same spaces/tabs should precede code
pass
▪ pass is a Python statement doing nothing. Can be used where a
statement is required but you want to skip (e.g. code will be writen later)
if-else.py
if x % 2 == 0:
print('even')
else:
pass
if.py
if-elif-else if x == 0:
print('zero')
if condition:
if-else.py
code if x % 2 == 0:
elif condition: # zero or more “elfi“ ≡ “else if” print('even')
else:
code print('odd')
else: # optional elif.py
code if x < 0:
print('negative')
if (condition) { elif x == 0:
code print('zero')
} else if (condition) { elif x == 1:
code print('one')
} else { else:
code print('>= 2')
Other languages using indentation for blocking:
} Java, C, C++ syntax
ABC (1976), occam (1983), Miranda (1985)
Questions – What value is printed?
x = 1
a) 1
if x == 2: b) 2
x = x + 1 c) 3
else:
x = x + 1 d) 4
x = x + 1 e) 5
x = x + 1
print(x) f) Don’t know
Nested if-statements
nested-if.py
if x < 0:
print('negative')
elif x % 2 == 0:
if x == 0:
print('zero')
elif x == 2:
print('even prime number')
else:
print('even composite number')
else:
if x == 1:
print('one')
else:
print('some odd number')
Common mistake
if-if.py if-elif.py
x = int(input()) x = int(input())
if x == 0: if x == 0:
print('zero') print('zero')
if x % 2 == 0: elif x % 2 == 0:
print('even') print('even')
Python shell Python shell
> 0 > 0
| zero | zero
| even
if-else expressions
▪ A very common computation is
if test:
think of this as the
x = true-expression
“common case” and the
else: “exceptional case”
x = false-expression
▪ In Python there is a shorthand for this:
x = true-expression if test else false-expression
▪ In C, C++, Java, Javascript the equivalent notation is (note the different order)
x = test ? true-expression : false-expression
Repeat until done
Code before
True
repeat ? do it once more
boolean expression
False
Code after
while-statement
while condition: The function randint(a, b) from
module random returns a random
code integer from {a, a + 1,..., b – 1, b}
...
break # jump to code after while loop
... random-pair.py
from random import randint
continue # jump to condition at the while True:
... # beginning of while loop x = randint(1, 10)
y = randint(1, 10)
count.py if abs(x - y) >= 2:
x = 1 break
while x <= 5: print('too close', x, y)
print(x, end=' ') print(x, y)
x = x + 1 Python shell
print('and', x)
while (condition) { | too close 4 4
code Python shell | too close 10 9
} Java, C, C++ syntax | 1 2 3 4 5 and 6 | 8 5
An exercise asks to
simplify the code
Computing 𝒙 using binary search
int-sqrt.py
x = 20
low = 0
high = x + 1
while True: # low <= sqrt(x) < high Integer division
if low + 1 == high: high+low
break 2
mid = (high + low) // 2
if mid * mid <= x:
mid ≤ 𝑥
low = mid
⇕
continue
mid2 ≤ 𝑥
high = mid
print(low) # low = floor(sqrt(x))
low mid 𝒙 𝒙
0 high 𝑥 𝑥+1
Division using the Newton-Raphson method
division.py
▪ Goal: Compute 1 / n only using +, -, and * n = 0.75 # n in [0.5, 1.0]
▪ x = 1 / n ⟺ f(x) = n – 1 / x = 0 x = 1.0
last = 0.0
while last < x:
▪ Problem reduces to finding root of f print(x)
last = x
▪ Newton-Raphson: x = (2 - n * x) * x
print('Apx of 1.0 /', n, '=', x)
x := x-f(x)/f’(x) = x-(n-1/x)/(1/x2) = (2-n∙x)∙x print('Python 1.0 /', n, '=', 1.0 / n)
since f’(x) = 1 / x2 for f(x) = n – 1 / x Python shell
| 1.0
| 1.25
f(x) | 1.328125
x – f(x) / f’(x) | 1.33331298828125
root | 1.3333333330228925
next approximation
| 1.3333333333333333
| Apx of 1.0 / 0.75 = 1.3333333333333333
| Python 1.0 / 0.75 = 1.3333333333333333
(x, f(x)) current approximation
en.wikipedia.org/wiki/Newton’s_method