Lecture 6: Loops
Craig Zilles (Computer Science) March 2, 2020 https://go.illinois.edu/cs105sp20
CS 105 Lecture 6: Loops Craig Zilles (Computer Science) - - PowerPoint PPT Presentation
CS 105 Lecture 6: Loops Craig Zilles (Computer Science) https://go.illinois.edu/cs105sp20 March 2, 2020 To Today 1. Warmup: Functions 2. Conditionals Short circuiting, nesting, and ternary operator if vs. if/else vs. if/elif/else
Craig Zilles (Computer Science) March 2, 2020 https://go.illinois.edu/cs105sp20
4. for Loops (definite loops) 5. range 6. while Loops (indefinite loops)
2
def add_one(x): return x + 1 x = 2 x = x + add_one(x) A) No bugs. The code is fine. B) The function body is not indented. C) We use x as both a parameter and a variable, but we are not allowed to do that D) Both B and C
3
True or anything() is True False and anything() is False
(len(my_str) > 10) and (my_str[10] == 'a')
4
5
hello there hello there
print('hello') and print('there')
but other times we make one big if, elif and else statement?
chapter can be used in real world cases. A thing I like to do to help me better grasp the concepts, is imagine them happening in this world. So giving me more mundane scenarios of where we would be using these things would help a lot.
6
Asked my TA to send email to all students in the class that didn't take Exam 0.
if student in exam0_takers: send_email(student)
7
Company sends recruiting invitations to students with Python in their resume, sends 'nack' email to others if 'python' in resume.lower(): send_invitation(student) else: send_polite_decline(student)
8
Final exam location based on first letter of netid: [a-j] Loomis 100 [k-o] DCL 1320 [p-z] English 214
first_char = netid.lower()[0] if first_char <= 'j': location = 'Loomis 100' elif first_char <= o: location = 'DCL 1320' else: location = 'English 214'
9
If you were choosing between 6 possibilities, what is the fewest elif statements you coud have: A) 1 B) 2 C) 3 D) 4 E) 5
10
When to use elif and when to use else? I think there should be
if sales_type == 2: if sales_bonus < 5: sales_bonus = 10 else: sales_bonus = sales_bonus + 2 else: sales_bonus = sales_bonus + 1 Can I change the first 'else' into elif?
11
12
Code Block A Code Block B (Execution determined by condition1) if condition1: Code Block E (Same indentation as A) if condition2: Code Block C (Execution determined by condition2)
A) A, E B) A, B, E C) A, B, D, E D) A, B, C, D, E
13
Code Block A Code Block B (Execution determined by condition1) if condition1: Code Block E (Same indentation as A) if condition2: Code Block C (Execution determined by condition2) Code Block D (Same indentation as B)
if condition: var = value1 else: var = value2
var = value1 if condition else value2
14
def f(x): y = 9 if x <= 6 else -9 if x > y: return y return x
15
A) -9 B) 7 C) 9 D) -9 then 7 E) 9 then 7
16
and when do use one
python knows how to execute which line of code in which order.
understand the difference between the for and while loops.
variable is and its purpose.
17
loop condition loop body (one or more statements) post-loop stuff
18
loop.
for student in ['Cesar', 'Jessica', 'Sydney']: print('{0} is awesome!'.format(student))
19
course_times = {'CS 105':'F9-11', 'CS 125':'MWF11-12'} for course in course_times: print(course, 'meets', course_times[course])
A) there is an error B) 1 C) 2 D) 3 E) 4
20
things = [22, [33, 44], 55, [66]] for thing in things: print(str(thing))
A) there is an error B) 2 C) 3 D) 4 E) 5
21
first, last, age = ['Craig', 'Zilles', 27]
x = "started as x" y = "started as y" x, y = y, x
22
does?
cities_by_pop = ['New York', 'Los Angeles', 'Chicago'] message = '{0} is number {1} by population' for index, name in enumerate(cities_by_pop): print(message.format(name, index + 1))
23
new_list = [] for index, value in enumerate(orig_list): if (index % 2) == 0: new_list.append(value)
A) [3, 7] B) [3, 22] C) [3, 7, 22, 90] D) [7, 90] E) [22, 90]
24
range(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] range(3, 8) # [3, 4, 5, 6, 7] range(-3, 11, 3) # [-3, 0, 3, 6, 9]
25
for i in range(5): # note that i not used print('CS 105 is lit!')
26
How many lines of output will this print? A) 0 –or– there is an error B) 1 C) 4 D) 5 E) 6
new_str = '' for index in range(0, len(orig_str), 2): new_str += orig_str[index] print(new_str) Teqikbonfxjme . is printed
27
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.
28
list1 = [‘lemon’, ‘orange’, ‘lime’] list2 = [‘banana’, ‘lemon’] for thing1 in list1: for thing2 in list2: if thing1 == thing2: print(thing1)
29
should be used.
30
new_list = [] while True: val = input("Enter a value or 'q' to quit\n") if val == 'q': break new_list.append(value) print(str(new_list))
31
num = 14 while num >= 1: print(num) num = num // 2 How many lines are printed: A) 3 B) 4 C) 5 D) 6 E) 7
32
33