📘 Control Flow¶

  • 📘 Containers
    • 🔹 Enumerate
    • 🔹 Zip
    • 🔹 Range
    • 🔹 itertools
  • 📘 Flow Control
    • 🔹 if-else block
    • 🔹 switch
    • 🔹 for loop
    • 🔹 while loop

📘 Containers¶

In [ ]:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

🔹 Enumerate¶

In [ ]:
my_list = ["apple", "banana", "carrot", "dragonfruit"]
my_list
In [ ]:
enumerate(my_list)
type(enumerate(my_list)) # enumerate
e1_list = list(enumerate(my_list)) # list of tuples returned when enum converted to list
e1_list
e1_list[0]
type(e1_list[0]) # tuple

# change starting position of index from 0 to 11
list(enumerate(my_list, 11))

Loop over an enum

In [ ]:
# prefer to use pos, element instead of a, b
for a, b in enumerate(my_list):
    # a is int, b is str
    print("###")
    print(a)
    print(type(a))
    print(b)
    print(type(b))
In [ ]:
for a in enumerate(my_list):
    # b is optional 
    # if b not mentioned, a is a tuple
    print("###")
    print(a)

🔹 Zip¶

📏 Range¶

In [ ]:
list(range(10))
[x for x in range(10)]
[range(10)] # weird, avoid.

🔹 itertools¶

Back to the top


📘 Flow Control¶

🔹 if-else block¶

In [ ]:
x = 4
x = 5
x = 10
if(x>5):
    print("greater than five")
elif(x<5):
    print("less than five")
elif(x==5):
    print("equals five")    
# else if does not work, use elif
In [ ]:
age = list(range(10,51,10))
['young' if x < 15 else 'old' for x in age]
['young' if (x < 15) else 'middleage' if (x < 35) else 'old' for x in age]
# [a if condition_1 else b if condition_2 else c for _ in _]

np.where()

🔹 switch¶

In [ ]:
# does not readily exist in python

🔹 for loop¶

In [ ]:
my_num = list(range(0,51,10))
for x in my_num:
    print(x)
x
In [ ]:
for x in my_num:
    print(x)
    if x>30:
        break
In [ ]:
for x in my_num:
    if x==30:
        continue
    print(x)    
In [ ]:
for x in range(5):
    print(x)
    del x

🔹 while loop¶

In [ ]:
i = 0
while(i < 5):
    print(i)
    i = i + 1
In [ ]:
i = 0
while 1:
    print(i)
    i = i + 1
    
    if(i==5):
        break

Back to the top