Week 6: Graphics and objects

Week 6 Goals

  • Learn about objects and classes

  • Learn about object constructors and methods

  • Use the graphics library to practice using objects and methods

  • Learn how to use the sleep function to create simple animations

Week 6 Code

  • starting.py and circle_color.py: getting started with graphics

  • bullseye.py: activity using graphics to create a bullseye pattern

  • dots.py or bubbles.py: activity creating random circles on the screen

  • animate.py: walk through how to create simple animations

  • follow.py or bounce.py: more activities to practice using graphics

Week 6 Concepts

Objects and Classes

So far we have talked about types in python such as int, bool, and float, and types that behave as sequences, list, str, and range.

This week we will introduce objects and object-oriented programming.

Objects mimic real-world objects by combining data and functionality into single entities. We say that objects "know stuff and can do stuff"

  1. data (what it knows, e.g. int value, chars in str, items in list)

  2. methods/functions that work on data (what it does)

    • how to know if a string contains only lowercase letters

    • how to add an element to the end of a list

    • how to convert an int into a float

When we talk about objects, we distinguish between the definition of the object and an instance of the object:

  • class ⇒ definition of the type (e.g. int, str)

  • object ⇒ instance of a class (e.g. 6, "Hello")

Object dot notation

Methods are called using the dot notation in Python.

Here are some examples using dot notation with lists:

>>> lst = [1, 4, 16]
>>> len(lst)
3   # this information is stored in the list object
>>> lst.append(25)        # no return, lst now is [1,4,16,25]
>>> lst.extend([36, 49])  # no return, lst now is [1,4,16,25,36,49]
>>> lst.insert(2, 9)      # no return, lst now is [1,4,9,16,25,36,49]
>>> lst.count(36)
1
>>> lst.index(36)
5
>>> lst.pop()
49    # removes and returns last element
>>> lst.pop(0)
1     # removes and returns item at index 0
>>> lst.reverse()         # no return, lst now [36, 16, 9, 4]
>>> lst = [3, 6, 1, 2, 6, 8, 2, 7, 4]
>>> lst.sort()            # no return, lst now sorted [1, 2, 2, 3, 4, 6, 6, 7, 8]
>>> help(list)

Here are some examples using dot notation with strings:

>>> s = "Hello"
>>> len(s)
5   # this information is stored in the string object
>>> s.upper()
'HELLO'
>>> s.lower()
'hello'
>>> s.find('l')
2
>>> s.replace('e', 'a')
'Hallo'
>>> s.isalpha()
True
>>> s.isdigit()
False
>>> s.islower()
False
>>> s.lower().islower()
True
>>> s.upper()
False
>>> s.endswith('o')
True
>>> s.startswith('h')
False # it starts with capital H, not lowercase h
>>> s = 'one two three four'
>>> lst = s.split(' ')
>>> lst
['one', 'two', 'three', 'four']
>>> ','.join(lst)
'one,two,three,four'
>>> help(str)

Graphics

The Zelle graphics library is a simple graphics library that allows you to create simple graphics programs. It is not meant for creating state-of-the-art graphics, but it is a good way to learn about objects and classes.

The graphics window is a grid of pixels:

  X                                   the default size is 200 x 200 pixels
  ------------------------
Y |(0,0)            (w,0)|            horizontal axis is X
  |                      |            vertical axis is Y
  |                      |
  |                      |            upper lefthand corner is origin (0,0)
  |                      |
  |                      |            lower righthand corner is (width-1, height-1) pixel
  |                      |
  |                      |            the pixel in the center is width/2, height/2
  |(0,h)            (w,h)|
  ------------------------

To use the graphics library, you need to import the graphics library:

from graphics import *   # top of program outside of all functions

There are lots of notes already provided for the graphics library: