EN.540.635 - Software Carpentry Python – A Crash Course Function, Classes, and Graphing.

Slides:



Advertisements
Similar presentations
When is Orientated Programming NOT? Mike Fitzpatrick.
Advertisements

Chapter 13 – Introduction to Classes
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
C++ fundamentals.
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
Functions & Objects IDIA 618 Spring 2012 Bridget M. Blodgett.
CIT 590 Intro to Programming Style Classes. Remember to finish up findAllCISCourses.py.
An Object-Oriented Approach to Programming Logic and Design
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
C++ Programming Basic Learning Prepared By The Smartpath Information systems
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Programming in Java CSCI-2220 Object Oriented Programming.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Object Oriented Programing (OOP)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Learning OOP in PHP. What is OOP? OOP stands for Object Oriented Programming. OOP is a programming paradigm wherein you create “objects” to work with.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Chapter 7 Continued Arrays & Strings. Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Object-Oriented Programming
Chapter 5: Enhancing Classes
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Programming paradigms
Catapult Python Programming Session 4
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
Concepts of Object Oriented Programming
Programming Logic and Design Seventh Edition
Types for Programs and Proofs
The Object-Oriented Thought Process Chapter 1
IF statements.
11.1 The Concept of Abstraction
About the Presentations
Functions CIS 40 – Introduction to Programming in Python
Object-Orientated Programming
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Templates.
Object Oriented Programming
To Get Started Paper sheet
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Chapter 10 Thinking in Objects
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Functions, Procedures, and Abstraction
Teach A-Level Computer Science: Object-Oriented Programming in Python
Objective of This Course
Advanced Programming Behnam Hatami Fall 2017.
The Scientific Method.
Focus of the Course Object-Oriented Software Development
Welcome to AP Computer Science A!
Welcome to AP Computer Science A!
Object-Oriented Programming
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Copy into binder – Class notes The Scientific Method
Functions, Procedures, and Abstraction
Presentation transcript:

EN.540.635 - Software Carpentry Python – A Crash Course Function, Classes, and Graphing

What’s wrong here?

What’s wrong here?

What’s wrong here?

In General If some logic is repeated throughout your code, use a function! Why? With less repeated code, you can minimize copy-paste errors. Further, if the logic changes, you need only change one aspect of the code, not N aspects of it (where N is each copied instance) Exception: If the logic/action is repeated, but an alternative function already exists. That is, don’t re-invent the wheel! If some logic is only run once, then it makes no difference to have or not have a function, and can in fact be detrimental to reading the code if a function is made and displaced from said segment of code.

Object Oriented Programming (OOP) An Idea From the 1950s to the 1970s, an idea was nurtured. Object Oriented Programming (OOP) Simply put, it was a method of programming in which objects could be defined that would hold data and methods. Data can be read as-is. That is, the object could hold variables that contain information. Methods can be read as “code”. That is, the object could hold functions that could then be run.

What are the basics of OOP? Abstraction Polymorphism Encapsulation Inheritance

What are the basics of OOP? Abstraction Think of a black box. Abstraction allows the user to only have to deal with the basics. Ex. The Python Imaging Library (PIL) allowed you to import an image just by running a = Image.load(“cat.jpeg”). How was the file read in? How did it know it was a .jpeg instead of a .png? Further, the object a now has methods such as a.getpixel((x, y))… how does it work?

What are the basics of OOP? Polymorphism The idea that your code should be a “renaissance code” of sorts Think back to the Python Imaging Library (again…) When opening a file, all you called was a = Image.load(“another_cat.png”) Due to polymorphism, this method works for .jpeg, .png, and other file types!

What are the basics of OOP? Encapsulation This is an idea that you can allow anyone to use your code, but you idiot-proof it first. Encapsulation means that the internal data and workings of your object are kept internal. No outsider can use it. In exchange, encapsulation requires you to make methods/functions that allows a user to access/change data they need. This is useful for many reasons, but I’ll list 2: In security contexts, you don’t want someone being able to read sensitive data. In regular use, your functions may assume data of a certain type is being passed, such as a float. By specifying a function such as a.set_x(4), you can force it to be a float, even when the user specifies a string.

What are the basics of OOP? Inheritance Imagine you have an object that is 500 lines of code. Imagine you need 20 instances of this object with varying initial parameters, but all the same functions. Would you rather: Write the same 500 lines of code 20 times, changing the default values Initialize 20 objects from the default 500 lines of code, and change the values If you chose (b), you are thinking with inheritance and objects The reason (a) is bad is because if you need to change a single function, you need to do it 20 times now. Whereas in (b), you only need to do so once!

What are the basics of OOP? Abstraction The logic is hidden, but it works. Polymorphism It works robustly too! Encapsulation It’s easy to move around, and hard to break Inheritance I can make many of them, and adapt off of them!

Example of OOP

Classes In Python, a class is how we can make an object We will go over this together now in lecture Make a blank class Make variables and functions Initialize objects and mess around __init__, __str__ and __repr__, __del__, __lt__, __eq__, … @staticmethod, @classmethod __str__  for End Users, to be pretty output __repr__  for programmers, to be used for debugging purposes (as much info as possible)