The document discusses Python programming concepts including variable assignment, importing modules, while loops, and breaking out of loops. It covers assigning multiple variables at once, importing common math functions from the math module, using while loops to execute code an unknown number of times until a condition is met, and using break statements to exit a loop early.
The document discusses Python programming concepts including variable assignment, importing modules, while loops, and breaking out of loops. It covers assigning multiple variables at once, importing common math functions from the math module, using while loops to execute code an unknown number of times until a condition is met, and using break statements to exit a loop early.
The document discusses Python programming concepts including variable assignment, importing modules, while loops, and breaking out of loops. It covers assigning multiple variables at once, importing common math functions from the math module, using while loops to execute code an unknown number of times until a condition is met, and using break statements to exit a loop early.
▶ 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 ...