1 / 104

CS 17700 Review, iClicker -Questions Week 15

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

franz
Télécharger la présentation

CS 17700 Review, iClicker -Questions Week 15

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 17700 Review, iClicker-QuestionsWeek 15

  2. Announcements

  3. ANY QUESTIONS?

  4. Variables and Functions

  5. Clicker Question:Are these programs equivalent? 1 2 print(Hello) print(“Hello”) A: yes B: no C: maybe

  6. Clicker Question • Which variable name is not valid? • a • seven • 4a • _4

  7. 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

  8. 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)

  9. 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

  10. 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

  11. Clicker Question:Are these programs equivalent? 1 2 a = 3 defmyFun(a): print (a) myFun(4) a = 3 print (a) A: yes B: no

  12. Clicker Question: does this program print 3 or 4? x = 3 defmyFun(): print (x) x = 4 myFun() A: 3 B: 4

  13. 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

  14. You must be careful! x = 3 defmyFun(): print (x) x = 1 x = 4 myFun() x =5 myFun() ERROR! ERROR!

  15. 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

  16. This works! x = 3 defmyFun(): global x print (x) x =1 x = 4 myFun() myFun()

  17. 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

  18. Clicker Question: Is x global or local? x = 3 defmyFun(): y = 4 z = x + y myFun() A: global B: local

  19. 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!

  20. Conditionals and Loops

  21. 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

  22. 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

  23. x = 7 x = 7 if x > 10: print x x = x + 1 True if x > 10 False print x x = x + 1 End

  24. x = 7 x = 7 if x > 10: print x else: x = x + 1 True if x > 10 False print x x = x + 1 End

  25. Truth Tables

  26. 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

  27. 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

  28. 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

  29. 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

  30. Tests Continued • We could write such a condition as follows: ifx > 0 and y > 0: print(x + y)

  31. 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

  32. 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)

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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>

  38. 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)

  39. 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

  40. 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)

  41. 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)

  42. String Operations

  43. String Operations

  44. CQ: Are these programs equivalent? 1 2 1.capitalize() “1”.capitalize() A: yes B: no

  45. 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

  46. 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

  47. 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] ….

  48. 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

  49. 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

  50. ListOperations

More Related