Programming with Python · free preview
Creating Functions
Define a function that takes parameters. Return a value from a function. Test and debug a function. Set default values for function parameters. Explain why we should divide programs into small, single-purpose functions.
1. Overview
Overview
2. Composing Functions
Composing Functions
3. Variable Scope
Variable Scope
4. Tidying up
Tidying up
5. Testing and Documenting
Testing and Documenting
6. Defining Defaults
Defining Defaults
7. Readable functions
Readable functions
8. Combining Strings
Combining Strings
9. Return versus print
Return versus print
10. Selecting Characters From Strings
Selecting Characters From Strings
11. Rescaling an Array
Rescaling an Array
12. Testing and Documenting Your Function
Testing and Documenting Your Function
13. Defining Defaults
Defining Defaults
14. Variables Inside and Outside Functions
Variables Inside and Outside Functions
15. Mixing Default and Non-Default Parameters
Mixing Default and Non-Default Parameters
16. Readable Code
Readable Code
17. Distinguish return from print
After result = fahr_to_celsius(32), what does result contain?
18. Trace a complete Fahrenheit converter with a docstring
Run this complete program mentally first. Predict the three printed floats and the value of fahr_to_celsius.__doc__. Then run it and compare your predictions, including the order of the printed lines.
Example. Trace a complete Fahrenheit converter with a docstring
Problem. Run this complete program mentally first. Predict the three printed floats and the value of fahr_to_celsius.__doc__. Then run it and compare your predictions, including the order of the printed lines.
Correct solution.
def fahr_to_celsius(temp_f):
"""Return temp_f converted from Fahrenheit to Celsius."""
return ((temp_f - 32) * (5/9))
print('boiling point:', fahr_to_celsius(212.0))
print('freezing point:', fahr_to_celsius(32.0))
print('body temperature:', round(fahr_to_celsius(98.6), 1))
print(fahr_to_celsius.__doc__)Output
boiling point: 100.0
freezing point: 0.0
body temperature: 37.0
Return temp_f converted from Fahrenheit to Celsius.Trace table.
- Before the first call, only the function name and the docstring exist.
fahr_to_celsius(212.0)assignstemp_f = 212.0, evaluates the expression, and returns100.0.fahr_to_celsius(32.0)uses a freshtemp_f = 32.0and returns0.0.fahr_to_celsius(98.6)computes37.0at the expression level, andround(..., 1)prints that exact rounded float.
关键提醒。
The function contract is readability plus reuse, the parameter receives the input, the expression performs the Fahrenheit-to-Celsius change, and return hands the new value back to the caller. The docstring is a real object stored on the function, so it can be printed. The three calls do not depend on shared mutable state; each call starts with a new local parameter binding.
19. Retrieval practice
Without looking back, write the exact body of offset_mean(values, target_mean_value) for a plain Python list so that it returns a new list whose mean is target_mean_value. Then apply it to values = [1, 2, 3, 4] and target_mean_value = 0.0, and state the returned list.
Write only the function body, then state the returned list to one decimal place. Name the two data operations used to move the mean.
20. Mastery check
Apply the function to a new input, define offset_mean(values, target_mean_value=100.0) and call it with [10, 20, 30]. Give the exact returned list, the mean of the original values, and one reason why this function should not use a mutable default value.
State the original mean, the shifted list, and the parameter design decision. Do not answer with the lesson objective alone.
Practice this interactively
Free account · instant grading · spaced review that schedules itself.
Start this course for free