Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
Advertisements

Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Object, Object- Relational, and XML: Concepts, Models, Languages,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 2: Input, Processing, and Output
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Python quick start guide
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
Python Programming Fundamentals
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Topics Introduction Hardware and Software How Computers Store Data
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Munster Programming Training
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 1 Introduction to Computers and Programming.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Methods and Data Programming with Alice and Java First Edition.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
COP 2510 Chapter 6 - Functions. Random function (randint) #import the desired function import random #integer random number in the range of 1 through.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
Topics Introduction to Functions Defining and Calling a Void Function
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Chapter 2: Input, Processing, and Output
Chapter 3 Simple Functions
Lesson 06: Functions Class Chat: Attendance: Participation
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
Loops and Simple Functions
Chapter 2: Input, Processing, and Output
Global Variables Created by assignment statement placed at beginning of program and outside all functions Can be accessed by any statement in the program.
Presentation transcript:

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning Functions and Modules

1-2 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Value-Returning Functions: Generating Random Numbers Concept: A value-returning function is a function that returns a value back to the part of the program that called it. Python, as well as most other programming languages, provides a library of prewritten functions that perform commonly needed tasks. These libraries typically contain a function that generates random numbers.

1-3 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Value-Returning Functions: Generating Random Numbers Value-returning function: A group of statements that performs a specific task Executed when called Returns a value back to the part of the program that called it

1-4 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Value-Returning Functions: Generating Random Numbers Standard Library Functions Already written and come with the language Make a programmer’s job easier Perform many commonly needed tasks Stored in files known as modules Include the import statement at the top of the program For example: input()raw_input()range()

1-5 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Value-Returning Functions: Generating Random Numbers Figure 6-1 The age variable references the value 25

1-6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Value-Returning Functions: Generating Random Numbers Generating Random Numbers For example: # causes the interpreter to load the contents of the random module import random # causes the interpreter to load the contents of number = random.randint(1, 100) Figure 6-2 A statement that calls the random function

1-7 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Value-Returning Functions: Generating Random Numbers Program 6-2 (random_numbers2.py)

1-8 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Value-Returning Functions: Generating Random Numbers # generates a random integer number in the range of 5 through 9 number = random.randrange(5, 10) # generates a random number in the [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100] number = random.randrange(0, 101, 10) # generates a random floating-point number in the range of 0.0 through 1.0 number = random.random() # generates a random floating-point number in the range of 1.0 through 10.0 number = random.uniform(1.0, 10.0)

1-9 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Random Integers randint(start,end) random integers start through end inclusive randrange(end) random integers 0 through end-1 randrange(start,end) random integers start through end -1 randrange(start,end,step) random integers start, start+step… Up to end 3-9

1-10 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Random Decimal (Floating Point) Numbers random() random decimal numbers 0.0 through 1.0: uniform(1.0,10.0) random decimal numbers 1.0, through 10.0:

1-11 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Your Own Value-Returning Functions Concept: A value-returning function has a return statement that returns value back to the part of the program that called it.

1-12 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Your Own Value-Returning Functions def function_name(): statement etc. return expression Figure 6-5 Parts of the function

1-13 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Your Own Value-Returning Functions Program 6-6 (total_ages.py)

1-14 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Your Own Value-Returning Functions Making the Most of the return statement def sum(num1, num2): result = num1 + num2 return result def sum(num1, num2): return num1 + num2

1-15 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Your Own Value-Returning Functions Using IPO Charts A tool used for designing and documenting functions IPO stands for Input, Processing, and Output Describes the input, processing, and output of a function

1-16 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Your Own Value-Returning Functions Figure 6-7 IPO charts for the getRegularPrice and discount functions

1-17 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Your Own Value-Returning Functions Returning Strings def get_name(): # Get the user’s name name = raw_input(‘Enter your name:’) # Return the name return name

1-18 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Your Own Value-Returning Functions Returning Boolean Values number = input(‘Enter a number: ‘) if is_even(number): print ‘The number is even.’ else: print ‘The number is odd.’ def is_even(number): if (number % 2) == 0: status = True else: status = False return status def is_even(number): if (number % 2) == 0: status = True else: status = False return status For example: Determine whether a number is even or odd … For example: Validate user input…

1-19 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Writing Your Own Value-Returning Functions Returning Multiple Values first_name, last_name =get_name() def get_name(): # Get the user’s first and last name. first = raw_input(‘Enter your first name: ‘) last = raw_input(‘Enter your last name: ‘) # Return both names. return first, last

1-20 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The math Module Concept: The Python standard library’s math module contains numerous functions that can be used in mathematical calculations.

1-21 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The math Module Table 6-2 Many of the functions in the math module

1-22 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The math Module Program 6-9 (square_root.py)

1-23 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Storing Functions In Modules Concept: A module is a file that contains Python code. Large programs are easier to debug and maintain when they are divided into modules.

1-24 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Storing Functions In Modules A module is a file that contains Python code Modularization makes large programs easier to understand, test, and maintain. Each module should contain functions Modules make it easier to reuse the same code Module file names should end in.py A module name cannot be the same as a Python key word Place the module in the same folder as the program

1-25 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Storing Functions In Modules Menu Driven Programs Display a list of the operations Allow the user to select the operation List of operations = menu An if-elif-else statement carries out the user’s selection

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley QUESTIONS ? Chapter 6 Value-Returning Functions and Modules