The understanding of variable definitions and control statements is fundamental to any programming language. Furthermore, the syntax of basic data structures such as lists and strings is foundational to mastering the Python language. In this unit, we take another step to improve upon our program organization skills by introducing functions. When a given task is performed many times throughout a program, it is usually wrapped within a function so that it can be used or "called" whenever needed. This notion of creating a specific function or "procedure" to achieve a given task is part of a programming methodology known as "procedural programming". We will also briefly contrast this approach with the use of methods that are used in "object-oriented programming". We will discuss these in greater detail in Unit 10.
Completing this unit should take you approximately 4 hours.
Before delving into some Python libraries dealing with applied mathematics, visit this optional site to review trigonometry if you need a refresher.
Programming also involves simulating random experiments such as flipping coins or rolling dice. If you need to review these topics, visit this optional site for a refresher.
We have already seen using the import command for importing the matplotlib library and the numpy package. Much of Python's power rests in the vast collection of packages, libraries, and modules available for just about any application you could think of. The math module contains a host of mathematically oriented methods typical of what a programmer would need to perform basic calculations. Consider executing this set of instructions:
import math a=math.exp(1) print(a) b=math.pi print(b) x=100 print(math.log(x,10)) print(math.log10(x)) y=math.pi/2 print(math.cos(y)) print(math.sin(y)) y=8 z=1/3 print(math.pow(y,z))
Notice once again the object-oriented dot notation for calling a method. Just about any function that can be calculated on a sophisticated calculator can be performed using the math module. For the sake of simplifying the code, if you know exactly what methods are needed from a given library, you can select a subset using the from
keyword. Consider the code from earlier rewritten using the from
keyword:
from math import exp, pi, log, log10, cos, sin, pow a=exp(1) print(a) b=pi print(b) x=100 print(log(x,10)) print(log10(x)) y=pi/2 print(cos(y)) print(sin(y)) y=8 z=1/3 print(pow(y,z))
Now all the math method calls look exactly like function calls. Sometimes it is more convenient to use the reduced representation.
While this example is not how we would perform encryption in practice, it is highly instructive for reviewing concepts covered so far regarding user-defined functions. Implement this example in Repl.it in order to solidify your working knowledge of Python.
In this video, course designer Eric Sakk walks through the major topics we covered in Unit 5. As you watch, work through the exercises to try them out yourself.
Take this assessment to see how well you understood this unit.