Python Programming An Introduction to Computer Science Chapter

  • Slides: 26
Download presentation
Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics Python Programming,

Python Programming: An Introduction to Computer Science Chapter 4 Objects and Graphics Python Programming, 2/e 1

Objectives n n n To understand the concept of objects and how they can

Objectives n n n To understand the concept of objects and how they can be used to simplify programs To be familiar with some objects available in Zelle's graphics library To be able to create objects in programs and call appropriate methods to perform graphical computations Python Programming, 2/e 2

Overview n n Each data type can represent a certain set of values, and

Overview n n Each data type can represent a certain set of values, and each had a set of associated operations The traditional programming view is that data is passive – it’s manipulated and combined with active operations Python Programming, 2/e 3

Overview n n n Modern computer programs are built using an object-oriented approach Most

Overview n n n Modern computer programs are built using an object-oriented approach Most applications you’re familiar with have Graphical User Interfaces (GUI) that provide windows, icons, buttons and menus There’s a graphics library (graphics. py) written specifically to go with this book. It’s based on Tkinter Python Programming, 2/e 4

The Object of Objects n n n Basic idea – view a complex system

The Object of Objects n n n Basic idea – view a complex system as the interaction of simpler objects. An object is a sort of active data type that combines data and operations Objects know stuff (contain data) and they can do stuff (have operations) Objects interact by sending each other messages Python Programming, 2/e 5

The Object of Objects n n Suppose we want to develop a data processing

The Object of Objects n n Suppose we want to develop a data processing system for a college or university. We must keep records on students who attend the school. Each student will be represented as an object. Python Programming, 2/e 6

The Object of Objects n The student object would contain data like: n n

The Object of Objects n The student object would contain data like: n n n n Name ID number Courses taken Campus Address Home Address GPA Etc. Python Programming, 2/e 7

The Object of Objects n n n The student object should also respond to

The Object of Objects n n n The student object should also respond to requests. We may want to send out a campus-wide mailing, so we’d need a campus address for each student. We could send the print. Campus. Address message to each student object. When the student object receives the message, it prints its own address. Python Programming, 2/e 8

Object of Objects n n Objects may refer to other objects. Each course might

Object of Objects n n Objects may refer to other objects. Each course might be represented by an object: n n Instructor Student roster Prerequisite courses When and where the class meets Python Programming, 2/e 9

Sample Operations ista 130 = Course("CESL 102") ista 130. add. Student('Jessie') ista 130. add.

Sample Operations ista 130 = Course("CESL 102") ista 130. add. Student('Jessie') ista 130. add. Student('Casey') ista 130. remove. Student('Jessie') ista 130. change. Room("CESL 103") print(ista 130. number. Of. Students()) # Guess the Output Python Programming, 2/e 10

str objects n n are immutable, cannot change them can be referenced with a

str objects n n are immutable, cannot change them can be referenced with a variable a. Str. Object = str('a string object') n have data (not seen) n n a sequence of characters an integer as current number of characters locale like U. S. , Germany, or China, . . . have operations (methods) upped = a. Str. Object. upper() 11

Other str operations http: //docs. python. org/library/stdtypes. html#string-methods s = str('Jessie Casey') # a

Other str operations http: //docs. python. org/library/stdtypes. html#string-methods s = str('Jessie Casey') # a str object s. count('e') # Value? s. index('ssi') s. index('Not') # Value? s. endswith('sey') # Value? s. split(' ') # Value? Python Programming, 2/e 12

Simple Graphics Programming n n This chapter uses the graphics. py library supplied with

Simple Graphics Programming n n This chapter uses the graphics. py library supplied with the supplemental materials Two location choices n n In Python’s Lib directory with other libraries In the same folder as your graphics program (what we'll do) n like copying Rick's test modules Python Programming, 2/e 13

Simple Graphics Programming n Import all functions with '*' and avoid using graphics. in

Simple Graphics Programming n Import all functions with '*' and avoid using graphics. in graphics. graph. Win() from graphics import * n n A graphics window is a place on the screen where the graphics will appear win = Graph. Win() What type of object is win? Python Programming, 2/e 14

Simple Graphics Programming n Windows can be closed/destroyed by issuing the command win. close()

Simple Graphics Programming n Windows can be closed/destroyed by issuing the command win. close() Python Programming, 2/e 15

Simple Graphics Programming n n n A graphics window is a collection of points

Simple Graphics Programming n n n A graphics window is a collection of points called pixels (picture elements). The default Graph. Win is 200 pixels tall by 200 pixels wide (40, 000 pixels total). One way to get pictures into the window is one pixel at a time, which would be tedious. The graphics routine has a number of predefined routines to draw geometric shapes. Python Programming, 2/e 16

-10 -20 -30 -40 -50 - Simple Graphics Programming n -60 -70 -80 -90

-10 -20 -30 -40 -50 - Simple Graphics Programming n -60 -70 -80 -90 -100 -120 - n -130 -140 - n -150 -160 -170 n The simplest object is the Point. Like points in geometry, point locations are represented with a coordinate system (x, y), where x is the horizontal location of the point and y is the vertical location. The origin (0, 0) in a graphics window is the upper left corner. X values increase from right to left, y values from top to bottom. Lower right corner is (199, 199) Python Programming, 2/e 17

Point and Circle objects from graphics import * win = Graph. Win() p =

Point and Circle objects from graphics import * win = Graph. Win() p = Point(50, 60) p. draw(win) p. get. X() # Value? ____ p. get. Y() # Value? ____ circ = Circle(p, 20) circ. draw(win) # What is the size of the # radius in pixels? _____ Python Programming, 2/e 18

Using Graphical Objects n n Computation is preformed by asking an object to carry

Using Graphical Objects n n Computation is preformed by asking an object to carry out one of its operations In the previous examples we manipulated str, Graph. Win, and Point n These are examples of classes Python Programming, 2/e 19

Using Graphical Objects n The class describes the properties of the instance: the data

Using Graphical Objects n The class describes the properties of the instance: the data and operations n n We will write our own classes in chapter 10 Each object is an instance of some class, If we say that Augie is a dog, then Augie is a specific individual in the larger class of all dogs. n Augie is an instance of the dog class. Python Programming, 2/e 20

Using Graphical Objects n To create a new instance of a class, we use

Using Graphical Objects n To create a new instance of a class, we use a special operation called a constructor <class-name>(<param 1>, <param 2>, …) n n <class-name> is the type of object s 1 = str('Leslie') p = Point(50, 60) c = Circle(p, 20) The arguments become the data remembered by the object Python 2/euse a To create a new instance of. Programming, a class, we 21

Using Graphical Objects n Only the most relevant instance variables are shown (others include

Using Graphical Objects n Only the most relevant instance variables are shown (others include the color, window they belong to, etc. ) Python Programming, 2/e 22

Using Graphical Objects n n To perform an operation on an object, we send

Using Graphical Objects n n To perform an operation on an object, we send the object a message. The set of messages an object responds to are called the methods of the object. Methods are functions that live inside the object. Methods are invoked using dot-notation: <object>. <method-name>(<param 1>, <param 2>, …) s. rjust(8, '*') # s is str object p. draw(win) # p is a Point object from. Left = p. get. X() c. set. Fill('yellow') # c is a Circle object Python Programming, 2/e 23

Circle remembers its Point circ = Circle(Point(100, 100), 30) win = Graph. Win() circ.

Circle remembers its Point circ = Circle(Point(100, 100), 30) win = Graph. Win() circ. draw(win) 24

Rectangle Objects upper. Left = Point(50, 50) lower. Right = Point(150, 80) rect =

Rectangle Objects upper. Left = Point(50, 50) lower. Right = Point(150, 80) rect = Rectangle(upper. Left, lower. Right) rect. set. Fill('yellow') rect. set. Outline('red') rect. draw(win) # Is rect's height > width ______ # What color is rect's border? ______ # How many pixels are yellow if the border # is 1 pixel wide? ______ Python Programming, 2/e 25

The Graph. Win object Python Programming, 2/e 26

The Graph. Win object Python Programming, 2/e 26