Lists, Tuples, Dictionaries, … + lots of computer work for the programmer's work! T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer'

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University
"Intelligent" CS 5 An object is structured data that is alive, responsible, and intelligent. Sound too friendly? This week’s objects and classes will be.
Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein Classes and Objects Object Oriented Programming.
Chapter 9 Imperative and object-oriented languages 1.
Types in Scripting Languages CS 351 – Programming Paradigms.
Modules and Objects Introduction to Computing Science and Programming I.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Class 24: Programming with Objects University of Virginia cs1120 David Evans.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
1 Python Control of Flow and Defining Classes LING 5200 Computational Corpus Linguistics Martha Palmer.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
A whole new class of programming CS 5 today HW10: Due Sun, Nov 15 Pr0: Ariane 5 Reading Pr1/Lab:the Date class Pr2 Connect4Board Pr3 Connect4Player (extra.
IS 313 Today Schedule Week 0: Classes Objects You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where.
Def tomorrow(self): """Changes the calling object so that it represents one calendar day after the date it originally represented. """ if self.month in.
Now String theory To Do: Lists This stuff hurts my brane. when you learn string theory from google images… Goal: Thinking like a machine You should now.
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
Classes and Objects The basics. Object-oriented programming Python is an object-oriented programming language, which means that it provides features that.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Looping and Counting Lecture 3 Hartmut Kaiser
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Please log on The. AN INTRODUCTION TO ‘Python is a high-level, general purpose programming language’ Python is one of the many programming languages.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
RUBY by Ryan Chase.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Q and A for Sections 6.2, 6.3 Victor Norman CS106.
IS 313 Today Schedule Week 0: files dictionaries You're saying that Python's got class ? Dictionaries and Classes Week 1: Week 2: Where we've been… Where.
Objects, If, Routines. Creating Objects an object is a fundamental software element that has a state and a set of behaviors. Ex: bank account or student.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2013.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
EECS 110: Lec 14: Classes and Objects
EECS 110: Lec 15: Classes and Objects (2)
EECS 110: Lec 17: Review for the Final Exam
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
Introduction to Object-Oriented Programming (OOP)
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2017.
Introduction to Object-Oriented Programming (OOP) II
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2018.
Introduction to Object-Oriented Programming (OOP)

Sharing, mutability, and immutability
EECS 110: Lec 14: Classes and Objects
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2016.
EECS 110: Lec 15: Classes and Objects (2)
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Introduction to Object-Oriented Programming (OOP)
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2019.
Introduction to Object-Oriented Programming (OOP) II
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Presentation transcript:

Lists, Tuples, Dictionaries, … + lots of computer work for the programmer's work! T = {'abe' :['homer','herb'], 'jackie':['marge','patty','selma'], 'homer' :['hugo','bart','lisa','maggie'], 'marge' :['hugo','bart','lisa','maggie']} e.g., the Simpson's dictionary…

Lists, Tuples, Dictionaries, … + - lots of computer work for the programmer's work! no options as to data organization… limited to square-bracket naming, e.g., fairly generic capabilities, e.g., len, print A[i] list int float str A A[0]A[1] A[2] A = [ 42, 3.1, '!'] '!' T = {'abe' :['homer','herb'], …} T.getChildren('abe') NOT POSSIBLE! have to use T['abe']

Lists, Tuples, Dictionaries, … + - lots of computer work for little programmer work! no options as to data organization… limited to square-bracket naming, e.g., fairly generic capabilities, e.g., len, print A[i] Classes and Objects take care of all 3 drawbacks... list int float str A A[0]A[1] A[2] A = [ 42, 3.1, '!'] '!'

Classes & Objects An object-oriented programming language allows you to build your own customized types of variables. (1) A class is a type of variable. (2) An object is one such variable. There will typically be MANY objects of a single class.

Examples… Do reference libraries have library references? Python's class libraries… Graphics libraries

Using objects and classes: >>> z = 3 + 4j >>> dir(z) all of the data members and methods of the complex class (and thus the object z !) >>> z.imag 4.0 >>> z.conjugate() 3-4j A particularly complex example… a data member of all objects of class complex its value for this object, z its return value for this object, z a method (function) within all objects of class complex

Objects An object is a data structure (like a list), except (1) Its data elements have names chosen by the programmer. (2) Data elements are chosen & organized by the programmer (3) An object can have behaviors built-in by the programmer. usually called "methods" instead of functions

Date this is an object of type Date >>> d = Date(1,1,2008) >>> d 1/1/2008 This is a class. It is a user-defined datatype that we can define ourselves this is a CONSTRUCTOR … What does it do? the repr esentation of a particular object of type Date >>> d.isLeapYear() True >>> d2 = Date(12,31,2007) >>> d2 12/31/2007 >>> d2.isLeapYear() False the isLeapYear method returns True or False. How does it know what year to check? How does it know to return False, instead of True in this case ?? Another object of type Date - again, via the constructor.

class Date: """ a blueprint (class) for objects that represent calendar days """ def __init__( self, mo, dy, yr ): """ the Date constructor """ self.month = mo self.day = dy self.year = yr def __repr__( self ): """ used for printing Dates """ s = "%02d/%02d/%04d" % (self.month, self.day, self.year) return s def isLeapYear( self ): """ anyone know the rule? """ The Date class Why is everyone so far away?!

>>> d = Date(1,1,2008) >>> d 1/1/2008 self These methods need access to the object that calls them >>> d.isLeapYear() True >>> d2 = Date(12,31,2007) >>> d2 12/31/2007 >>> d2.isLeapYear() False is the specific OBJECT THAT invokes A METHOD These methods need access to the object that calls them

a Leap of faith…. class Date: def __init__( self, mo, dy, yr ): (constructor) def __repr__(self): (for printing) def isLeapYear( self ): """ here it is """ if self.year % 400 == 0: return True if self.year % 100 == 0: return False if self.year % 4 == 0: return True return False John Herschel How about a 4000-year rule?

Date >>> d = Date(1,1,2008) >>> d 1/1/2008 always created with the CONSTRUCTOR … >>> d.yesterday() >>> d 12/31/2007 >>> d.subNDays(35) >>> d the yesterday method returns nothing at all. Is it doing anything? Some methods return a value; others change the object that call it! d has changed!

Date id s >>> d = Date(11,10,2008) >>> d 11/10/2008 >>> d2 = Date(11,11,2008) >>> d2 11/11/2008 this initializes a different Date! What date is on your id ? What id is on your Date? >>> d == d2 ? >>> d2.yesterday() >>> d == d2 ?

Double Date >>> d2 = d >>> d 11/10/2008 >>> d.addNDays(36) >>> d2 ? >>> d2 = d.copy() >>> d2 == d ? >>> d.equals(d2) ? Excuse me -- id s please! How many Dates are here?

class Date: def __init__( self, mo, dy, yr ): def __repr__(self): def isLeapYear(self): def copy(self): """ returns a DIFFERENT object w/SAME date! """ def equals(self, d2): """ returns True if they represent the same date; False otherwise """ More Date How many Dates are here? Would two be self ish?

class Date: def isBefore(self, d2): """ if self is before d2, this should return True; else False """ if self.yr < d2.yr: return True if self.mo < d2.mo: return True if self.dy < d2.dy: return True return False def tomorrow(self): """ moves the date that calls it ahead 1 day """ DIM = [0,31,28,31,30,31,30,31,31,30,31,30,31] This method is WRONG! Write this tomorrow method. It does not return anything. It just CHANGES the date object that calls it.

class Date: def isBefore(self, d2): """ if self is before d2, this should return True; else False """ if self.year < d2.year: return True if self.month < d2.month: return True if self.day < d2.day: return True return False What's wrong?

class Date: def tomorrow(self): """ moves the date that calls it ahead 1 day """ DIM = [0,31,28,31,30,31,30,31,31,30,31,30,31]