1.04k likes | 1.17k Views
CS 17700 Review, iClicker -Questions Week 15. Announcements. ANY QUESTIONS?. Variables and Functions. Clicker Question: Are these programs equivalent?. 1. 2. p rint(Hello). p rint(“Hello”). A: yes. B: no. C: maybe. Clicker Question. Which variable name is not valid? a seven 4a
E N D
Clicker Question:Are these programs equivalent? 1 2 print(Hello) print(“Hello”) A: yes B: no C: maybe
Clicker Question • Which variable name is not valid? • a • seven • 4a • _4
Clicker Question:Are these programs equivalent? 1 2 defmyFun(a): print(a) return a print(myFun(4)) defmyFun(a): print(a) print (myFun(4)) A: yes B: no
Function Call • Once the function is defined it can be called as many times as one likes • If the function has a return it can be stored into a variable at the call myFunction(6,7) myFunction(4,10) a = myFunction(6,7)
Clicker Question:Are these programs equivalent? 1 2 a = 3 defmyFun(a): print(a) print(a) a = 3 defmyFun(a): print(a) print(a) A: yes B: no
Clicker Question:Are these programs equivalent? 1 2 a = 3 defmyFun(b): print(b) print(a) myFun(3) a = 3 defmyFun(b): print(b) print(b) myFun(3) A: yes B: no
Clicker Question:Are these programs equivalent? 1 2 a = 3 defmyFun(a): print (a) myFun(4) a = 3 print (a) A: yes B: no
Clicker Question: does this program print 3 or 4? x = 3 defmyFun(): print (x) x = 4 myFun() A: 3 B: 4
Variables and Functions • Variables used in functions but defined outside of the function can be changed • Multiple calls to a function may yield different results if the program “rebinds” such variables
You must be careful! x = 3 defmyFun(): print (x) x = 1 x = 4 myFun() x =5 myFun() ERROR! ERROR!
Global Variables • How can we get the example code we saw earlier to work? • Python is not sure if we want x to be a local variable or if it should refer to the x we defined outside of the function • We can inform python if we want x to refer to the variable outside of the function • New keyword global
This works! x = 3 defmyFun(): global x print (x) x =1 x = 4 myFun() myFun()
Global or Local? • If the global keyword is used, the variable is global • If the first use of the variable is a ‘read’ (reference), the variable is global • NOTE: We cannot assign to such a variable later • Function arguments are always local • If the first use of the variable is a write (assignment), the variable is local • Unless the variable is defined global
Clicker Question: Is x global or local? x = 3 defmyFun(): y = 4 z = x + y myFun() A: global B: local
Let’s Review • Functions take input and produce output • Output is provided by the “return” statement • Otherwise the function does not provide output • At the call site of the function the arguments get bound • The arguments can rebind variables that have already been defined for the duration of the call • You can use global variables, defined outside the function, but you must be careful!
Conditionals • It is also possible to specify what to do if the condition is False. Contrast these two examples. x = 7 if x > 10: print x x = x + 1 x = 7 if x > 10: print x else: x = x + 1
CQ: Are these programs equivalent? 1 2 x = 7 if x > 10: print x x = x + 1 print x x = 7 if x > 10: print x else: x = x + 1 print x A: yes B: no
x = 7 x = 7 if x > 10: print x x = x + 1 True if x > 10 False print x x = x + 1 End
x = 7 x = 7 if x > 10: print x else: x = x + 1 True if x > 10 False print x x = x + 1 End
CQ:Are these programs equivalent? defprintCountNTimes(n): count = 0 while(count < n): print('The count is: ', count ) count = count + 1 2 1 printCountNTimes(4) printCountNTimes(4) printCountNTimes(8) A: yes B: no
While Loop Dangers • While loops do not require that the loop ever stop. • In this example, count starts at 0 and gets smaller. Thus it will always be < 9 count = 0 while (count < 9): print('The count is: ', count ) count = count - 1
CQ: Do these programs print the same things? x = 12 if x > 10: print (x) x = x + 1 print (x) 1 x = 12 if x > 10: print(x) else: x = x + 1 print(x) 2 A: yes B: no
Clicker Question • Now we can start building useful conditions • Does this check if x > 0? if x and y > 0: print(x , y ) A: yes B: no
Tests Continued • We could write such a condition as follows: ifx > 0 and y > 0: print(x + y)
Clicker Question:Are these programs equivalent? 1 2 if (x+y) < 10: print(x) if (x+y)>=10: print(y) if(x+y) < 10: print(x) else: print(y) A: yes B: no
Example Revisited if x < 10: print(x+y) if y < 100: print(x) else: print(y) if x < 10 and y < 100: print(x+y) print(x) if x < 10 and y >= 100: print(x+y) print(y)
Analysis def minOfThree(a,b,c):if a<=b: if a<=c:return aelse:return celse:if b<=c:return belse:return c a<=b Who can be min? not a! not b! y n a<=c b<=c y n n y c a b c
Clicker Question 1 if “False”: print(“hi”) 2 if False: print(“hi”) A: 1 and 2 both print B: only 1 prints C: only 2 prints D: neither 1 nor 2 print
Clicker Question 3 if eval(“False”): print(“hi”) 2 if False: print(“hi”) A: 3 and 2 both print B: only 3 prints C: only 2 prints D: neither 3 nor 2 print
CQ:Are these programs equivalent? 1 2 for a in range(0, 10, 1): print(a) for a in range(10): print(a) A: yes B: no
Definite Loops • for loops alter the flow of program execution, so they are referred to as control structures. more items in <sequence> no yes <var> = next item <body>
CQ:Are these programs equivalent? A: Yes B: No 1 2 x = 0 y = 0 for k in range(5): x = x + k y = x + k print(y) x = 0 y = 0 for k in range(5): x = x + k y = x + k print(y)
CQ:Are these programs equivalent? 1 2 a = 0 while(a < 10): print(a) a = a+1 for a in range(10): print(a) A: yes B: no
CQ: Do these functions have the same output? defnested1(a,b): forx in range(0,a): for y in range (0, b): print(x*y) A: yes B: no defnested2(a,b): foryin range(0,b): for x in range (0, a): print(x*y)
When might they be equivalent? • What about when a=b? • That is, we call the functions and provide the same values for a and b • Output of nested1(2,2) = output of nested2(2,2)
CQ: Are these programs equivalent? 1 2 1.capitalize() “1”.capitalize() A: yes B: no
Clicker Question: Are these two functions equivalent? defprintByCharacter(str) i = 0 whilei < len(str): print (str[i]) i = i + 1 defprintByCharacter(str) i = 0 whilei < 16: print (str[i]) i = i + 1 A: yes B: no
CQ: Are these programs equivalent? i = 0 x = “This is a string” whilei < len(x): print (x[i]) i = i + 1 x = “This is a string” for y in x: print (y) A: yes B: no
What is going on here? x = “This is a string” for y in x: print (y) x T h i s Under the hood we are doing something similar to: i y = x[j] ….
CQ: Are these programs equivalent? i = 0 x = “This is a string” whilei < len(x): print (x[i]) i = i + 1 x = “This is a string” i = 0 – len(x) whilei < 0: print (x[i]) i = i + 1 A: yes B: no
CQ:Are these programs equivalent? 1 2 b = [‘h’,’e’,’l’,’l’,’o’] b.insert(len(b), “w”) print(b) b = [‘h’,’e’,’l’,’l’,’o’] b.append(“w”) print(b) A: yes B: no