7  Control Flow

import sys
sys.version
'3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:41:22) [Clang 13.0.1 ]'

7.1 if - elif - else

a = 4

Conditionals do not require parentheses:

if a < 10:
    print("hello")
hello

But they work with them just the same:

if (a < 10):
    print("hey")
hey
x1 = 9
if x1 > 10:
    print("x1 is greater than 10")
elif 5 < x1 <= 10:        
    print("x1 is between 5 and 10")
else:
    print("x1 is not very big")  
x1 is between 5 and 10

7.2 for loops

for i in range(4):
    print("Working on Feature " + str(i))
Working on Feature 0
Working on Feature 1
Working on Feature 2
Working on Feature 3

7.3 enumerate()

enumerate(iterable, start=0) outputs pairs of an index (starting at start) and an item from iterable

a = ['one', 'two', 'three', 'four']
for it in enumerate(a):
    print(it)
(0, 'one')
(1, 'two')
(2, 'three')
(3, 'four')
for i, el in enumerate(a, start=1):
    print("Item number ", i, ": ", el, sep = '')
Item number 1: one
Item number 2: two
Item number 3: three
Item number 4: four

7.4 while loops

x = 18
while x > 12:
    x -= 1
    print(x)
17
16
15
14
13
12

7.5 Resources