Two things for this week: have fun with graphics/drawing pictures, learn about objects and object-oriented programming (OOP).
Technically, almost everything in python is an object. Most objects have both data and functions (now called methods), together in one thing (the object).
Here are some simple examples of calling methods on a python list object:
>>> L = []
>>> L.append("A")
>>> L.append("B")
>>> L.append("C")
>>> print(L)
['A', 'B', 'C']
>>> L.reverse()
>>> print(L)
['C', 'B', 'A']
>>> L.sort()
>>> print(L)
['A', 'B', 'C']
>>> L.count("A")
1
>>> L.count("Z")
0
The list methods used above are append()
(add an item to the list), reverse()
(reverse the order of the list), sort()
(sort items from low to high), and
count()
(count how many are in list). There are other python list methods available.
Here are some string methods, acting on a string object:
>>> S = "swarthmore"
>>> S.upper()
'SWARTHMORE'
>>> S.count("r")
2
>>> S.index("w")
1
>>> S.isalpha()
True
>>> print(S)
swarthmore
The string methods used above are: upper()
(return uppercase version of string),
count()
(count how many are in string), index()
(show where char is in string),
and isalpha()
(return True
if string in only alphabetic chars). There are many
other python str methods available.
Note: strings are immutable, so even calling S.upper()
doesn't change what
is stored in S
(it just returns a new, uppercase, string). If you want to change
what is stored in the variable S
, you could use S = S.upper()
.
Using objects in your programs is just a different way to organize your data, but also provides advantages as programs and programming teams get larger and more complex (divide and conquer, code reuse, easier to read/write/debug, etc).
The syntax for calling methods on objects is object.method()
, where the
method may or may not require arguments.
This week we will learn the Zelle graphics library, which is an excellent example of using objects (i.e., it's easy to see the objects, because they are graphical objects, like circles and squares, drawn on the screen).
To use the Zelle graphics library, we need to import the classes and functions with this at the top of our programs:
from graphics import *
(and don't name your program graphics.py
, because that's what the Zelle graphics
file you are importing is named).
This program creates a graphics window and draws a circle in the middle of the window:
from graphics import *
def main():
width = 400
height = 400
gw = GraphWin("first graphics!", width, height)
cp = Point(width/2, height/2) # center point
c = Circle(cp, width/3)
c.draw(gw)
c.setFill("green")
# wait for click to end
gw.getMouse()
############################################
main()
Things to note:
GraphWin
, a Point
, and a Circle
draw()
and the setFill()
methods are
usedgetMouse()
method is used to wait for a mouse click, which ends
the programAnd here's the image:
Look through the documentation on the Zelle graphics library and learn about all of the possible objects (Circle, Rectangle, Text, etc), as well as the methods that go with each object (draw(), move(), setFill(), etc).
See if you can draw this simple scene: