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

Preview:

Citation preview

Life after CS106AP!

Day 1!

Life after CS106AP!

Day 1!

Content courtesy of Nick Parlante

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

not and or

● not and or○ not boolean_expression

○ boolean_expression_a and boolean_expression_b

○ boolean_expression_a or boolean_expression_b

if ___________:

# Do something

elif ___________:

# Do something different

else:

# Do something else

if ___________:

# Do something

else:

# Do something else

●○

●○

●○

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

Variables!

Definition

Definition

Think of them as labels for containers!

●○

●○

Python objects are stored on RAM

●○

●○

●○

num_flowers = 5

num_flowers = 5

num_flowers = 5

=

Definition

num_flowers = 5

=

Definition i.e. attaching it to the bag

num_flowers = 5

variable’s name

num_flowers = 5

variable’svalue

num_flowers = 5

num_flowers = 5num_picked = 2

num_flowers = 5num_picked = 2num_flowers = num_flowers – num_picked

num_flowers = 5num_picked = 2num_flowers = num_flowers – num_picked

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

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

variable assignment!

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

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

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

variable retrieval!

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

Definition

num_flowers = 5 num_picked = 2 num_flowers = num_flowers – num_picked

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

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

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

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

num_flowers = 5 num_picked = 2 num_flowers =

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

3

num_flowers = 5 num_picked = 2 num_flowers =

This is a new Python object!

3

num_flowers = 5 num_picked = 2 num_flowers =

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

3

Style note

●=

●=

●=

●○○

●○

●○

●○

●○

num_flowers = 5

●○

5 is an integer

num_flowers = 5

●○

5 is an integer

num_flowers = 5

●○

5.0 is a float(has decimals)

num_flowers = 5.0

●○

●○

●○

num_flowers = 5

●○

●○○

fraction = 0.2

●○

●○○

fraction = 0.2

Called “doubles” in some other languages

●○

●○○○

is_raining_today = True

●○

●○○○

●●●●●

Talk to your neighbor!

●●●●●

●●●●●

●●●●●

●●●●●

●●●●●

type(x)

● type(x)

● None

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

()

*, /, //, %

+, -

()

*, /, //, %

+, -

Ties within rows are broken by going from left to right

●●●●●●

Talk with a partner!

()

*, /, //, %

+, -

●●●●●●

()

*, /, //, %

+, -

●●●●●●

()

*, /, //, %

+, -

●●●●●●

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

●●●●●●

For example:

x = 24 + x * 3

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

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

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

()

*, /, //, %

+, -

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

()

*, /, //, %

+, -

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

Ties still broken by going left to right!

()

*, /, //, %

+, -

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

What if the operator isn’t built in?

math

math

○ math.sqrt(4)

math

○ math.sqrt(4)

Name of library

Library function

math

○ math.sqrt(4)

● import math

math

○ math.sqrt(4)

● import math

●○

●○○

● math

○ math.sqrt(value)

● math

print()

def print_my_variables():

x = 5

y = 2

print(x)

print(y)

print(x + y)

print()

def print_my_variables():

x = 5

y = 2

print(x)

print(y)

print(x + y)

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

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

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.

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!

Constants!

Style note

MY_CONSTANT = 500

Style note

●○○

●○○

Constants!

●○○

Life after CS106AP!

Day 1!