Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Python 3 :: Lesson 7

▶ Assign multiple variables: x, y = y, x


▶ Assign multiple variables: x, y, z = [1, 2, ’test’]
▶ from math import pi
▶ from math import sqrt
▶ from math import sin
▶ from math import cos
▶ from math import inf
▶ from math import *
▶ All numbers above a limit ∼ 1.798e + 308 inf
▶ All numbers below a limit ∼ 5e − 324 inf
Python 3 :: Lesson 8
▶ while loops: Useful when we do not know how many times we need to run it.
▶ Syntax:
while Condition is True:
Execute command 1
Execute command 2
else:
Execute command 3
▶ If Condition is False, else block runs.
▶ Multiple conditions can be combined.
Example: a < b and k < max iter
▶ Important: For while loops always add a maximum iteration check.
▶ Note we also have a for-else loop
for iteration:
Execute command 1
Execute command 2
else:
Execute command 3
▶ If iteration is exhausted the else block is run.
Python 3 :: Lesson 9

▶ Exits from a loop: break


▶ If there are nested loops, break returns the control to the higher level.
▶ Example for for loops
for i in range(10):
if i==5:
break
else:
print(i)
...
▶ Example for while loops
k = 1; l = 1
while True:
k, l = l, k+l
print(l)
if l>100:
break
...

You might also like