Lecture 7: More on Functions + Excel
Craig Zilles (Computer Science) March 9, 2020 https://go.illinois.edu/cs105sp20
CS 105 Lecture 7: More on Functions + Excel Craig Zilles (Computer - - PowerPoint PPT Presentation
CS 105 Lecture 7: More on Functions + Excel Craig Zilles (Computer Science) https://go.illinois.edu/cs105sp20 March 9, 2020 To Today 1. nested loops (indefinite loops) 2. break and continue 3. Dynamic Typing 4. Scope 5. Excel Cell
Craig Zilles (Computer Science) March 9, 2020 https://go.illinois.edu/cs105sp20
1. nested loops (indefinite loops) 2. break and continue
2
understand what is happening in them. The challenge problems based around them were pretty hard for me.
continue and break commands
nested loops because sometimes I do not understand what gets read in what order.
3
list1 = [‘lemon’, ‘orange’, ‘lime’] list2 = [‘banana’, ‘lemon’] for thing1 in list1: for thing2 in list2: if thing1 == thing2: print(thing1)
4
How many chars are printed? for c in "sleepy": if c == "e": break print(c) A) 0 B) 1 C) 2 D) 3 E) 6
5
def func(a_list): for item in a_list: if item == "": break print(item) print("done")
6
def func(a_list): for item in a_list: if item == "": return print(item) print("done")
mixed_list = [ 'hi', 3, math.pi, 'there', ['CS', 105]] for item in mixed_list: if type(item) != str: continue print(item) How many items are printed? A) 0 B) 1 C) 2 D) 3 E) 5
7
understand their function and why we would use them
how to use your function
def my_function(): "Docstrings are a string literal that are first thing in the function" return 32
8
different things at different times (polymorphism)
9 String 6 001000011 001010011
Type Number of characters Characters (Stored using Unicode encoding)
000100000 000110001 000110000 000110101 ‘CS 105’ C S 1 5
for print_all(collection): for item in collection: print(item) print_all([1, 2, 3, 4]) print_all({ 'key': 'val', 'CS': 105' }) print_all(7) print_all('a string') A) Error B) No error
10
defining a function?
11
type "global 'variable name' "? I don't understand global vs local variables and how they are used.
12
my_var = 11 def my_print(my_var): print(my_var) my_print(22) print(my_var)
13
What does it print? A) B) C) D) E) Error
11 11 11 22 22 11 22 22
function's scope
scope, it tries the scope the function was defined in.
14
defining a function?
15
16
17
A) 9 B) 10 C) 18 D) 20 E) Some other value
18
19
total = 0 for cell in range: total += sum
20
count = 0 for cell in range: if cell == criteria: # sort of count += 1
21
range's criteria
22
23
A) D12 B) E11 C) E12 D) E13 E) F12
24
must be sorted in increasing order)
equal, values must be sorted in descending order)
containing the value
25
26
retrieve a value.
(default). FALSE = exact match.
27
28