life after cs106ap! - web.stanford.edu · definition ints, strings, booleans, lists, floats,...
TRANSCRIPT
![Page 1: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/1.jpg)
![Page 2: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/2.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/3.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/4.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/5.jpg)
![Page 6: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/6.jpg)
![Page 7: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/7.jpg)
![Page 8: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/8.jpg)
![Page 9: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/9.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/10.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/11.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/12.jpg)
![Page 13: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/13.jpg)
Definition
![Page 14: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/14.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/15.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/16.jpg)
●○
Definition
![Page 17: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/17.jpg)
●○
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/18.jpg)
●○
●○○○
![Page 19: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/19.jpg)
●○
●○ 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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/20.jpg)
●○
●○ 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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/21.jpg)
●○
●○ 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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/22.jpg)
![Page 23: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/23.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/24.jpg)
PynstaUser
![Page 25: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/25.jpg)
![Page 26: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/26.jpg)
PynstaUser
●○○○
●○○
●
![Page 27: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/27.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/28.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/29.jpg)
![Page 30: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/30.jpg)
●
●
●
![Page 31: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/31.jpg)
●○○
●○
○
![Page 32: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/32.jpg)
●○○
●○
○
Today!
![Page 33: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/33.jpg)
![Page 34: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/34.jpg)
Encapsulation!
![Page 35: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/35.jpg)
Definition
![Page 36: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/36.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/37.jpg)
●●●●
![Page 38: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/38.jpg)
vs.
![Page 39: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/39.jpg)
●
●
![Page 40: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/40.jpg)
●
●
![Page 41: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/41.jpg)
●
●
![Page 42: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/42.jpg)
●
●
![Page 43: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/43.jpg)
![Page 44: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/44.jpg)
●○○
![Page 45: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/45.jpg)
●
●○
○
![Page 46: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/46.jpg)
●
●
●○○
○
![Page 47: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/47.jpg)
●
●
●○○
○
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/48.jpg)
●
●
●○○
![Page 49: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/49.jpg)
![Page 50: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/50.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/51.jpg)
![Page 52: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/52.jpg)
●
●
![Page 53: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/53.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/54.jpg)
![Page 55: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/55.jpg)
![Page 56: Life after CS106AP! - web.stanford.edu · Definition ints, strings, booleans, lists, floats, dictionaries, etc. are all built-in Python data types](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/56.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/57.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/58.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/59.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/60.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/61.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/62.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/63.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/64.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/65.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/66.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/67.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/68.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/69.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/70.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022041216/5e052e59671d22305538d511/html5/thumbnails/71.jpg)
Bubbles.py