Lilian Blot TOWARDS MORE ADVANCED CONCEPTS & OBJECT ORIENTED PROGRAMMING Building Data Structure Autumn 2014 TPOP 1.

Slides:



Advertisements
Similar presentations
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Advertisements

Lilian Blot Recursion Autumn 2012 TPOP 1. Lilian Blot Recursion Autumn 2012 TPOP 2.
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
CIT 590 Intro to Programming Classes. Schedule change The upcoming HW (HW6) is your last major Python HW. It will involve object oriented programming.
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.
Search in Python Chapter 3.
Lilian Blot TPOP Practical Assignment Optional, but you are strongly encourage to do it, especially if you are new to programming Available on Module webpage.
OBJECT ORIENTED PROGRAMMING (OOP) IN PYTHON David Moodie.
Lilian Blot BUILDING CLASSES Java Programming Spring 2014 TPOP 1.
Lilian Blot LINEAR DATA STRUCTURE LINKED LIST Abstract Data Structure Autumn 2014 TPOP 1.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
Lilian Blot INHERITANCE Object Oriented Programming Spring 2014 TPOP 1.
Lilian Blot Announcements The TPOP problem class this afternoon:  group 1 should come at 3.30pm and group 2 at 4pm. Teaching Evaluation Form  week 9.
Classes 2 COMPSCI 105 S Principles of Computer Science.
Classes and Objects, Part 1 Victor Norman CS104. Reading Quiz, Q1 A class definition define these two elements. A. attributes and functions B. attributes.
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Python Crash Course Classes 3 rd year Bachelors V1.0 dd Hour 7.
Object Oriented Software Development
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
Classes 3 COMPSCI 105 S Principles of Computer Science.
Chapter 10 Classes. Review of basic OOP concepts  Objects: comprised of associated DATA and ACTIONS (methods) which manipulate that data.  Instance:
Methods in Computational Linguistics II Queens College Lecture 7: Structuring Things.
CIT 590 Intro to Programming Style Classes. Remember to finish up findAllCISCourses.py.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
11/27/07. >>> Overview * objects * class * self * in-object methods * nice printing * privacy * property * static vs. dynamic * inheritance.
CSC1018F: Object Orientation, Exceptions and File Handling Diving into Python Ch. 5&6 Think Like a Comp. Scientist Ch
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Python Functions.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
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.
1 Using Templates COSC 1567 C++ Programming Lecture 10.
Object Oriented Programing (OOP)
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Classes COMPSCI 105 SS 2015 Principles of Computer Science.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
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:
Chapter 17 Q and A Victor Norman, et al. CS104. What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Classes (Part 1) Lecture 3
Definition and Application of Binary Trees
Object Oriented Programming
COMPSCI 107 Computer Science Fundamentals
CSSE 120—Rose Hulman Institute of Technology
CS-104 Final Exam Review Victor Norman.
Functions CIS 40 – Introduction to Programming in Python
Introduction to Object-Oriented Programming (OOP)
Python Classes By Craig Pennell.
Classes In C#.
Introduction to Object-Oriented Programming (OOP)
CS2011 Introduction to Programming I Objects and Classes
A Level Computer Science Topic 6: Introducing OOP
Programming For Big Data
Introduction to Object-Oriented Programming (OOP)
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Object-Oriented Design AND CLASS PROPERTIES
Classes and Objects Systems Programming.
Presentation transcript:

Lilian Blot TOWARDS MORE ADVANCED CONCEPTS & OBJECT ORIENTED PROGRAMMING Building Data Structure Autumn 2014 TPOP 1

Lilian Blot Overview Data representation Building new data structures Examples Autumn 2014 TPOP 2

Lilian Blot Data Representation Example: Address  house number  street name  city  county  postcode Person  Surname  First name  NI  Address  Phone numbers Autumn 2014 TPOP 3

Lilian Blot Using what we know so far Lists  Address [house_number, street_name, city, county, postcode]  Person [surname, first_name, NI, address, phone_numbers] String  Address “house_number, street_name, city, county, postcode”  Person “surname, first_name, NI, address, phone_numbers” Autumn 2014 TPOP 4

Lilian Blot Using what we know so far Dictionary  Address {house_number:15, street_name : ‘Lili Street’, city: ‘York’, county: ‘Yorkshire’, postcode: ‘YO5-5GH’}  Person {surname: ‘Blot’, first_name: ‘Lilian’, NI: ‘OO7’, address:…, phone_numbers: {…} } Autumn 2014 TPOP 5

Lilian Blot Creating our Own Data Structure Keyword class General Form: Example Autumn 2014 TPOP 6 class DataStructureName: ``` doc-string ``` pass Code class Address: ``` doc-string ``` pass Code

Lilian Blot Creating our Own Data Structure Using the new Data structure : Autumn 2014 TPOP 7 >>> addr = Address() >>> addr.street = ‘Lili Street’ >>> print addr.street Lili Street Code

Lilian Blot What are Classes? Classes are composed from structural and behavioural constituents. Attributes (member variables or instance variables) enable a class instance to maintain state.  a.k.a properties, fields, data members Methods, enable the behaviour of class instances. Classes define the type of their instances Autumn 2014 TPOP 8

Lilian Blot Creating our Own Data Structure Keyword class Key method __init__(…) Key parameter self isinstance(object,Class) Autumn 2014 TPOP 9 class DataStructureName: ``` doc-string ``` def __init__(self, ): Code

Lilian Blot Creating our Own Data Structure Code examples:  First Attempt: Address Autumn 2014 TPOP 10

Lilian Blot Address Creating our Own Data Structure Code examples:  First Attempt: Address Autumn 2014 TPOP 11 … 35 … … self number streetcitycounty postcode house_number house_street house_city house_county house_postcode my_address

Lilian Blot Creating our Own Data Structure Code examples:  First Attempt: Address  Second Attempt: Address Autumn 2014 TPOP 12

Lilian Blot Address Creating our Own Data Structure Code examples:  First Attempt: Address  Second Attempt: Address Autumn 2014 TPOP 13 … 35 … … number streetcitycounty postcode my_address 555

Lilian Blot Creating our Own Data Structure The objects we define are MUTABLE Autumn 2014 TPOP 14

Lilian Blot Terminology Class Address is the definition of an object my_address is an instance of Address number, city, postcode,… are attributes __init__ is a constructor __str__ is a method Autumn 2014 TPOP 15

Lilian Blot Creating our Own Data Structure Code examples:  First Attempt: Address  Second Attempt: Address  Person Autumn 2014 TPOP 16

Lilian Blot __repr__ Method Definition: Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method. Autumn 2014 TPOP 17

Lilian Blot __str__ Method Definition : Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''. Autumn 2014 TPOP 18

Lilian Blot __str__ versus __repr__ Every class should implement the __repr__ method __str__ is optional. If not implemented, __repr__ will be used instead __repr__: representation of python object usually eval will convert it back to that object __str__: is whatever you think is that object in text form Autumn 2014 TPOP 19

Lilian Blot Summary We have seen how to build our own data structure Our data structure can be embedded into another data structure User defined Objects are MUTABLE Autumn 2014 TPOP 20

Lilian Blot Exercises Client requirements Build a genealogy tree  First Name, Surname, Maiden name  Date of Birth  Place of Birth (city/town/village and country)  Current Address Your Job: Build an appropriate data structure Autumn 2014 TPOP 21

Lilian Blot Nested Structures Autumn 2014 TPOP 22

Lilian Blot Exercises Food for thought how to save/read the data structure into/from a text file how to search for/find a person in the tree how to add/remove a person from the tree could you implement your solution? Autumn 2014 TPOP 23