Functions Review: WWPD print(print(one) and 2 and print(tHrE3)) - - PowerPoint PPT Presentation

functions review wwpd
SMART_READER_LITE
LIVE PREVIEW

Functions Review: WWPD print(print(one) and 2 and print(tHrE3)) - - PowerPoint PPT Presentation

Functions Review: WWPD print(print(one) and 2 and print(tHrE3)) Call Expressions A call expression calls a function on its arguments. 1. 2. 3. wears_jacket(100, True) operator operands Call Expressions A call expression calls


slide-1
SLIDE 1

Functions Review: WWPD

print(print(“one”) and 2 and print(“tHrE3”))

slide-2
SLIDE 2

Call Expressions

A call expression calls a function on its arguments. 1. 2. 3.

wears_jacket(100, True)

  • perator
  • perands
slide-3
SLIDE 3

Call Expressions

A call expression calls a function on its arguments. 1. Evaluate the operator to get a function. 2. Evaluate the operand(s) from left to right. 3. Apply the value of the operator on the value(s) of the operand(s).

wears_jacket(100, True)

  • perator
  • perands
slide-4
SLIDE 4

Boolean Stuff in Python

False-y

  • Truth-y
slide-5
SLIDE 5

Boolean Stuff in Python

False-y

  • False
  • None
  • “ ”, [ ], { }

Truth-y

  • True
  • 1
  • -1
  • “Hello”
  • Almost everything else
slide-6
SLIDE 6

Boolean Logic

And Or

slide-7
SLIDE 7

Boolean Logic

And

  • first false-y or last truth-y value (and stops evaluating there)
  • “Are you free Saturday and Sunday?”

Or

  • first truth-y or last false-y value (and stops evaluating there)
  • “Are you free Saturday or Sunday?”
slide-8
SLIDE 8

Discussion 1:

Caroline Lemieux (clemieux@berkeley.edu) January 31, 2019

Slides adapted from Nancy Shaw’s

slide-9
SLIDE 9

Announcements

  • HW 1 due tonight
  • Lab 0 & Lab 1 due tomorrow
  • Hog project

○ Phase 1 due next Tuesday ○ Whole project due next Thursday (can work with partners)

  • There are office hours tonight 6:30-8:00 if you need last minute help!
slide-10
SLIDE 10

Agenda

  • Concept check
  • Announcements
  • Anything you want to add to the agenda?
  • Review of Functions & Control
  • Environment Diagrams
slide-11
SLIDE 11

Control

slide-12
SLIDE 12

Control Review

n = 0 if n < 10: print(“1”) elif n >= 0: print(2)

slide-13
SLIDE 13

Control Review

n = 0 if n < 10: print(“1”) elif n >= 0: print(2)

1

slide-14
SLIDE 14

Control Review

n = 0 if n < 10: print(1) if n >= 0: print(2)

slide-15
SLIDE 15

Control Review

n = 0 if n < 10: print(1) if n >= 0: print(2)

1 2

slide-16
SLIDE 16

Control Review

n = 100 if n < 10: print(1) print(2)

slide-17
SLIDE 17

Control Review

n = 100 if n < 10: print(1) print(2)

2

slide-18
SLIDE 18

Control Review

n = 100 if n == 100: print(1) print(2)

1 2

slide-19
SLIDE 19

TRY 1.1

slide-20
SLIDE 20

Iteration

while <cond>: <body>

  • Keep evaluating body until <cond> is false-y

○ Should make sure it’s eventually false!

slide-21
SLIDE 21

TRY 1.2 & 1.3

slide-22
SLIDE 22

Summary

False Values: True Values: False, 0, None, Everything [ ], “ ”, { } else

And: first false, last true value Or: first true, last false value

if <cond>: ... elif <cond>: ... else: ...

while <cond>: ...

Any number of these Optional Keep evaluating body until False-y

SLIDE STOLEN FROM THE GREAT CHRIST ALLSMAN

slide-23
SLIDE 23

Attendance

links.cs61a.org/caro-disc

slide-24
SLIDE 24

Environment Diagrams

slide-25
SLIDE 25

Assignment Statements

x = 10 % 4 y = x x **= 2

slide-26
SLIDE 26

Assignment Statements

  • 1. Evaluate the right!
  • 2. Var | Val
slide-27
SLIDE 27

def Statements

def double(x): return x * 2 def triple(x): return x * 3 hmmm = double double = triple

Try it!

slide-28
SLIDE 28

def Statements

  • 1. Create a function thingy (intrinsic name,

parameters, and parent)

  • 2. FUNC | ---------> to thingy
slide-29
SLIDE 29

def Statements

  • 1. Create a function object (intrinsic name,

parameters, and parent)

  • 2. FUNC | ---------> to object
  • Note: function values are objects that are

POINTED POINTED POINTED to

  • (only values are not pointed at. Objects which you will learn later and lists are pointed at as well)
  • DO NOT evaluate the body of the function
slide-30
SLIDE 30

def Statements

def double(x): return x * 2 def triple(x): return x * 3 hmmm = double double = triple

Try it!

slide-31
SLIDE 31

Call expressions

def double(x): return x * 2 hmmm = double wow = double(3) hmmm(wow)

slide-32
SLIDE 32

Call Expressions

b(a)

  • perator
  • perand
  • Follow the golden rules of evaluation:

○ Evaluate operator ○ Evaluate operands ○ Apply operator to operands

  • Call expressions create new frames!
slide-33
SLIDE 33

Creating Frames

  • Label frame # (f1, f2, f3)
  • Label frame with function’s intrinsic name (the

thing being pointed at)

  • Label with the parent (defined earlier)
slide-34
SLIDE 34

Call Expressions

  • Bind parameters to arguments (what you

pass in aka the stuff in the parentheses)

  • Evaluate body using the golden rules
  • At end, be sure to put the return value

(default is None)

slide-35
SLIDE 35

Call expressions

def double(x): return x * 2 hmmm = double wow = double(3) hmmm(wow)

Try it!

slide-36
SLIDE 36

Lookups

  • When trying to find the value of a variable:

○ If it’s in your current frame, great! ○ If not, look in the parent of your frame, then in your parent’s parent, and so on ○ If there are no more parents (you’re in the global frame), it doesn’t exist!

slide-37
SLIDE 37

LET’S PUT IT ALL TOGETHER!

slide-38
SLIDE 38

Assignment Statements:

Evaluate RHS Make binding in current frame

Def Statements:

Don’t go into body yet Make binding in current frame

[P = G]

Call Expressions:

f1 [P = G] Label w/ index, name, parent Bind arguments to parameters Return something

[P = G]

slide-39
SLIDE 39

2.4

from operator import add def sub(a, b): sub = add return a - b add = sub sub = min print(add(2, sub(2, 3)))

slide-40
SLIDE 40

Common Misconceptions

When do you draw the pointer (vs not)?

slide-41
SLIDE 41

Common Misconceptions

return f vs return f()

How do you know when you should call a function and need to open a new frame?

slide-42
SLIDE 42

Common Misconceptions

f(a(2)) Which frame do I open first? Function f or function a?

slide-43
SLIDE 43

Common Misconceptions

x = 4 y = x a = func b = a

Why is 4 copied but why is func not? (ie. why are there not two copies of func)