1 / 26

STARTING OUT WITH Python First Edition by Tony Gaddis

Chapter 6 Value-Returning Functions and Modules. STARTING OUT WITH Python First Edition by Tony Gaddis. 6.1 Introduction to Value-Returning Functions: Generating Random Numbers. Concept:

dai-chan
Download Presentation

STARTING OUT WITH Python First Edition by Tony Gaddis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6Value-Returning Functions and Modules STARTING OUT WITH PythonFirst Edition by Tony Gaddis

  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.

  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

  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()

  5. 6.1 Introduction to Value-Returning Functions: Generating Random Numbers Figure 6-1 The age variable references the value 25

  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

  7. 6.1 Introduction to Value-Returning Functions: Generating Random Numbers Program 6-2 (random_numbers2.py)

  8. 6.1 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)

  9. 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

  10. 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

  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.

  12. 6.2 Writing Your Own Value-Returning Functions • def function_name(): • statement • statement • etc. • return expression Figure 6-5 Parts of the function

  13. 6.2 Writing Your Own Value-Returning Functions Program 6-6 (total_ages.py)

  14. 6.2 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

  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

  16. 6.2 Writing Your Own Value-Returning Functions Figure 6-7 IPO charts for the getRegularPrice and discount functions

  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

  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.’ • For example: Determine whether a number is even or odd … • def is_even(number): • if (number % 2) == 0: • status = True • else: • status = False • return status • For example: Validate user input…

  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

  20. 6.3 The math Module Concept: The Python standard library’s math module contains numerous functions that can be used in mathematical calculations.

  21. 6.3 The math Module Table 6-2 Many of the functions in the math module

  22. 6.3 The math Module Program 6-9 (square_root.py)

  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.

  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

  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

  26. Chapter 6Value-Returning Functions and Modules QUESTIONS ?

More Related