Functions
Functions are like machines


Learning to write your own functions will greatly increase the complexity of the programs that you can write
A function is a black box-it takes some input,does something with it, and spits out some output
Functions hide details away, allowing you to solve problems at a higher level without getting bogged down
Examples
A typical function looks like this:
A function example: sum
Anatomy of sum function
Writing your own function
Importing a function from a file (module)
Once we import
MyMathFunctionsmodule we just wrote, we can use themysumfunction andmyproductfunction just like the built-in functionThe way to call a function is to give the function name followed by parenthesis with values for the number of arguments expected
Function arguments
We can define functions with more than one arguments
Arguments with default values do not need to be supplied when calling a function. But if provided, will overwrite the default values
Some existing Python modules
The
osmodule provides a platform independent way to work with the operating system, make or remove files and directoriesThe
csvmodule provides readers and writers for comma separated value dataThe
sysmodule contains many objects and functions for dealing with how python was complied or called when executedThe
globmodule proves theglobfunction to perform file globbing similar to what the unix shell providesThe
mathmodule provides common algebra and trigonometric function along with several math constantsThe
remodule provides access to powerful regular expressionThe
datetimemodule provides time and datetime objects. allowing easy comparison of times and datesThe
timemodule provides simple estimates for how long a command takesThe
picklemodule provides a way to save python objects to a file that you can unpickle later in a different programThe
pypimodule helps package installationThe
numpymodule is the de facto standard for numerical computingThe
pandasmodule is useful for tabular data managingThe
matplotlibmodule is the most frequently used plotting package in PythonThe
seabornmodule is a module based onmatplotlib. It provides a high-level interface for drawing attractive graphics.
Last updated
Was this helpful?
