life after cs106ap!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. ·...

128

Upload: others

Post on 08-Sep-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 2: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Life after CS106AP!

Day 1!

Page 3: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Life after CS106AP!

Day 1!

Page 4: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 5: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 6: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 7: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 8: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Content courtesy of Nick Parlante

Page 9: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Page 10: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Page 11: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Ask yourself if all of the conditions always need to be checked!

Page 12: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 13: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

not and or

● not and or○ not boolean_expression

○ boolean_expression_a and boolean_expression_b

○ boolean_expression_a or boolean_expression_b

Page 14: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

if ___________:

# Do something

elif ___________:

# Do something different

else:

# Do something else

if ___________:

# Do something

else:

# Do something else

Page 15: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

●○

●○

Page 16: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 17: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Image (c) 2019 HP Development Company, L.P. [source]

Page 18: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 19: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Variables!

Page 20: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Definition

Page 21: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Definition

Think of them as labels for containers!

Page 22: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

Page 23: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

Python objects are stored on RAM

Page 24: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

Page 25: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

Page 26: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

Page 27: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 28: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5

Page 29: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5

Page 30: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5

=

Definition

Page 31: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5

=

Definition i.e. attaching it to the bag

Page 32: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5

variable’s name

Page 33: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5

variable’svalue

Page 34: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5

Page 35: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5num_picked = 2

Page 36: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5num_picked = 2num_flowers = num_flowers – num_picked

Page 37: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5num_picked = 2num_flowers = num_flowers – num_picked

Think/Pair/Share: Try to predict what happens here!

Page 38: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

variable assignment!

Page 39: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

The right side of the equals sign always gets evaluated first.

Page 40: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

variable retrieval!

Page 41: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

Definition

Page 42: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

The right side of the equals sign always gets evaluated first.

Page 43: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

We get the values using variable retrieval (i.e. checking what suitcase is attached).

5 2

Page 44: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

Then we can evaluate the right hand side of the assignment.

5 2

Page 45: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

Then we can evaluate the right hand side of the assignment.

5 2

3

Page 46: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5 num_picked = 2 num_flowers =

The right side of the equals sign always gets evaluated first.

3

Page 47: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5 num_picked = 2 num_flowers =

This is a new Python object!

3

Page 48: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

num_flowers = 5 num_picked = 2 num_flowers =

Python handles all the baggage for you when you use variables.

3

Page 49: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Page 50: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Style note

Page 51: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●=

Page 52: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●=

Page 53: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●=

Page 54: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 55: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○○

Page 56: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 57: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

Page 58: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

Page 59: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

Page 60: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

num_flowers = 5

Page 61: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

5 is an integer

num_flowers = 5

Page 62: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

5 is an integer

num_flowers = 5

Page 63: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

5.0 is a float(has decimals)

num_flowers = 5.0

Page 64: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

Page 65: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

●○

num_flowers = 5

Page 66: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

●○○

fraction = 0.2

Page 67: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

●○○

fraction = 0.2

Called “doubles” in some other languages

Page 68: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

●○○○

is_raining_today = True

Page 69: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

●○○○

Page 70: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 71: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●●●●●

Talk to your neighbor!

Page 72: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●●●●●

Page 73: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●●●●●

Page 74: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●●●●●

Page 75: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●●●●●

Page 76: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●●●●●

Page 77: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

type(x)

Page 78: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

● type(x)

● None

Page 79: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 80: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 81: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Page 82: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Literals are Python objectswritten directly in code, e.g. the 5 in num_flowers = 5

Page 83: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 84: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 85: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

()

*, /, //, %

+, -

Page 86: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

()

*, /, //, %

+, -

Ties within rows are broken by going from left to right

Page 87: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●●●●●●

Talk with a partner!

()

*, /, //, %

+, -

Page 88: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●●●●●●

()

*, /, //, %

+, -

Page 89: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●●●●●●

()

*, /, //, %

+, -

Page 90: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●●●●●●

NOTE: Any of the literals can also be replaced with variables that are associated with the same value

Page 91: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●●●●●●

For example:

x = 24 + x * 3

This evaluates to 10, just like our first example expression!

Page 92: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 93: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

The resulting expression always evaluates to true or false (but the operators don’t need to compare two booleans).

Page 94: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Therefore, we can use the resulting expression as the condition in if statements and while loops!

Page 95: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

()

*, /, //, %

+, -

<, <=, >, >=, ==, !=

Page 96: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

()

*, /, //, %

+, -

<, <=, >, >=, ==, !=

Ties still broken by going left to right!

Page 97: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

()

*, /, //, %

+, -

<, <=, >, >=, ==, !=

Page 98: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

What if the operator isn’t built in?

Page 99: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

math

Page 100: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

math

○ math.sqrt(4)

Page 101: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

math

○ math.sqrt(4)

Name of library

Library function

Page 102: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

math

○ math.sqrt(4)

● import math

Page 103: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

math

○ math.sqrt(4)

● import math

Page 104: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○

Page 105: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○○

Page 106: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Page 107: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

● math

○ math.sqrt(value)

Page 108: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

● math

Page 109: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 110: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

print()

def print_my_variables():

x = 5

y = 2

print(x)

print(y)

print(x + y)

Page 111: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

print()

def print_my_variables():

x = 5

y = 2

print(x)

print(y)

print(x + y)

Page 112: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

print()

def print_my_variables():

x = 5

y = 2

print(x)

print(y)

print(x + y)

NOTE: Variables only exist in the function in which they’re created

Page 113: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

print()

def print_my_variables():

x = 5

y = 2

print(x)

print(y)

print(x + y)

print() displays the value of what’s inside the parentheses

Page 114: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

print()

def print_my_variables():

x = 5

y = 2

print(x)

print(y)

print(x + y)

In functions, we can’t just type x or y directly like we did in the interpreter.

Page 115: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

print()

def print_my_variables():

x = 5

y = 2

print(x)

print(y)

print(x + y)

We’ll go into more depth about print() later this week!

Page 116: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 117: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Constants!

Page 118: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Style note

Page 119: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

MY_CONSTANT = 500

Style note

Page 120: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Page 121: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Page 122: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 123: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○○

Page 124: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○○

Constants!

Page 125: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

●○○

Page 126: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers
Page 127: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Page 128: Life after CS106AP!web.stanford.edu/class/archive/cs/cs106ap/cs106ap.1198/... · 2019. 7. 1. · num_flowers = This is a new Python object! 3. num_flowers = 5 num_picked = 2 num_flowers

Life after CS106AP!

Day 1!