03 Fns Modules
03 Fns Modules
edu/courses/cs1110/2019sp
Lecture 3:
Functions & Modules
(Sections 3.1-3.3)
CS 1110
Introduction to Computing Using Python
3
Modules
• Many more functions available via built-in modules
§ “Libraries” of functions and variables
• To access a module, use the import command:
import <module name>
Can then access functions like this:
<module name>.<function name>(<arguments>)
Example:
>>> import math
>>> p = math.ceil(3.14159265)
>>> p
4 4
Module Variables
5
Visualizing functions & variables
• So far just built-ins
int()
float()
str()
type()
print()
…
C:\> python
>>>
6
Visualizing functions & variables
• So far just built-ins
int()
• Now we’ve defined a float()
new variable str()
type()
print()
…
C:\> python x 7
>>> x = 7
>>>
7
Visualizing functions & variables
• So far just built-ins
int()
• Now we’ve defined a float()
new variable str()
• Now we’ve imported a type()
module print()
…
C:\> python x 7
>>> x = 7 math
ceil()
>>> import math sqrt()
>>> e 2.718281
pi 3.14159
…
8
module help
9
Reading the Python Documentation
https://docs.python.org/3/library/math.html
10
A Closer Reading of the Python
Documentation
https://docs.python.org/3/library/math.html
Function
name
Possible arguments
11
Other Useful Modules
• io
§ Read/write from files
• random
§ Generate random numbers
§ Can pick any distribution
• string
§ Useful string functions
• sys
§ Information about your OS
12
Making your Own Module
13
Interactive Shell vs. Modules
Module Text
15
Modules Must be in Working Directory!
Must run python from same folder as the module
16
Using a Module (my_module.py)
x = 1+2
x = 3*x
17
On import….
18
variable x stays “within” the module
Clicker Question!
x = 1+2 my_module
x = 3*x
x x
3 9
21
Windows command line
You must import (Mac looks different)
my_module my_module
xx
3 9 xx
3 9
23
What does the docstring do?
# my_module.py
x = 1+2
x = 3*x
24
from command
• You can also import like this:
from <module> import <function name>
• Example:
>>> from math import pi
>>> pi no longer need the module name
3.141592653589793
pi 3.141592653589793
25
from command
• You can also import everything from a module:
from <module> import *
• Example:
>>> from math import *
ceil()
>>> pi sqrt()
3.141592653589793 e 2.718281828459045
pi 3.141592653589793
>>> ceil(pi) …
4
Module functions now behave
like built-in functions 26
Dangers of Importing Everything
>>> e = 12345
>>> from math import *
>>> e
2.718281828459045 e 2.718281828459045
12345
e was ceil()
overwritten! sqrt()
pi 3.141592653589793
…
27
Avoiding from Keeps Variables Separate
>>> e = 12345
>>> import math
>>> math.e
e 12345
2.718281828459045
math
>>> e
ceil()
12345 sqrt()
e 2.718281
pi 3.14159
…
28
Ways of Executing Python Code
29
Running a Script
30
Running my_module.py as a script
31
Running my_module.py as a script
35
my_module.py vs. script.py
my_module.py script.py
# my_module.py # script.py
x = 1+2 x = 1+2
x = 3*x x = 3*x
Only difference print(x)
36
Running script.py as a script
x = 1+2
x = 3*x
print(x)
37
Subtle difference about script mode
Module Script
39