TME 310 - Computational Physical Modeling
University of Washington Tacoma
Functions allow us to organize and re-use our work.
Part of the purpose of looking back at our solutions during reflection and self-grading is to identify ways our work may be useful for future problems.
Here’s an example of a function called average:
The function signature consists of:
def keywordaverage)(x, y)
() are not(x=1,y=2):) at the end of the linedef average(x, y):
total = x + y # <--- the function body starts here...
avg = total / 2
return avg # <--- and ends hereThe function body consists of:
pass keyword)return statementUser-defined functions are called just like built in functions but they need to be called after their defined.
Functions can have default values for some input parameters. Parameters with default values are optional.
Required parameters have to come before optional parameters.
SyntaxError: parameter without a default follows parameter with a default
Python has several built-in and external libraries that extend its capabilities.
The two we’ll use most in this course are
We’ll look at Matplotlib today
Import libraries with the import statement
It’s common to aliases as shorthand for imported modules by using the as keyword:
This imports the entire matplotlib library into your code. You can use mpl to refer to it.
Sometimes libraries are large and we only need a small part of them. In these cases, we can import a specific function using the from keyword:
Subcomponents of libraries are accessed with . syntax
Or we can import a submodule:
It’s typical to start using Matplotlib with:
I’ll add a link to the Matplotlib documentation page to the course website. But for today, we’ll just look at one plotting fuction: matplotlib.pyplot.plot()

