Python Mini-Course University of Oklahoma Department of Psychology Lesson 27 Classes and Functions 6/17/09 Python Mini-Course: Lesson 27 1.

Slides:



Advertisements
Similar presentations
Exception Handling Genome 559. Review - classes 1) Class constructors - class myClass: def __init__(self, arg1, arg2): self.var1 = arg1 self.var2 = arg2.
Advertisements

Python Programming Chapter 13: Classes and Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Solving Equations A Solution
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 4 Beginning Functions 4/5/09 Python Mini-Course: Day 1 - Lesson 4 1.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 8 Fruitful Functions 05/02/09 Python Mini-Course: Day 2 - Lesson 8 1.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 26 Classes and Objects 6/16/09 Python Mini-Course: Lesson 26 1.
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Final Project of Information Retrieval and Extraction by d 吳蕙如.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.
Introduction Using the Pythagorean Theorem to solve problems provides a familiar example of a relationship between variables that involve radicals (or.
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Python quick start guide
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Exceptions COMPSCI 105 S Principles of Computer Science.
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 2 Fundamentals of Programming Languages 4/5/09 Python Mini-Course: Day.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 19 Handling Exceptions 6/09/09 Python Mini-Course: Lesson 19 1.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Python Programming Using Variables and input. Objectives We’re learning to make use of if statements to enable code to ask questions. Outcomes Build an.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 9 Iteration: Recursion 5/02/09 Python Mini-Course: Day 3 - Lesson 9 1.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 13 Case study: Word play 05/02/09 Python Mini-Course: Day 4 – Lesson.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Make a blank window This is a starter activity and should take 5 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.In script mode create a file.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 5 Function Interfaces 4/18/09 Python Mini-Course: Day 2 - Lesson 5 1.
Fill the screen challenge! This is a starter activity and should take 3 minutes [ slide 1 ] 1.Log in to your computer 2.Open IDLE 3.In interactive mode,
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures.
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
Solve the following system using the elimination method.
Lesson 1-8 Solving Addition and Subtraction Equations.
Python Let’s get started!.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
Functions CMSC 201 – Lab 5. Overview Objectives for today's lab:  Practice breaking down a program into multiple functions  Practice writing function.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Domain Testing Functional testing which tests the application by giving inputs and evaluating its appropriate outputs. system does not accept invalid and.
Python Let’s get started!.
Introduction to Python
Topics Introduction to Repetition Structures
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline Test-Driving the Microwave Oven Application Designing.
Lesson 04: Conditionals Class Chat: Attendance: Participation
Objective Solve equations in one variable that contain variable terms on both sides.
Coding Concepts (Basics)
Patterns to KNOW.
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Python 19 Mr. Husch.
Advanced Python Concepts: Exceptions
Objective Solve equations in one variable that contain variable terms on both sides.
Advanced Python Concepts: Exceptions
By Ryan Christen Errors and Exceptions.
Variables and Equations
Python 19 Mr. Husch.
Understanding and Preventing Buffer Overflow Attacks in Unix
Global Variables Created by assignment statement placed at beginning of program and outside all functions Can be accessed by any statement in the program.
If there is any case in which true premises lead to a false conclusion, the argument is invalid. Therefore this argument is INVALID.
If there is any case in which true premises lead to a false conclusion, the argument is invalid. Therefore this argument is INVALID.
Python Reserved Words Poster
Presentation transcript:

Python Mini-Course University of Oklahoma Department of Psychology Lesson 27 Classes and Functions 6/17/09 Python Mini-Course: Lesson 27 1

Lesson objectives 1. Create functions that operate on objects 2. Differentiate between pure functions and modifiers 3. Use argument checking in functions 6/17/09 Python Mini-Course: Lesson 27 2

The Time class Throughout this lesson, we will be using the custom Time class See time.py In IDLE, run the time.py script to define the Time class and functions We can now use these at the command line 6/17/09 Python Mini-Course: Lesson 27 3

Creating time objects time1 = Time(1,0,0) print_time(time1) time2 = Time(0,30,5) print_time(time2) 6/17/09 Python Mini-Course: Lesson 27 4

Passing by reference A variable name is a pointer to the memory location where data are stored When you pass an argument to a function, you are passing in the pointer If the function changes the value of an argument, it changes the data in the memory location This is then seen outside the function 6/17/09 Python Mini-Course: Lesson 27 5

Modifier functions Modifiers can (and usually do) change the value of arguments that are passed into the function See the increment_time() function 6/17/09 Python Mini-Course: Lesson 27 6

Modifier functions: Example increment_time(time1, time2) print_time(time1) print_time(time2) 6/17/09 Python Mini-Course: Lesson 27 7

Pure functions Pure functions do not change the value of any arguments See the add_time() function 6/17/09 Python Mini-Course: Lesson 27 8

Pure functions: Example time3 = add_time(time1, time2) print_time(time1) print_time(time2) print_time(time3) 6/17/09 Python Mini-Course: Lesson 27 9

Argument checking Try this: time4 = Time(0,90,0) increment_time(time4, time1) increment_time(time4, 30) 6/17/09 Python Mini-Course: Lesson 27 10

Argument checking Two problems: 1. The Time class allows invalid times (>60 minutes or seconds) 2. The increment_time() function is designed to accept two time object, but the wrong data types were passed to it 6/17/09 Python Mini-Course: Lesson 27 11

Argument checking Solutions: 1. Check the formatting of the time 2. Check the type for the input argument 6/17/09 Python Mini-Course: Lesson 27 12

The valid_time() function def valid_time(t): validity = True # All values must be at least zero if t.hours < 0 or t.minutes < 0 or t.seconds < 0: validity = False # Minutes and seconds must be base 60 if t.minutes >= 60 or t.seconds >= 60: validity = False return validity 6/17/09 Python Mini-Course: Lesson 27 13

Adding argument checking def increment_time(t1, t2): """ Given two Time objects t1 and t2, add t2 to t1 """ # Check the input arguments if type(t1) != Time or type(t2) != Time: raise AttributeError, \ 'invalid argument passed to increment_time' if not valid_time(t1) or not valid_time(t2): raise ValueError, 'invalid Time object in increment_time' # Add the times t1.hour += t2.hour t1.minute += t2.minute t1.second += t2.second 6/17/09 Python Mini-Course: Lesson 27 14