cs 61a discussion 1
play

CS 61A Discussion 1 Control and Environments Albert Xu Attendance: - PowerPoint PPT Presentation

CS 61A Discussion 1 Control and Environments Albert Xu Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/ Announcements Homework 1 is due today @ 11:59pm. Hog is due next Thursday checkpoint on Monday (no


  1. Short Circuiting Let’s check out a few examples… >>> True and 0 and 1 and -3 0 >>> -2 or 1 / 0 -2 >>> False and 1 / 0

  2. Short Circuiting Let’s check out a few examples… >>> True and 0 and 1 and -3 0 >>> -2 or 1 / 0 -2 >>> False and 1 / 0 False and returns true if both are true! But once Python sees a single falsy value, it immediately knows that the whole statement is false! (no need to evaluate the rest)

  3. Final Note Boolean operators, just like arithmetic, follows a set order of operations.

  4. Final Note Boolean operators, just like arithmetic, follows a set order of operations. The order is: Not And Or

  5. Final Note Boolean operators, just like arithmetic, follows a set order of operations. The order is: Not And Or

  6. Final Note Boolean operators, just like arithmetic, follows a set order of operations. The order is: Not And Or This is good to know, but probably not super important. Just make sure to remember short-circuiting rules!

  7. if statement (and the rest of the suite)

  8. if statement (and the rest of the suite)

  9. if statement (and the rest of the suite) if this first if statement is true, then we print out wrong!, and ignore the rest of the block, including both elifs and the else. …otherwise, keep going and check the elif.

  10. if statement (and the rest of the suite) okay, so if the first if wasn’t true, then check the first elif. if true, then print out …otherwise, keep going and check good job!, and ignore the rest of the block. the next elif.

  11. if statement (and the rest of the suite) same with the other elifs…

  12. if statement (and the rest of the suite) and if nothing else before was true, then do what’s inside the else statement. We’ll print out that ain’t it chief.

  13. if statement (and the rest of the suite) An if statement block consists of: the rule! - an if clause - zero or more elif clauses - zero or one else clauses

  14. while statement (or loop) Sometimes you want to keep performing an action over an over again until a certain criterion is true.

  15. while statement (or loop) Sometimes you want to keep performing an action over an over again until a certain criterion is true. For example, if you were interested in printing out all the natural numbers from 1 to 100, how would you do it?

  16. while statement (or loop) Sometimes you want to keep performing an action over an over again until a certain criterion is true. For example, if you were interested in printing out all the natural numbers from 1 to 100, how would you do it?

  17. while statement (or loop) Sometimes you want to keep performing an action over an over again until a certain criterion is true. For example, if you were interested in printing out all the natural numbers from 1 to 100, how would you do it? what does this print out?

  18. while statement (or loop) Sometimes you want to keep performing an action over an over again until a certain criterion is true. I thought about how to fix it. Which of these is correct? A B

  19. while statement (or loop) Sometimes you want to keep performing an action over an over again until a certain criterion is true. I thought about how to fix it. Which of these is correct? A B They both are! Let this be a lesson that you should be careful of your boundary conditions…

  20. FizzBuzz Warmup problem For every number from 1 to n: • For multiples of three, print "Fizz" • For multiples of five, print "Buzz" • For multiples of both three and five print "FizzBuzz" only • Otherwise, print the number *these slides borrowed from past TA Jerry Chen - jerryjrchen.com

  21. FizzBuzz *these slides borrowed from past TA Jerry Chen - jerryjrchen.com

  22. FizzBuzz *these slides borrowed from past TA Jerry Chen - jerryjrchen.com

  23. *these slides borrowed from past TA Jerry Chen - jerryjrchen.com

  24. *these slides borrowed from past TA Jerry Chen - jerryjrchen.com

  25. *these slides borrowed from past TA Jerry Chen - jerryjrchen.com

  26. for loop (in brief)

  27. for loop (in brief) You’ll find that almost always using a for loop is more convenient than a while loop!

  28. for loop (in brief) You’ll find that almost always using a for loop is more convenient than a while loop!

  29. for loop (in brief) You’ll find that almost always using a for loop is more convenient than a while loop! Saves lines of code! In general, for loops iterate over an iterable, and we’ll talk about how it works when we get to iterators near the middle of the semester.

  30. Call Expressions how do we evaluate function calls? 1) Evaluate the operator

  31. Call Expressions how do we evaluate function calls? 1) Evaluate the operator 2) Evaluate the operands

  32. Call Expressions how do we evaluate function calls? 1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame

  33. Call Expressions how do we evaluate function calls? 1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters

  34. Call Expressions how do we evaluate function calls? 1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

  35. Call Expressions how do we evaluate function calls? 1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body >>> print(print(“hi”))

  36. Call Expressions how do we evaluate function calls? 1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body >>> print(print(“hi”)) hi

  37. Call Expressions how do we evaluate function calls? 1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body >>> print(print(“hi”)) hi None

  38. Environment Diagrams let’s draw an environment diagram for this function.

  39. Environment Diagrams let’s draw an environment diagram for this function. def statement procedure: 1) Write the function on the right side, with appropriate intrinsic name + parent 2) Write the name of the function on the left in current frame 3) Draw an arrow to the function object on the right! 4) Skip the body.

  40. Environment Diagrams let’s draw an environment diagram for this function. assignment statement procedure: 1) Evaluate the right hand side 2) Bind that value to a name on the left 3) If the name doesn’t exist in the current frame, add it! Otherwise change the current value

  41. Environment Diagrams let’s draw an environment diagram for this function. assignment statement procedure: 1) Evaluate the right hand side 2) Bind that value to a name on the left 3) If the name doesn’t exist in the current frame, add it! Otherwise change the current value

  42. Environment Diagrams let’s draw an environment diagram for this function. function call procedure: 1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body *don’t open a frame for built-in functions!

  43. Environment Diagrams let’s draw an environment diagram for this function. function call procedure: 1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body *don’t open a frame for built-in functions!

  44. Environment Diagrams let’s draw an environment diagram for this function. return to where we called the function from! assignment statement procedure: 1) Evaluate the right hand side 2) Bind that value to a name on the left 3) If the name doesn’t exist in the current frame, add it! Otherwise change the current value

  45. Environment Diagrams let’s draw an environment diagram for this function. function call procedure: 1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body *don’t open a frame for built-in functions!

  46. Environment Diagrams let’s draw an environment diagram for this function. function call procedure: 1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body *don’t open a frame for built-in functions!

  47. Environment Diagrams let’s draw an environment diagram for this function. function call procedure: 1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body *don’t open a frame for built-in functions!

  48. Environment Diagrams let’s draw an environment diagram for this function. ! ! m a r g o r p f o d n e …and we’re done!

  49. Please remember. remember remember remember

  50. Please remember. remember remember remember rmember remebrer

  51. Please remember. remember remember remember rmember remebrer remmrebber

  52. Please remember. remember remember remember rmember remebrer remmrebber function call procedure: 1) Evaluate the operator 2) Evaluate the operands print(double(3)) 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

  53. Please remember. remember remember remember rmember remebrer remmrebber function call procedure: 1) Evaluate the operator f1 f2 2) Evaluate the operands print(double(3)) 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

  54. Please remember. remember remember remember rmember remebrer remmrebber function call procedure: 1) Evaluate the operator f1 f2 2) Evaluate the operands print(double(3)) 3) Open the call frame 4) Bind the parameters 5) Evaluate the body Which frame is opened first, f1 or f2?

  55. Please remember. remember remember remember rmember remebrer remmrebber function call procedure: 1) Evaluate the operator f1 f2 2) Evaluate the operands print(double(3)) 3) Open the call frame 4) Bind the parameters 5) Evaluate the body Which frame is opened first, f1 or f2? f2

  56. Please remember. remember remember remember rmember remebrer remmrebber function call procedure: 1) Evaluate the operator f1 f2 2) Evaluate the operands print(double(3)) 3) Open the call frame 4) Bind the parameters 5) Evaluate the body Which frame is opened first, f1 or f2? f2!

  57. Please remember. remember remember remember rmember remebrer remmrebber function call procedure: 1) Evaluate the operator f1 f2 2) Evaluate the operands print(double(3)) 3) Open the call frame 4) Bind the parameters 5) Evaluate the body Which frame is opened first, f1 or f2? f2!!

  58. Please remember. remember remember remember rmember remebrer remmrebber function call procedure: 1) Evaluate the operator f1 f2 2) Evaluate the operands print(double(3)) 3) Open the call frame 4) Bind the parameters 5) Evaluate the body Which frame is opened first, f1 or f2? f2!!!

  59. Please remember. remember remember remember rmember remebrer remmrebber function call procedure: 1) Evaluate the operator f1 f2 2) Evaluate the operands print(double(3)) 3) Open the call frame 4) Bind the parameters 5) Evaluate the body Which frame is opened first, f1 or f2? f2!!! Inner functions are evaluated first , because they’re an operand of outer functions.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend