Creating your own classes class Elephant(object): """A virtual pet""" def __init__(self, name, age, weight, trunkradius, trunklength): self.name = name.

Slides:



Advertisements
Similar presentations
In our lesson today we will learn how to find the area of a building.
Advertisements

Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
From datetime import * class StopWatch(object): def __init__(self): now = datetime.now() self.hr = now.hour self.min = now.minute self.sec = now.second.
INFO 206 Lab Exercise 1 Introduction to Classes and Objects 1/18/2012i206 Lab 1 - Exercise1.
Congruence and Similarity
Distributive Property / Combining Like Terms Word Problems
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Surface Area of Rectangular Prisms 1.How many outside surfaces does a rectangular prism have? 2.What shape are each of the faces? The six rectangular sides.
Python classes: new and old. New and classic classes  With Python 2.2, classes and instances come in two flavors: old and new  New classes cleaned up.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Lesson 2-2 Example Kaliska is playing a board game with her family. A $5 bill of the play money has two sides with a length of 12 centimeters and.
Inheritance. Inhertance Inheritance is used to indicate that one class will get most or all of its features from a parent class. class Dog(Pet): Make.
Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.
What is area? The amount of space that a figure encloses
What is area? The amount of space that a figure encloses The number of square units that covers a shape or figure. It is two-dimensional It is always.
By: Chris Harvey Python Classes. Namespaces A mapping from names to objects Different namespaces have different mappings Namespaces have varying lifetimes.
James Tam Classes and Objects You will learn how to define new types of variables.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Computer Science 111 Fundamentals of Programming I Model/View/Controller and Data model design.
Python Programming in Context Chapter 12. Objectives To introduce the concept of inheritance To create a working object-oriented graphics package To provide.
Area & Perimeter. SHAPE OVERVIEW Rectangle Triangle Hexagon Trapezoid Square Parallelogram Pentagon Circle.
DO NOW!!! (1 st ) 1.A rectangular prism has length 4 cm, width 5 cm, and height 9 cm. a) Find the area of the cross section parallel to the base. b) Find.
Guide to Programming with Python Week 11 Chapter Nine Inheritance Working with multiple objects.
Surface Area and Volume 7 th Grade Surface Area of Prisms Surface Area = The total area of all the surfaces of a three- dimensional object.
Distribute and Combine Like Terms Applications. What is the area of the following shape? 5 2x-3 1.
Rectangular Prism A solid (3-dimensional) object which has six faces that are rectangles. Volume = Length × Width × Height Which is usually shortened.
PYTHON OBJECTS & CLASSES. What is an object? The abstract idea of anything What is in an object: Attributes Characteristics Represented by internal variables.
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
Dr. Philip Cannata 1 Python Classes. Dr. Philip Cannata 2.
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Python – May 19 Review –What is the difference between: list, tuple, set, dictionary? –When is it appropriate to use each? Creating our own data types:
Make a Model A box company makes boxes to hold popcorn. Each box is made by cutting the square corners out of a rectangular sheet of cardboard. The rectangle.
Making our own Classes and objects  As in real life, we’re now creating classes of objects.  a class that defines basic characteristics and functions.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
+ Pyramids and Prisms. + Solid An object with 3 Dimensions Height, Width, Length.
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.
INTRO2CS Tirgul 8 1. What We’ll Be Seeing Today  Introduction to Object-Oriented Programming (OOP).  Using Objects  Special methods 2.
Perimeter & Area. Today’s Objectives:  Learn what it means to find perimeter and area.  Practice finding or estimating the perimeter and area.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CMSC201 Computer Science I for Majors Lecture 18 – Classes and Modules (Continued, Part 3) Prof. Katherine Gibson Based on slides from the.
CMSC201 Computer Science I for Majors Lecture 25 – Classes
EECS 110: Lec 15: Classes and Objects (2)
Guide to Programming with Python
Area of a Triangle.
Area of a Rectangle = base x height
Computer Science 111 Fundamentals of Programming I
UNIT 8: 2-D MEASUREMENTS PERIMETER AREA SQUARE RECTANGLE PARALLELOGRAM
Python Classes Python has Classes, Methods, Overloaded operators, as well as Inheritance. Its somewhat 'loose' in the since it does not have true encapsulation,
Object Oriented Programming in Python
More on Functions (Part 2)
Computer Science 111 Fundamentals of Programming I
Conrad Huang Genentech Hall, N453A x6-0415
CSC1018F: Object Orientation, Exceptions and File Handling (Tutorial)
Area & Perimeter.
Python classes: new and old
EECS 110: Lec 15: Classes and Objects (2)
Object-Oriented Programming and class Design
Python classes: new and old
By- Sabrina,Julianna, and Killian
Making our own Classes and objects
CSE 231 Lab 13.
Area and Volume How to … Calculate the area of a square or rectangle
Presentation transcript:

Creating your own classes class Elephant(object): """A virtual pet""" def __init__(self, name, age, weight, trunkradius, trunklength): self.name = name self.age = age self.weight = weight self.trunkradius = trunkradius self.trunklength = trunklength We now have a class of Elephants Elephants starts with a capital letter Our Elephants all have names and ages. __init__: __init__() is always the constructor in Python The two __ on both sides of the word init indicate that it is a special name Constructor is automatically invoked when the an object of this type is created.

Creating an Elephant named Rita: class Elephant(object): """A virtual pet""“ def __init__(self, name, age, weight, trunkradius, trunklength): self.name = name self.age = age self.weight = weight self.trunkradius = trunkradius self.trunklength = trunklength # main elephant1 = Elephant(“Rita”,32,6080,10,96) print(“The Elephant’s name is “ + elephant1.name + “and her age is “ + str(elephant1.age)) elephant2 = Elephant(“Harold”,28,7940,14,100) print(“The Elephant’s name is “ + elephant2.name + “and his age is “ + str(elephant2.age))

Creating an Elephant named Rita: class Elephant(object): """A virtual pet""“ def __init__(self, name, age, weight, trunkradius, trunklength): self.name = name self.age = age self.weight = weight self.trunkradius = trunkradius self.trunklength = trunklength # main elephant1 = Elephant(“Rita”,32,6080,10,96) print(“The Elephant’s name is “ + elephant1.name + “and her age is “ + elephant1.age) print(“Her trunk’s volume is" ) ???

Creating an Elephant named Rita: class Elephant(object): """A virtual pet""" def __init__(self, name, age, weight, trunkradius, trunklength): self.name = name self.age = age self.weight = weight self.trunkradius = trunkradius self.trunklength = trunklength def trunkvol(self): return(self.trunkradius**2 * math.pi * self.trunklength) # main elephant1 = Elephant("Rita",32,6080,10,96) print("The Elephant's name is " + elephant1.name + "and her age is " + str(elephant1.age)) print("Her trunk's volume is " + str(elephant1.trunkvol() ))

Example: class Rectangle(object): """ A rectangle is a shape with width and height [DESCRIPTION] width - number [PROPERTIES] height - number """ def __init__(self, width, height): #[CONSTRUCTOR] self.width = width self.height = height """ def Rect_function(self,...): [TEMPLATE] return self.width... self.height... ""“ #main rectangle1 = Rectangle(10, 10) #[EXAMPLES] rectangle2 = Rectangle(20, 5)

from cisc106 import * class Rectangle: """ A rectangle is a shape with width and height [DESCRIPTION] width - number [PROPERTIES] height - number """ def __init__(self, width, height): #[CONSTRUCTOR] self.width = width self.height = height """ def Rect_function(self,...): [TEMPLATE] return self.width... self.height... """ def area(self): """ Computes the area of a Rectangle object aRectangle - Rectangle return - number """ return self.width * self.height rectangle1 = Rectangle(10, 10) #[EXAMPLES] rectangle2 = Rectangle(20, 5) assertEqual(rectangle1.area(), 100) assertEqual(rectangle2.area(), 100) assertEqual(Rectangle(6,8).area(),48)

from cisc106 import * class Rectangle: def __init__(self, width, height): #[CONSTRUCTOR] self.width = width self.height = height def area(self): return self.width * self.height def boxvol(self,x): return self.width * self.height * x mugwump = Rectangle(10, 10) #[EXAMPLES] rectangle2 = Rectangle(20, 5) assertEqual(mugwump.area(), 100) assertEqual(rectangle2.boxvol(2), 200) assertEqual(Rectangle(6,8).area(),48)

Class for a wallet? class Wallet(object): def __init__(self,tw,te,fi,on,ch): self.twenties = tw self.tens = te self.fives = fi self.ones = on self.change = ch self.amount = self.calcamt() def calamt(self): return self.twenties*20 + self.tens * 10 + self.fives * 5 + self.ones + self.change/100.0 def spend(self,spend_amt): if spend_amt >= 20: self.twenties = self.twenties - math.floor(spend_amt/20) spend_amt = spend_amt%20 if spend_amt >= 10: self.tens = self.tens - math.floor(spend_amt/10) spend_amt = spend_amt%10 if spend_amt >= 5: self.fives = self.fives - math.floor(spend_amt/5) spend_amt = spend_amt%5 if spend_amt >= 1: self.ones = self.ones - math.floor(spend_amt) spend_amt = spend_amt%1 self.change = self.change - spend_amt * 100 self.amount = self.calcamt() wallet = Wallet(2,3,5,3,24) assertEqual(wallet.amount,98.24) wallet.spend(22.12) assertEqual(wallet.amount, _________) assertEqual(wallet.amount, 76.12) wallet.spend(35.25) assertEqual(wallet.amount,_________) assertEqual(wallet.amount,40.87) Now write a method (class function) to add money to the wallet (Much simpler function!)

class Wallet: def __init__(self,tw,te,fi,on,ch): self.twenties = tw self.tens = te self.fives = fi self.ones = on self.change = ch self.amount = self.calc_amt() def calc_amt(self): return self.twenties*20 + self.tens * 10 + self.fives * 5 + self.ones + self.change/100.0 def add(self,tw,te,fi,on,ch): self.twenties += tw self.tens = self.tens + te self.fives = self.fives + fi self.ones = self.ones + on self.change = self.change + ch self.amount = self.calc_amt() def spend(self,spend_amt): if spend_amt >= 20: self.twenties = self.twenties - math.floor(spend_amt/20) spend_amt = spend_amt%20 if spend_amt >= 10: self.tens = self.tens - math.floor(spend_amt/10) spend_amt = spend_amt%10 if spend_amt >= 5: self.fives = self.fives - math.floor(spend_amt/5) spend_amt = spend_amt%5 if spend_amt >= 1: self.ones = self.ones - math.floor(spend_amt) spend_amt = spend_amt%1 self.change = self.change - spend_amt * 100 self.amount = self.calc_amt() wally = Wallet(3,2,2,4,1,32) wally.add(1,1,1,1,1)

from datetime import * class StopWatch(object): def __init__(self): now = datetime.now() self.hr = now.hour self.min = now.minute self.sec = now.second self.micro = now.microsecond def converttoms(self): return self.micro+self.sec* self.min*60* self.hr*60*60* def time_diff(self,watch): x = self.converttoms() y = watch.converttoms() diff = abs(x - y) return(diff) time1 = StopWatch() print time1.micro time2 = StopWatch() print time2.micro print(time1.time_diff(time2)) Class Stopwatch?