FUNCTIONS
Function Argument
Name
in Functions
built-in modules
Functions Libraries of functions and variables.
To access a module, use the import command:
& import <module name>
Modules
Can then access functions like this:
<module name>.<function name>(<arguments>)
Example:
>>> import math
Modules >>> math.cos (2.0)
-0.4161468365471424
With Import Without Import
Functions >>> import math >>> math.cos(2.0)
& >>> math.cos(2.0)
-
Traceback (most
recent call last):
Modules 0.4161468365471424 File "<stdin>", line 1,
in <module>
NameError: name
'math' is not defined
module help
Functions io
&
Read/write from files
random
Modules Generate random numbers
Can pick any distribution
string
Useful string functions
sys
Other Useful Information about your OS
Modules
Functions Write in a text editor
We use Komodo Edit
& but any editor will work
Modules
Making your
Own Module
Module Text
Functions # module.py
&
"""This is a simple module.
It shows how modules work""
Modules
x = 1+2
x = 3*x
module.py
Useful for code documentation
Functions You can also import like this:
& from <module> import <function name>
Modules
Example:
>>> from math import pi
>>> pi
from command 3.141592653589793
Example:
>>> from math import *
>>> pi
from command 3.141592653589793
>>> cos(pi)
-1.0 Module functions now behave
like built-in functions
Functions
&
Modules
Anatomy of a
Function
Definition
Functions
&
Modules
Anatomy of a
Function
Definition
Functions Passes a value from the function to the
& caller
Modules Format: return <expression>
Understanding
How Functions
Work
Functions Example: to_centigrade
Random
Number Seeds
Generating Random number created by functions in
random module are actually pseudo-random
Random numbers
Seed value: initializes the formula that
Numbers generates random numbers
Need to use different seeds in order to
get different series of random numbers
By default uses system time for seed
Can use random.seed () function to
specify desired seed value
Random
Numbers
Random
Number Seeds