Learning about Functions
Learn about functions in Python.
We'll cover the following
Functions
Before you get into graphing, you need to learn about functions.
Let’s use the equation to explain what a function is in Python. You can write a function that takes x, turns it into , and returns back to what originally called the function. This characteristic means that you do not have to keep pasting in the equation every time you want to use it; you can just invoke the function to calculate it for us. It is easy enough to write each time, but it would become more annoying to keep pasting a more complicated equation. The other benefit is that you can edit the function’s calculation in one place, and those changes will be reflected immediately every time we call that equation. If you suddenly wanted the function to calculate instead of , you would not have to go hunt for every in the code to change. You create a function in Python by starting with the def
keyword. For example:
def squared(x):return x*x # or x**2. Remember x^2 won't work!
That function would accept any number and return
its square. You call the function by using its name and passing the argument x
, which can be any variable name:
a = 5y = squared(a)print(y)
You need to define the squared()
function before you try calling it; therefore, most functions will go at the top of the program after the imports so that they can be called later in the program. This next function will take x
and print its square to console, but it does not return a value for x
:
a = 5y = squared2(a)print(type(y))
25
will be printed to the console, but y
will be of type NoneType
, not 25
, because you did not return anything.
Type Hinting
Starting in Python 3, you can use type hinting to suggest what data type x
is, and what the function will return. You can also create docstrings, which are blurbs that describe what the function does, what parameters it accepts, and what it returns. This is like writing a big comment explaining what your function does, and it is always beneficial (though sometimes tedious or annoying) to write one for every function. Here is the original squared
function with type hinting and a docstring:
def squared(x: float) -> float:"""Takes x as a float and returns its square:param x: float:return x**2"""return x*x
The type hinting says that the squared
function expects a float
and will return a float
. In an IDE like Spyder, PyCharm, or many others, if you type squared(
with just the open parenthesis, a popup textbox will remind you what the argument(s) should be.
Challenge
Create a function is_div_by_seven()
that accepts number
as an int
and returns True
if number
is divisible by 7, or returns False
if number
is not divisible by 7.
number
can only be a nonzero, positive int
.
A number is divisible by 7 if there is no remainder.
Example:
is_div_by_seven(7) --> True
is_div_by_seven(100) --> False
# Fill in below with a is_div_by_seven(num) function
Now that you have learned about functions, let’s dig into graphing thrust available and thrust required.