Python Programming Chapter 13: Classes and Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.

Slides:



Advertisements
Similar presentations
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use.
Lilian Blot PART III: ITERATIONS Core Elements Autumn 2012 TPOP 1.
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Python Objects and Classes
Types of selection structures
Bottoms Up Factoring. Start with the X-box 3-9 Product Sum
X-box Factoring. X- Box 3-9 Product Sum Factor the x-box way Example: Factor 3x 2 -13x (3)(-10)= x 2x 3x 2 x-5 3x +2.
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Method Parameters and Overloading. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
Python Programming Chapter 5: Fruitful Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Python Programming Chapter 9: Tuples Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Python Programming Chapter 1: The way of the program Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Python Programming Chapter 12: Classes and Objects Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2010/2011.
Chapter 2: Algorithm Discovery and Design
Chapter 4: Conditionals and Recursion
Main task -write me a program
Chapter 2: Algorithm Discovery and Design
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Adapted from slides by Marie desJardins
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Python Programming Chapter 14: Classes and Methods Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Invitation to Computer Science, Java Version, Second Edition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
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)
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Python Mini-Course University of Oklahoma Department of Psychology Lesson 27 Classes and Functions 6/17/09 Python Mini-Course: Lesson 27 1.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CS 101 – Oct. 7 Solving simple problems: create algorithm Structure of solution –Sequence of steps (1,2,3….) –Sometimes we need to make a choice –Sometimes.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
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.
Introduction to Computer Science / Procedural – 67130
Topics Introduction to Repetition Structures
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
PROBLEM SOLVING CSC 111.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Computing Fundamentals
CISC101 Reminders All assignments are now posted.
CS149D Elements of Computer Science
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
COMPUTER PROGRAMMING SKILLS
def-ining a function A function as an execution control structure
REPETITION Why Repetition?
Presentation transcript:

Python Programming Chapter 13: Classes and Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012

Python Programming Chapter 13- Saad Bani Mohammad2 Time As another example of a user-defined type, we'll define a class called Time that records the time of day. The class definition looks like this: class Time: pass We can create a new Time object and assign attributes for hours, minutes, and seconds: time = Time() time.hours = 11 time.minutes = 59 time.seconds = 30 The state diagram for the Time object looks like this: As an exercise, write a function printTime that takes a Time object as an argument and prints it in the form hours:minutes:seconds. As a second exercise, write a boolean function after that takes two Time objects, t1 and t2, as arguments, and returns true (1) if t1 follows t2 chronologically ( زمنياً ) and false (0) otherwise.

Python Programming Chapter 13- Saad Bani Mohammad3 Pure Functions In the next few sections, we'll write two versions of a function called addTime, which calculates the sum of two Times. They will demonstrate two kinds of functions: pure functions and modifiers. The following is a rough version of addTime: def addTime(t1, t2): sum = Time() sum.hours = t1.hours + t2.hours sum.minutes = t1.minutes + t2.minutes sum.seconds = t1.seconds + t2.seconds return sum The function creates a new Time object, initializes its attributes, and returns a reference to the new object. This is called a pure function because it does not modify any of the objects passed to it as parameters and it has no side effects, such as displaying a value or getting user input.

Python Programming Chapter 13- Saad Bani Mohammad4 Pure Functions (Cont…) Here is an example of how to use this function. We'll create two Time objects: currentTime, which contains the current time; and breadTime, which contains the amount of time it takes for a breadmaker to make bread. Then we'll use addTime to figure out when the bread will be done. If you haven't finished writing printTime yet, take a look ahead to the next slide before you try this: >>> currentTime = Time() >>> currentTime.hours = 9 >>> currentTime.minutes = 14 >>> currentTime.seconds = 30 >>> breadTime = Time() >>> breadTime.hours = 3 >>> breadTime.minutes = 35 >>> breadTime.seconds = 0 >>> doneTime = addTime(currentTime, breadTime) >>> printTime(doneTime) The output of this program is 12:49:30, which is correct. On the other hand, there are cases where the result is not correct. Can you think of one? The problem is that this function does not deal with cases where the number of seconds or minutes adds up to more than sixty. When that happens, we have to "carry" the extra seconds into the minutes column or the extra minutes into the hours column.

Python Programming Chapter 13- Saad Bani Mohammad5 printTime In the previous slide, we defined a class named Time and you wrote a function named printTime, which should have looked something like this: class Time: pass def printTime(time): print str(time.hours)+":+str(time.minutes)+":" +str(time.seconds) To call this function, we passed a Time object as a parameter: >>> currentTime = Time() >>> currentTime.hours = 9 >>> currentTime.minutes = 14 >>> currentTime.seconds = 30 >>> printTime(currentTime)

Python Programming Chapter 13- Saad Bani Mohammad6 Pure Functions (Cont…) Here's a second corrected version of the function: def addTime(t1, t2): sum = Time() sum.hours = t1.hours + t2.hours sum.minutes = t1.minutes + t2.minutes sum.seconds = t1.seconds + t2.seconds if sum.seconds >= 60: sum.seconds = sum.seconds - 60 sum.minutes = sum.minutes + 1 if sum.minutes >= 60: sum.minutes = sum.minutes - 60 sum.hours = sum.hours + 1 return sum

Python Programming Chapter 13- Saad Bani Mohammad7 Modifiers There are times when it is useful for a function to modify one or more of the objects it gets as parameters. Usually, the caller keeps a reference to the objects it passes, so any changes the function makes are visible to the caller. Functions that work this way are called modifiers. increment, which adds a given number of seconds to a Time object, would be written most naturally as a modifier. A rough draft of the function looks like this: def increment(time, seconds): time.seconds = time.seconds + seconds if time.seconds >= 60: time.seconds = time.seconds - 60 time.minutes = time.minutes + 1 if time.minutes >= 60: time.minutes = time.minutes - 60 time.hours = time.hours + 1 The first line performs the basic operation; the remainder deals with the special cases we saw before.

Python Programming Chapter 13- Saad Bani Mohammad8 Modifiers (Cont...) Is this function correct? What happens if the parameter seconds is much greater than sixty? In that case, it is not enough to carry once; we have to keep doing it until seconds is less than sixty. One solution is to replace the if statements with while statements: def increment(time, seconds): time.seconds = time.seconds + seconds while time.seconds >= 60: time.seconds = time.seconds - 60 time.minutes = time.minutes + 1 while time.minutes >= 60: time.minutes = time.minutes - 60 time.hours = time.hours + 1 This function is now correct, but it is not the most efficient solution.