Post on 30-Dec-2015
description
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
STARTING OUT WITH
PythonPythonFirst Edition
by Tony Gaddis
Chapter 6Value-Returning Functions and Modules
1-2
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-2
6.1 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 6-3
6.1 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 6-4
6.1 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 6-5
6.1 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 6-6
6.1 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 6-7
6.1 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 6-8
6.1 Introduction to Value-Returning Functions: Generating Random Numbers
# generates a random integer number in the range of 5 through 9number = 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.0number = random.random()
# generates a random floating-point number in the range of 1.0 through 10.0number = 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:
0.0863786658417
uniform(1.0,10.0)
random decimal numbers 1.0, through 10.0:
5.97946536804
3-10
1-11
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-11
6.2 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 6-12
6.2 Writing Your Own Value-Returning Functions
def function_name():
statement
statementetc.return
expressionFigure 6-5 Parts of the function
1-13
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-13
6.2 Writing Your Own Value-Returning Functions
Program 6-6 (total_ages.py)
1-14
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-14
6.2 Writing Your Own Value-Returning Functions
Making the Most of the return statement
def sum(num1, num2):result = num1 +
num2return result
def sum(num1, num2):return num1 + num2
1-15
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-15
6.2 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 6-16
6.2 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 6-17
6.2 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 6-18
6.2 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 = Trueelse:
status = False return status
def is_even(number):if (number % 2) == 0:
status = Trueelse:
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 6-19
6.2 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 6-20
6.3 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 6-21
6.3 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 6-22
6.3 The math Module
Program 6-9 (square_root.py)
1-23
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6-23
6.4 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 6-24
6.4 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 6-25
6.4 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 6Value-Returning Functions and Modules