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

cs 61a discussion 1
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS 61A Discussion 1

Control and Environments

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/

Albert Xu

slide-2
SLIDE 2

Announcements

  • Homework 1 is due today @ 11:59pm.
  • Hog is due next Thursday
  • checkpoint on Monday (no partner)
  • 1 EC for submitting by next Wednesday
  • Additional Topics Lectures! (first one yesterday)
slide-3
SLIDE 3

Last Week’s Survey

slide-4
SLIDE 4

Last Week’s Survey

slide-5
SLIDE 5

Expressions vs. Statements

  • What is a value?


Expressions Statements

slide-6
SLIDE 6

Expressions vs. Statements

  • What is a value?

  • Primitive, such as int, list,

string, boolean


Expressions Statements

slide-7
SLIDE 7

Expressions vs. Statements

  • What is a value?

  • Primitive, such as int, list,

string, boolean


  • Can be assigned to a name


Expressions Statements

slide-8
SLIDE 8

Expressions vs. Statements

  • What is a value?

  • Primitive, such as int, list,

string, boolean


  • Can be assigned to a name

  • What is an expression?

Expressions Statements

slide-9
SLIDE 9

Expressions vs. Statements

  • What is a value?

  • Primitive, such as int, list,

string, boolean


  • Can be assigned to a name

  • What is an expression?
  • Evaluate to values

Expressions Statements

slide-10
SLIDE 10

Expressions vs. Statements

  • What is a value?

  • Primitive, such as int, list,

string, boolean


  • Can be assigned to a name

  • What is an expression?
  • Evaluate to values
  • Python interpreter has to

think to turn an expression into a value

Expressions Statements

slide-11
SLIDE 11

Expressions vs. Statements

  • What is a value?

  • Primitive, such as int, list,

string, boolean


  • Can be assigned to a name

  • What is an expression?
  • Evaluate to values
  • Python interpreter has to

think to turn an expression into a value

  • Think about expressions as

questions, values as answers

Expressions Statements

slide-12
SLIDE 12

Expressions vs. Statements

  • What is a value?

  • Primitive, such as int, list,

string, boolean


  • Can be assigned to a name

  • What is an expression?
  • Evaluate to values
  • Python interpreter has to

think to turn an expression into a value

  • Think about expressions as

questions, values as answers

  • Every expression evaluates to some value


Expressions Statements

slide-13
SLIDE 13

Expressions vs. Statements

  • What is a value?

  • Primitive, such as int, list,

string, boolean


  • Can be assigned to a name

  • What is an expression?
  • Evaluate to values
  • Python interpreter has to

think to turn an expression into a value

  • Think about expressions as

questions, values as answers

  • Every expression evaluates to some value

  • On the other hand, statements change

something about the environment


  • They won’t evaluate to anything, just

change the program flow


Expressions Statements

slide-14
SLIDE 14

Expressions vs. Statements

  • What is a value?

  • Primitive, such as int, list,

string, boolean


  • Can be assigned to a name

  • What is an expression?
  • Evaluate to values
  • Python interpreter has to

think to turn an expression into a value

  • Think about expressions as

questions, values as answers

  • Every expression evaluates to some value

  • On the other hand, statements change

something about the environment


  • They won’t evaluate to anything, just

change the program flow


  • Ex: while loop, if statement, def

statement, assignment statement


Expressions Statements

slide-15
SLIDE 15

Expressions vs. Statements

  • What is a value?

  • Primitive, such as int, list,

string, boolean


  • Can be assigned to a name

  • What is an expression?
  • Evaluate to values
  • Python interpreter has to

think to turn an expression into a value

  • Think about expressions as

questions, values as answers

  • Every expression evaluates to some value

  • On the other hand, statements change

something about the environment


  • They won’t evaluate to anything, just

change the program flow


  • Ex: while loop, if statement, def

statement, assignment statement


  • It’s important to know when you have

an expression, and when you have a statement


Expressions Statements

slide-16
SLIDE 16

Expressions vs. Statements

  • What is a value?

  • Primitive, such as int, list,

string, boolean


  • Can be assigned to a name

  • What is an expression?
  • Evaluate to values
  • Python interpreter has to

think to turn an expression into a value

  • Think about expressions as

questions, values as answers

  • Every expression evaluates to some value

  • On the other hand, statements change

something about the environment


  • They won’t evaluate to anything, just

change the program flow


  • Ex: while loop, if statement, def

statement, assignment statement


  • It’s important to know when you have

an expression, and when you have a statement


  • You can’t return the value of a

statement, but you can return the value of an expression!

Expressions Statements

slide-17
SLIDE 17

Programs are linear.

slide-18
SLIDE 18

Programs are linear.

Here is a program that prints something, does a calculation, then prints out the result.

slide-19
SLIDE 19

Programs are linear.

Here is a program that prints something, does a calculation, then prints out the result.

  • ops.
slide-20
SLIDE 20

Programs are linear.

Here is a program that prints something, does a calculation, then prints out the result. But this isn’t a very interesting program. Why?

slide-21
SLIDE 21

Programs are linear.

Here is a program that prints something, does a calculation, then prints out the result. But this isn’t a very interesting program. Why? It always does the same thing, regardless of inputs or its environment! This is just a glorified calculator.

slide-22
SLIDE 22

We need control.

slide-23
SLIDE 23

We need control.

Here is a stoplight. It tells us when to go and when to stop.

slide-24
SLIDE 24

We need control.

Here is a stoplight. It tells us when to go and when to stop. Think of booleans as just that. True is go, False is stop.

slide-25
SLIDE 25

We need control.

Here is a stoplight. It tells us when to go and when to stop. Think of booleans as just that. True is go, False is stop. Okay, so that’s not exactly what they mean. But it’s pretty close.

slide-26
SLIDE 26

Boolean Operators

not: returns the opposite truth value!

>>> not True False >>> not False True

slide-27
SLIDE 27

Boolean Operators

not: returns the opposite truth value! and: true if both values are true, false otherwise

>>> not True False >>> True and True True >>> False and True False >>> not False True

slide-28
SLIDE 28

Boolean Operators

not: returns the opposite truth value! and: true if both values are true, false otherwise

  • r: true if at least one of the values is true, false otherwise

>>> not True False >>> True and True True >>> False and True False >>> False or True True >>> False or False False >>> not False True

slide-29
SLIDE 29

Not just true, but truthy.

Python assigns a boolean value to all values, not just True and False.

slide-30
SLIDE 30

Not just true, but truthy.

Python assigns a boolean value to all values, not just True and False. For non-boolean values, we call this truthy and falsy

slide-31
SLIDE 31

Not just true, but truthy.

Python assigns a boolean value to all values, not just True and False. For non-boolean values, we call this truthy and falsy What are some falsy values you know?

slide-32
SLIDE 32

Not just true, but truthy.

Python assigns a boolean value to all values, not just True and False. For non-boolean values, we call this truthy and falsy What are some falsy values you know?

[] “” {} None

slide-33
SLIDE 33

Not just true, but truthy.

Python assigns a boolean value to all values, not just True and False. For non-boolean values, we call this truthy and falsy What are some falsy values you know?

[] “” {} None

truthy values?

  • 1

5 “hello” [1, 2] functions

slide-34
SLIDE 34

Short Circuiting

Doing boolean operations is really slow. Turns out our friend the Python interpreter takes some shortcuts!

slide-35
SLIDE 35

Short Circuiting

Doing boolean operations is really slow. Turns out our friend the Python interpreter takes some shortcuts! Rules: 1) Return Immediately if you know the truth value, no need to evaluate the rest of the expression 2) No need to return true or false, just return the last value seen, since those values are truthy or falsy anyways :)

slide-36
SLIDE 36

Short Circuiting

Doing boolean operations is really slow. Turns out our friend the Python interpreter takes some shortcuts! Rules: 1) Return Immediately if you know the truth value, no need to evaluate the rest of the expression 2) No need to return true or false, just return the last value seen, since those values are truthy or falsy anyways :) Keep these rules in mind when we look at a few examples!

slide-37
SLIDE 37

Short Circuiting

Let’s check out a few examples… >>> True and 0 and 1 and -3

slide-38
SLIDE 38

Short Circuiting

Let’s check out a few examples… >>> True and 0 and 1 and -3

slide-39
SLIDE 39

Short Circuiting

Let’s check out a few examples… >>> True and 0 and 1 and -3

and returns true if both are true! But 0 is falsy, so we stop and return it.

slide-40
SLIDE 40

Short Circuiting

Let’s check out a few examples… >>> True and 0 and 1 and -3 >>> 0 or 1 / 0

slide-41
SLIDE 41

Short Circuiting

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

  • 2
  • r returns true if one of the

values is true! Once Python sees -2, it returns right away!

slide-42
SLIDE 42

Short Circuiting

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

  • 2

>>> False and 1 / 0

slide-43
SLIDE 43

Short Circuiting

Let’s check out a few examples… >>> True and 0 and 1 and -3 >>> -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)

slide-44
SLIDE 44

Final Note

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

slide-45
SLIDE 45

Final Note

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

slide-46
SLIDE 46

Final Note

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

slide-47
SLIDE 47

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!

slide-48
SLIDE 48

if statement

(and the rest of the suite)

slide-49
SLIDE 49

if statement

(and the rest of the suite)

slide-50
SLIDE 50

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.

slide-51
SLIDE 51

if statement

(and the rest of the suite)

  • kay, so if the first if wasn’t true, then

check the first elif. if true, then print out good job!, and ignore the rest of the block. …otherwise, keep going and check the next elif.

slide-52
SLIDE 52

if statement

(and the rest of the suite)

same with the other elifs…

slide-53
SLIDE 53

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

  • ut that ain’t it chief.
slide-54
SLIDE 54

if statement

(and the rest of the suite) An if statement block consists of:

  • an if clause
  • zero or more elif clauses
  • zero or one else clauses

the rule!

slide-55
SLIDE 55

while statement

(or loop) Sometimes you want to keep performing an action

  • ver an over again until a certain criterion is true.
slide-56
SLIDE 56

while statement

(or loop) Sometimes you want to keep performing an action

  • ver 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?

slide-57
SLIDE 57

while statement

(or loop) Sometimes you want to keep performing an action

  • ver 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?

slide-58
SLIDE 58

while statement

(or loop) Sometimes you want to keep performing an action

  • ver 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?

slide-59
SLIDE 59

while statement

(or loop) Sometimes you want to keep performing an action

  • ver an over again until a certain criterion is true.

I thought about how to fix it. Which of these is correct? A B

slide-60
SLIDE 60

while statement

(or loop) Sometimes you want to keep performing an action

  • ver 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…

slide-61
SLIDE 61

FizzBuzz

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

Warmup problem

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

slide-62
SLIDE 62

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

FizzBuzz

slide-63
SLIDE 63

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

FizzBuzz

slide-64
SLIDE 64

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

slide-65
SLIDE 65

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

slide-66
SLIDE 66

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

slide-67
SLIDE 67

for loop

(in brief)

slide-68
SLIDE 68

for loop

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

slide-69
SLIDE 69

for loop

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

slide-70
SLIDE 70

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.

slide-71
SLIDE 71

Call Expressions

how do we evaluate function calls?

1) Evaluate the operator

slide-72
SLIDE 72

Call Expressions

how do we evaluate function calls?

1) Evaluate the operator 2) Evaluate the operands

slide-73
SLIDE 73

Call Expressions

how do we evaluate function calls?

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame

slide-74
SLIDE 74

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

slide-75
SLIDE 75

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

slide-76
SLIDE 76

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”))

slide-77
SLIDE 77

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

slide-78
SLIDE 78

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

slide-79
SLIDE 79

Environment Diagrams

let’s draw an environment diagram for this function.

slide-80
SLIDE 80

Environment Diagrams

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.

let’s draw an environment diagram for this function.

slide-81
SLIDE 81

Environment Diagrams

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

let’s draw an environment diagram for this function.

slide-82
SLIDE 82

Environment Diagrams

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

let’s draw an environment diagram for this function.

slide-83
SLIDE 83

Environment Diagrams

function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

let’s draw an environment diagram for this function.

*don’t open a frame for built-in functions!

slide-84
SLIDE 84

Environment Diagrams

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!

let’s draw an environment diagram for this function.

slide-85
SLIDE 85

Environment Diagrams

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

let’s draw an environment diagram for this function.

return to where we called the function from!

slide-86
SLIDE 86

Environment Diagrams

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!

let’s draw an environment diagram for this function.

slide-87
SLIDE 87

Environment Diagrams

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!

let’s draw an environment diagram for this function.

slide-88
SLIDE 88

Environment Diagrams

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!

let’s draw an environment diagram for this function.

slide-89
SLIDE 89

Environment Diagrams

…and we’re done! let’s draw an environment diagram for this function.

e n d

  • f

p r

  • g

r a m ! !

slide-90
SLIDE 90

Please remember.

remember remember remember

slide-91
SLIDE 91

Please remember.

remember remember remember rmember remebrer

slide-92
SLIDE 92

Please remember.

remember remember remember rmember remebrer remmrebber

slide-93
SLIDE 93

Please remember.

remember remember remember rmember remebrer remmrebber function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

print(double(3))

slide-94
SLIDE 94

Please remember.

remember remember remember rmember remebrer remmrebber function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

print(double(3))

f1 f2

slide-95
SLIDE 95

Please remember.

remember remember remember rmember remebrer remmrebber function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

print(double(3))

f1 f2

Which frame is opened first, f1 or f2?

slide-96
SLIDE 96

Please remember.

remember remember remember rmember remebrer remmrebber function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

print(double(3))

f1 f2

Which frame is opened first, f1 or f2? f2

slide-97
SLIDE 97

Please remember.

remember remember remember rmember remebrer remmrebber function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

print(double(3))

f1 f2

Which frame is opened first, f1 or f2? f2!

slide-98
SLIDE 98

Please remember.

remember remember remember rmember remebrer remmrebber function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

print(double(3))

f1 f2

Which frame is opened first, f1 or f2? f2!!

slide-99
SLIDE 99

Please remember.

remember remember remember rmember remebrer remmrebber function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

print(double(3))

f1 f2

Which frame is opened first, f1 or f2? f2!!!

slide-100
SLIDE 100

Please remember.

remember remember remember rmember remebrer remmrebber function call procedure:

1) Evaluate the operator 2) Evaluate the operands 3) Open the call frame 4) Bind the parameters 5) Evaluate the body

print(double(3))

f1 f2

Which frame is opened first, f1 or f2? f2!!!

Inner functions are evaluated first, because they’re an operand of outer functions.

slide-101
SLIDE 101

Also…

Draw environment diagrams for these two snippets.

slide-102
SLIDE 102

Also…

Draw environment diagrams for these two snippets.

slide-103
SLIDE 103

Also…

When does the variable go inside the box? When do we draw an arrow to it? Draw environment diagrams for these two snippets.

slide-104
SLIDE 104

Also…

When does the variable go inside the box? When do we draw an arrow to it? Draw environment diagrams for these two snippets.

Primitive values go inside boxes, and they’re copied. For functions and other nonprimitive values, draw them on the right and draw pointers to them. Whenever you copy, just copy what’s already inside the box!

slide-105
SLIDE 105

Thanks for coming.

Have a great rest of your week! :)

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/