Programming for Engineers in Python

Similar documents
Chapter 6 Introduction to Defining Classes

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

CS108 Lecture 16: User Defined Classes. Overview/Questions

Data Structures (list, dictionary, tuples, sets, strings)

Object Model Comparisons

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

The Dynamic Typing Interlude

CS24 Week 3 Lecture 1

CSCE 156 Computer Science II

Objects and Classes. Chapter 8

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture 6 Introduction to Objects and Classes

Frequently Asked Questions

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

CS313D: ADVANCED PROGRAMMING LANGUAGE

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Cosmology with python: Beginner to Advanced in one week. Tiago Batalha de Castro

Lesson 6 Introduction to Object-Oriented Programming

INF 102 CONCEPTS OF PROG. LANGS FUNCTIONAL COMPOSITION. Instructors: James Jones Copyright Instructors.

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Play with Python: An intro to Data Science

15 212: Principles of Programming. Some Notes on Continuations

CSE 303: Concepts and Tools for Software Development

CT 229 Arrays in Java

Lecture 04 FUNCTIONS AND ARRAYS

CMSC 132: Object-Oriented Programming II

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

CMSC 132: Object-Oriented Programming II

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 10

CS313D: ADVANCED PROGRAMMING LANGUAGE

Announcements. Homework 1 due Monday 10/12 (today) Homework 2 released next Monday 10/19 is due 11/2

Launchpad Lecture -10

Example: Fibonacci Numbers

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

Object-Oriented Programming Concepts

Lecture 15: Even more pointer stuff Virtual function table

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

1. Write two major differences between Object-oriented programming and procedural programming?

CSCA08 Winter 2018 Week 9:Object-Oriented Programming. Marzieh Ahmadzadeh,, Brian Harrington University of Toronto Scarborough

C++ Classes & Object Oriented Programming

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

Introduction to Programming Using Python Lecture 4. Dr. Zhang COSC 1437 Fall, 2018 October 11, 2018

Pointers and Memory 1

Lecture 14: more class, C++ streams

CS61A Lecture 20 Object Oriented Programming: Implementation. Jom Magrotker UC Berkeley EECS July 23, 2012

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

DECOMPOSITION, ABSTRACTION, FUNCTIONS

CSCC24 Functional Programming Scheme Part 2

61A Lecture 16. Wednesday, October 5

Glossary. For Introduction to Programming Using Python By Y. Daniel Liang

About this exam review

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

Recursion 1. Recursion is the process of defining something in terms of itself.

Object-Oriented Programming

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Object-Oriented Programming

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

CSE 341, Autumn 2015, Ruby Introduction Summary

Mozart System Limitations for Version (draft)

CSC102 INTRO TO PROGRAMMING WITH PYTHON REVIEW MICHAEL GROSSBERG

VARIABLES AND TYPES CITS1001

CS558 Programming Languages. Winter 2013 Lecture 3

Functions and Decomposition

Run-time Environments - 3

: Intro Programming for Scientists and Engineers Final Exam

Object-Oriented Programming (OOP) Fundamental Principles of OOP

CompuScholar, Inc. Alignment to Nevada "Computer Science" Course Standards

User Defined Types. Babes-Bolyai University Lecture 06. Lect Phd. Arthur Molnar. User defined types. Python scope and namespace

Structured Programming

CS304 Object Oriented Programming Final Term

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes

Week 8 Lecture: Programming

Object Oriented Programming

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Garbage Collection (1)

CS 170, Section /3/2009 CS170, Section 000, Fall

1: Introduction to Object (1)

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Assignment II: Calculator Brain

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Recall. Key terms. Review. Encapsulation (by getters, setters, properties) OOP Features. CSC148 Intro. to Computer Science

Programming, numerics and optimization

G Programming Languages - Fall 2012

Weeks 6&7: Procedures and Parameter Passing

PREPARING FOR THE FINAL EXAM

Homework notes. Homework 2 grades posted on canvas. Homework 3 due tomorrow. Homework 4 posted on canvas. Due Tuesday, Oct. 3

Short Notes of CS201

CSE 100: C++ TEMPLATES AND ITERATORS

Object Oriented Programming #10

Software Architecture (Lesson 2) Object-Oriented Paradigm (1)

The Pyth Language. Administrivia

Transcription:

Programming for Engineers in Python Lecture 5: Object Oriented Programming Autumn 2011-12 1

Lecture 4 Highlights Tuples, Dictionaries Sorting Lists Modular programming Data analysis: text categorization Collect data Most frequent words How it is really done 2

Today Quick review on functions Object Oriented Programming (partially based on chapters 15-18 in the book Think Python, available in the site) 3

Functions (Quick Overview) 4

How to Calculate 5! + 3! + 6!? 5

Problems Same code duplicated 3 times More elements more code duplicates The program becomes longer and more complicated May cause bugs, cut & paste errors Hard to understand what was the writer s intension What would we like? Write the code once! Use it several times with different arguments 6

Solution - Functions 7

Functions A group of declarations and statements that is assigned a name Effectively, a named statement block Usually has a value A sub-program Inside a fucntion we can call other functions Which can themselves use other functions, and so on Function output: Return value to the calling function If no value is to be returned, None is returned 8

What are They Good For? Generalize a repeated set of instructions We don t have to keep writing the same thing over and over Solve bugs once They can break your problem down into smaller sub-tasks Easier to solve complex problems They make a program much easier to read and maintain Abstraction we don t have to know how a function is implemented to use it 9

In Short Why do we need functions? Code reusability Modularity Abstraction 10

Examples 11

Passing Arguments to Functions When a function is called, arguments values are attached to function s formal parameters by order, and an assignment occurs before execution Values are copied to formal parameters 12

Passing Arguments to Functions A reference is passed by reference Example: lists This explains why we can change an array s content within a function a 4 5 6 7 8 9 13

Example 14

Pyhton Memory Model Stack: local variables and arguments, every function uses a certain part of the stack Stack variables disappear when scope ends Heap: global variables and objects, scope independent Garbage Collector Partial description 15

How to Change a Variable via Functions? So how can a method change an outer variable? By its return value By accessing heap-based memory (e.g., lists) 16

A Car How would you represent a car? Parts / features: 4 wheels, steering wheel, horn, color, Functionality: drive, turn left, honk, repaint, In Python??? 17

Lets Start Simpler How would you represent a library? Container of books Each book: Title Author ISBN number Number of pages Publisher 18

Printing a Book 19

What Would We Want? To group the definition of several variables under a single name a new type Somewhat similar to functions Using the new type as if it is part of the language: Define variables, perform operation 20

Object-Oriented Programming (OOP) Represent problem-domain entities using a computer language When building a software in a specific domain, describe the different components of the domain as types and variables Thus we can take another step up in abstraction 21

Class as a Blueprint A class is a blueprint of objects 22

Class as a Blueprint A class is a blueprint of objects 23

Classes as Data Types Classes define types that are a composition of other types and have unique functionality An instance of a class is named an object Every instance may contain: Constructors Attributes (data members / fields) Methods 24

Car Example Members: 4 wheels, steering wheel, horn, color, Every car instance has its own Methods: drive, turn left, honk, repaint, Constructors: by color (only), by 4 wheels, engine, 25

How to Represent a Point in 2D? Alternatives: Two variables x,y Elements in a list / tuple A new data type Creating a new type is a (little) more complicated, but has its advantages (to be apparent soon) class a user defined type How to represent a point? 26

Creating a new Point (instantiation) blank is an instance of class Point 27

But Where is the Point? x does not exist in blank (nor y) We want to be able to access x,y via Point instance 28

Constructors A special function, defined in the class s code that produces instances Invoked automatically when an object is instantiated Given as input whatever is required to produce an instance What would a Point s constructor accept as inputs? 29

Constructors More Technically Defined within the class s scope Always named init Short for initialization 2 underscores init 2 underscores The first parameter would be self To be explained later on Note that when invoking (calling) the constructor, self is not passed as an argument It is common for the parameters of init to have the same names as the attributes 30

Attributes p1 Point x 3.0 y 4.0 The variable p1 refers to a Point object p1.x means Go to object p1 refers to and get the value of x There is no conflict between a variable x and the attribute x 31

Instances and Functions Objects can be passed as arguments to functions Objects are mutable 32

Copying Objects is operator indicates that p1 and p2 are not the same object, the default behavior of the == operator is the same as the is operator 33

Instances as Return Values 34

A Circle How would you represent a circle object? Attributes? Constructor? 35

Shallow / Deep Copy 36

Shallow / Deep Copy (Cont.) 37

Object Oriented Programming Programs are made of object definitions and function definitions, and most of the computation is expected in terms of operations on objects Each object definition corresponds to some object or concept in the real world, and the functions that operate on that object correspond to the ways real-world objects interact 38

Methods Method is a function that is associated with a particular class Examples in strings, lists, dictionaries, tuples Difference between methods and functions: Methods are defined inside a class definition The syntax for invoking a method A method is called through an instance 39

Example: Distance Between Two Points (function) 40

Example: Distance Between Two Points (method) 41

Example: In Circle 42

Next week: defining a new type that behaves as part of the language More OOP Implementation of Rational Numbers 43