lecture_3_DataStructures_and_Iteration
for loop pattern 1: Sequence scans Ex: Find sum and prod of a list of numbers num_list = [1,2,3,4] print(sum(num_list)) output:
10
Lists: index of a specific element x = [1, 2, 3, 4, 5] print(x.index(3)) What is the output?
2
Lists are mutable (unlike strings) for example, what is the output of the code below? vowels = ['a', 'e', 'i', 'o', 'u'] print(id(vowels), vowels) vowels[0] = 'A' vowels[1:3] = ['E', 'I'] # slice reassignment print(id(vowels), vowels)
4355691528 ['A', 'E', 'I', 'o', 'u']
x = [1,2,3,4,5] # direct assignment y = [1, 'a', [1,2,3]] z = [] # creates an empty list print(type(x), x) print(type(y), y) print(type(z), z) What is the output?
<class 'list'> [1, 2, 3, 4, 5] <class 'list'> [1, 'a', [1, 2, 3]] <class 'list'> []
Iteration: Repetitively apply some logic 3 Common patterns:
Do something to/for each item in a sequence (ex. random patient assignment) Repeat something n times (ex. snooze) Repeat something as long as some condition is True (or False) (will be covered later) (ex. statistical model refinement
General process of loop construction (3 MAJOR STEPS)
Initialize some variable(s) before the loop starts. Apply some computation(s) for each item in the loop body, possibly changing the variables. Use the results after the loop terminates.
Lists: Membership in operator, similar to string type output of below? x = [1,2,3,4,5] print(1 in x) # boolean expression: evaluates to True/False print(10 not in x)
True True
Lists: Add / remove elements Output? x = [1,2,3,4,5] x.append(10) print(x)
[1, 2, 3, 4, 5, 10]
Lists: Add / remove elements Output? x = [1,2,3,4,5] x.extend([11,12,13,14,15]) # or x + [11,12,13,14,15] print(x)
[1, 2, 3, 4, 5, 11, 12, 13, 14, 15]
Lists: Add / remove elements Output? x = [1,2,3,4,5] print(x) x.pop(2) print(x)
[1, 2, 3, 4, 5] [1, 2, 4, 5]
x = [3,5,2,7,1,6,4] print(x) x.sort() print(x) x.sort(reverse=True) print(x) Output? Explain x. sort
[3, 5, 2, 7, 1, 6, 4] [1, 2, 3, 4, 5, 6, 7] [7, 6, 5, 4, 3, 2, 1] x.sort() will order numbers from smallest to greatest
for loop pattern 1: Sequence scans Ex. Simple list traversal vowels = ['a', 'e', 'i', 'o', 'u'] for vowel in vowels: print(vowel) output:
a e i o u
vowels = ['a', 'e', 'i', 'o', 'u'] print(vowels[0]) print(vowels[1]) print(vowels[-1]) print(vowels[1:3]) print(vowels[::2]) print(vowels[::-1]) what is output?
a e u ['e', 'i'] ['a', 'i', 'u'] ['u', 'o', 'i', 'e', 'a']
Basic structure for iteration: + example
for item in iterable: <do_action(s)> For ex.: for gene in list_of_genes: translate(gene) # Use indentation to delineate from rest of the codw
for loop pattern 2: range (+ len) example of modifying an existing list: num_list1 = [5, 10, 15, 20] for idx in range(len(num_list1)): num_list1[idx] = num_list1[idx]*2 print('num_list: ', num_list1) output:
num_list: [10, 20, 30, 40]
Use built-in functionality as much as possible Less code More efficient # DIY num_list = [1,2,3,4] sum_ = 0 for num in num_list: sum_ = sum_ + num print("avg: ", sum_/len(num_list))
output: avg: 2.5
for loop pattern 2: range (+ len) function What does the Built-in range() function do? what is the output of the code b elow? x = range(10) type(x)
returns a range object output: range
General process of loop construction example: num_list = [1,2,3,4] # Input sum_ = 0 # Initialize prod = 1 for num in num_list: # Apply sum_ = sum_ + num prod = prod*num print('sum: ','\t', sum_) print('prod: ', '\t', prod) What is the output?
sum: 10 prod: 24
T or F: for compound statement is used to apply some logic to each item in any iterable (string, list, dictionaries etc.)
true