http://bit.ly/ds2oo1 practicum 4
Read final project spec for class next week
-7 lends Cia ' cat ' print(x) 42 ERROR ! 1 Functions + Scope - - PowerPoint PPT Presentation
http://bit.ly/ds2oo1 practicum 4 Read final project spec for class next week DS 2001:Programming Practicum Fall 2020 Felix Muzny Practicum 4: Lists! Trace the value of x as the given code executes. What is printed by this code x = 4 x DX
http://bit.ly/ds2oo1 practicum 4
Read final project spec for class next week
DS 2001:Programming Practicum Fall 2020 — Felix Muzny
1
Trace the value of x as the given code
when it runs?
x = 4 ls = ['cat', 'bat', 'horse'] for i in range(len(ls)): x = x + (ls[i] % 2) print(x)
xDX
$
lendsCia
'cat
'
42 ERROR!
to that function)
function
what variables have what values at different points.
2
def square1(num): squared = num ** 2 print(squared) print(num)
# ERROR
what order. This gets complex when we have multiple functions!
3
1 def add(num1, num2): 2 a = num1 + num2 3 return a 4 def sub(num1, num2): 5 a = num1 - num2 6 return a 7 def main(): 8 plussed = add(10, 5) 9 minussed = sub(10, 5) 10 print("done!") 11 main()
Order ofexecution
4
7
6
10
2
3
→
what order. This gets complex when we have multiple functions!
4
1 def add(num1, num2): 2 a = num1 + num2 3 return a 4 def sub(num1, num2): 5 a = num1 - num2 6 return a 7 def main(): 8 plussed = add(sub(10, 5), sub(11, 3)) 9 extra_plus = add(plussed, plussed) 10 print("done!") 11 main()
7-
8
4
56
7
45
6
1
23
data = [[ ], [ ], [ ], [ ]]
and indexes of the sublists corresponding to columns
5
1 "Anuj" 15 "Mon and Sat" 2 "Kaushik" 14 "Tue and Wed" 3 "Niyati" 14 "Tue and Fri" 4 "Satya" 15 "Tue and Fri"
OT ) # 1st row
Anuj
6
ls = ['cat', 'bat', 'dog'] # iterate by element for element in ls: print(element) # iterate by index for i in range(len(ls)): ls[i] = ls[i] + 's'
"s
'}
"cats
"
, " bats
"
.
.
7
def number_uppercase(ls): for element in ls:
count = 0
count
= count
+I
return count
8
data = [[1, 3, 5], [2, 4, 6]] for row in data: print("row:", row) for item in row: print(item)
P P t
loop # Z
indices
9
data = [[1, 3, 5], [2, 4, 6]] for row_index in range(len(data)): print("row:", row_index) for col_index in _______________: print(____________)
PPP
Ed, [1,03]
we are used to when we pass them as parameters and update them.
10
def main():
print(odds) print(other_odds) main() def main(): x = 1
x = x + 2 print(x) print(other_x) main()
we are used to when we pass them as parameters and update them.
11
def list_add_mutate(ls, num): ls[0] = ls[0] + num def main():
list_add_mutate(odds, 2) print(odds) main()
we are used to when we pass them as parameters and update them.
12
def list_add_pure(ls, num): new_ls = [:] # get a copy of ls new_ls[0] = new_ls[0] + num return new_ls def main():
updated_odds = list_add_pure(odds, 2) print(odds) print(updated_odds) main()
a-
# Ct, 3,5]
# E3, 3,5]
13
list_part = list_variable[begin_index:end_index]
ls = ['cat', 'hat', 'horse'] print(ls[1:2]) print(ls[1:3]) print(ls[1:]) print(ls[:2]) print(ls[:])
calls in your main function.
definitions can be read in by python first.
remote, we'll put you in groups and ask you to screenshare with each other, as usual.
14