A3 - Math and Functions That Return Values
We can combine some of what we've learned so far to create programs that implement simple math formulas. Here is an example:
# Gather input
length = int(input("Enter the length of the rectangle: "))
width = int(input("Enter the width of the rectangle: "))
# Calculate
area = length * width
# Display output
print("The area of the rectangle is", area)
A couple things to notice here:
-
When we want to store user input as a number, we need to use
int(input(...))instead of justinput(...). Doing so will allow Python to perform mathematical operations with the variable later on. -
There are three main sections of code:
- A section to gather input
- A section to perform calculations
- A section to display output
Your assignment solutions should have these same three sections.
Functions
Python doesn't have an operator for every possible mathematical operation.
For more advanced math, we use functions.
Let's look at the math.sqrt function as an example, which takes the square root of a number:
import math
print(math.sqrt(25)) # 5
print(math.sqrt(25) + 6) # 11
print(math.sqrt(40+9)) # 7
x = math.sqrt(25) + math.sqrt(40+9) - 2
print(x) # 10
Characteristics of Function Calls
What we see above are examples of function calls.
- Function calls are of the form
function_name(<arguments>). For instance, in the function callmath.sqrt(25), the function name ismath.sqrtand the argument is25. - You can use a function call anywhere within a mathematical expression, and the argument itself can be a mathematical expression.
- We say that a function call returns a value, which will then replace the function call itself.
The value returned by
math.sqrt(25)is5. - Some functions can accept muliple arguments, separated by commas
Here are four other functions that can be used this way:
abs(x)returns the absolute value of x.round(x)returns x rounded to the nearest integer. You can optionally add a second argument to round off to a certain number of digits. For example,round(6.54321, 3)produces6.543.math.floor(x)returns x rounded down to the nearest integer.math.ceil(x)returns x rounded up to the nearest integer.
Imports
Most of the functionality we use in Python is not built into the language, but is imported from other modules.
Any functions you see beginning with math. are imported from the math module.
For these to work, you must add the line import math to the top of your program.
One More Full Example
Let's ask the user for the radius of a circle, then use that information
to calculate the area of the circle rounded to one decimal place.
The math module has a built-in variable math.pi that we can use for .
import math
# Gather input
radius = int(input("What is the radius? "))
# Calculate
area = math.pi * radius**2 # (1)!
# Display Output
print("The area of the circle is", round(area, 1))
- Recall that
**means "to the power of"
Your Task
Create three Python files:
a3a.pya3b.pya3c.py
You are going to do something similar to first and last examples above in each of these three files, but you should pick your own mathematical formulas to use (here are some ideas). A reminder that there are three sections to each of these examples: "gather input", "calculate" and "display result".
This would be a good template to use to get started in each of your files (it's identical to the last example above):
import math
# Gather input
radius = int(input("What is the radius? "))
# Calculate
area = math.pi * radius**2
# Display Output
print("The area of the circle is", round(area, 1))
At minimum, one of your programs should use a function that returns a value (for example, math.sqrt or round).
Glossary
-
Function - A small task carried out by Python.
-
Function Call - The code to trigger a function, consisting of its name and a list of arguments.
-
Argument - A piece of information sent to a function.
-
Module - An add-on to Python, providing access to extra functions and features.