71

Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 2: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Life after CS106AP!

Day 1!

Page 3: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Life after CS106AP!

Day 1!

Page 4: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Life after CS106AP!

Day 1!

Page 5: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 6: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 7: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 8: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 9: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

while True:

if stop_condition:

break

# ‘Animate’ object

pause(timestep)

Page 10: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

while True:

if stop_condition:

break

# ‘Animate’ object

pause(timestep) Small movements help make the animation appear continuous!

Page 11: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

while True:

if stop_condition:

break

# ‘Animate’ object

pause(timestep)

How long to wait in milliseconds

Page 12: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 13: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Definition

Page 14: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Definition

ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Page 15: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Definition

Classes allow us to define our own data types!

Page 16: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○

Definition

Page 17: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○

image = SimpleImage(width, height)

Creates an instance of the SimpleImage class (i.e. an object of the type SimpleImage)

Page 18: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○

●○○○

Page 19: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○

●○ oval.fill_color oval.width○○ Variables stored inside

the class

Page 20: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○

●○ oval.fill_color oval.width○ oval.move()○

Functions you can call on the object

Page 21: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○

●○ oval.fill_color oval.width○ oval.move()○ GOval(width, height)

How you create the object

Page 22: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 23: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Pynstagram.py

Page 24: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

PynstaUser

Page 25: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 26: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

PynstaUser

●○○○

●○○

Page 27: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

PynstaUser

●○○○

●○○

● PynstaUser(name)

Page 28: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

class PynstaUser:

def __init__(self, username): self.name = username self.friends = [] self.posts = []

def add_friend(self, user): …

def post(self, message): ...

Methods

Attributes (must start ith self. to be attributes!)

Class definition and name

Constructor

Page 29: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 30: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Page 31: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○○

●○

Page 32: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○○

●○

Today!

Page 33: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 34: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Encapsulation!

Page 35: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Definition

Page 36: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Definition

a class!

Page 37: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●●●●

Page 38: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

vs.

Page 39: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Page 40: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Page 41: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Page 42: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Page 43: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 44: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○○

Page 45: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○

Page 46: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○○

Page 47: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○○

Getters and setters!

Page 48: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

●○○

○more on this tomorro

Page 49: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 50: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Bubbles.py

Page 51: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 52: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Page 53: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Life after CS106AP!

Day 1!

Page 54: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 55: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types
Page 56: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Event-driven programming!

Page 57: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def main():

...

...

def your_mouse_listener():

...

Page 58: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def main():

...

...

def your_mouse_listener():

...

Definition

Page 59: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def main():

...

...

def your_mouse_listener():

...

Definition

clicking, moving, dragging

Page 60: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def main():

...

...

def your_mouse_listener():

...

Page 61: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def main():

...

...

def your_mouse_listener():

...

Page 62: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def main():

...

...

def your_mouse_listener():

...

MOUSE CLICK

Page 63: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def main():

...

...

def your_mouse_listener():

...

MOUSE CLICK

Page 64: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def main():

...

...

def your_mouse_listener():

...

MOUSE CLICK

The function happens immediately, no matter where you are in your program!

Page 65: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def mouse_listener_handler(event):

...

Page 66: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def mouse_listener_handler(event):

...

It must take in an event for campy to recognize it as a valid mouse listener.

Page 67: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def mouse_listener_handler(event):

...

event gives us access to information about the mouse event (e.g. x, y coordinates of the click).

Page 68: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def mouse_listener_handler(event):

...

onmouseevent()

onmouseclicked(mouse_listener_handler)

Page 69: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def mouse_listener_handler(event):

...

onmouseevent()

onmouseclicked(mouse_listener_handler)

Pass in your mouse listener function as the argument

Page 70: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

def mouse_listener_handler(event):

...

onmouseevent()

onmouseclicked(mouse_listener_handler)

Don’t include parentheses after the function name!

Page 71: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types

Bubbles.py