3: Graphs of Inverse Functions © Christine Crisp Teach A Level Maths Vol. 2: A2 Core Modules.
Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem...
Transcript of Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem...
![Page 1: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/1.jpg)
Functions & Modules
Lecture 3
CS 1110: Introduction to Computing Using Python
[Andersen, Gries, Lee, Marschner, Van Loan, White]
![Page 2: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/2.jpg)
Announcements
• Be checking course page for announcements http://www.cs.cornell.edu/courses/cs1110/2017sp/
• AEW workshops still have space ENGRG 1010
• can enroll through Student Center 1-credit S/U course 2-hour weekly workshop work on problem sets related to the week’s content
2/2/2017 Functions & Modules 2
![Page 3: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/3.jpg)
Things to Do Before Next Class
• Read the rest of Chapter 3 Can skip 3.10
• You should be using the First Edition of the textbook Second edition is for Python 3
2/2/2017 Functions & Modules 3
![Page 4: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/4.jpg)
Function Calls
• Python supports expressions with math-like functions A function in an expression is a function call Will explain the meaning of this later
• Function expressions have the form fun(x,y,…)
• Examples (math functions that work in Python): round(2.34) max(a+3,24)
2/2/2017 Functions & Modules 4
functionname
argument
Arguments can be any expression
![Page 5: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/5.jpg)
Always-available Built-in Functions
• You have seen many functions already Type casting functions: int(), float(), bool() Get type of a value: type() Exit function: exit()
• Longer list: http://docs.python.org/2/library/functions.html
2/2/2017 Functions & Modules 5
Arguments go in (), but name() refers to function in general
![Page 6: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/6.jpg)
The Lack of Built-in Functions
• Python contains few built-in functions• Missing many functions you would expect Example: cos(), sqrt()
• Many more functions are available through built-in modules
2/2/2017 Functions & Modules 6
![Page 7: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/7.jpg)
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>>> math.cos(2.0)-0.4161468365471424
2/2/2017 Functions & Modules 7
![Page 8: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/8.jpg)
Necessity of import
With import
C:\> python
>>> import math>>> math.cos(2.0)-0.4161468365471424
Without import
C:\> python
>>> math.cos(2.0)Traceback (most recent call last):
File "<stdin>", line 1, in <module>NameError: name 'math' is not defined
2/2/2017 Functions & Modules 8
This is the Windows command line
(Mac looks different)
(startup text omitted) (startup text omitted)
Python is unaware of what “math” is
![Page 9: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/9.jpg)
Module Variables
• Modules can have variables, too• Can access them like this:
<module name>.<variable name>
• Example:>>> import math>>> math.pi3.141592653589793
2/2/2017 Functions & Modules 9
![Page 10: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/10.jpg)
module help
• After importing a module, can see what functions and variables are available with: help(<module name>)
2/2/2017 Functions & Modules 10
![Page 11: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/11.jpg)
Reading the Python Documentation
2/2/2017 Functions & Modules 11
https://docs.python.org/2/library/math.html
![Page 12: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/12.jpg)
Reading the Python Documentation
2/2/2017 Functions & Modules 12
Function name
Possible arguments
What the function evaluates toModule
https://docs.python.org/2/library/math.html
![Page 13: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/13.jpg)
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
2/2/2017 Functions & Modules 13
![Page 14: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/14.jpg)
Making your Own Module
2/2/2017 Functions & Modules 14
• Write in a text editor We use Komodo Edit… …but any editor will work
![Page 15: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/15.jpg)
Interactive Shell vs. Modules
Python Interactive Shell Module
2/2/2017 Functions & Modules 15
• Type python at command line
• Type commands after >>>
• Python executes as you type
• Written in text editor
• Loaded through import
• Python executes statements when import is called
![Page 16: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/16.jpg)
module.py
Module Text
# module.py
"""This is a simple module.It shows how modules work"""
x = 1+2x = 3*x
2/2/2017 Functions & Modules 16
Single line comment(not executed)
Docstring (note the Triple Quotes)Acts as a multiple-line commentUseful for code documentation
CommandsExecuted on import
![Page 17: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/17.jpg)
Modules Must be in Working Directory!
• Must run python from the same folder as the module
2/2/2017 Functions & Modules 17
![Page 18: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/18.jpg)
Using a Module (module.py)
Module Text
# module.py
"""This is a simple module.It shows how modules work"""
x = 1+2x = 3*x
Python Command Shell
>>>
2/2/2017 Functions & Modules 18
![Page 19: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/19.jpg)
Using a Module (module.py)
Module Text
# module.py
"""This is a simple module.It shows how modules work"""
x = 1+2x = 3*x
Python Command Shell
>>> import module
2/2/2017 Functions & Modules 19
Needs to be the same name as the file without the “.py”
![Page 20: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/20.jpg)
On import….
Module Text
# module.py
"""This is a simple module.It shows how modules work"""
x = 1+2x = 3*x
Python Command Shell
>>> import module
2/2/2017 Functions & Modules 20
Python does not execute (because of #)
Python does not execute(because of """ and """)
Python executes this. 3x
Python executes this. x 3 9xThis variable x stays “within” the module
![Page 21: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/21.jpg)
Using a Module (module.py)
Module Text
# module.py
"""This is a simple module.It shows how modules work"""
x = 1+2x = 3*x
Python Command Shell
>>> import module>>> module.x
2/2/2017 Functions & Modules 21
module name
The variable we want to access
![Page 22: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/22.jpg)
Using a Module (module.py)
Module Text
# module.py
"""This is a simple module.It shows how modules work"""
x = 1+2x = 3*x
Python Command Shell
>>> import module>>> module.x9
2/2/2017 Functions & Modules 22
![Page 23: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/23.jpg)
You Must import
C:\> python>>> import module>>> module.x9
C:\> python>>> module.xTraceback (most recent call last):
File "<stdin>", line 1, in <module>NameError: name 'module' is not defined
2/2/2017 Functions & Modules 23
![Page 24: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/24.jpg)
You Must Use the Module Name
>>> import module>>> module.x9
>>> import module>>> xTraceback (most recent call last):
File "<stdin>", line 1, in <module>NameError: name 'x' is not defined
2/2/2017 Functions & Modules 24
![Page 25: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/25.jpg)
What does the docstring do?
Module Text
# module.py
"""This is a simple module.It shows how modules work"""
x = 1+2x = 3*x
Python Command Shell
2/2/2017 Functions & Modules 25
![Page 26: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/26.jpg)
from command
• You can also import like this:from <module> import <function name>
• Example:>>> from math import pi>>> pi3.141592653589793
2/2/2017 Functions & Modules 26
Note that you don’t need the module name now
![Page 27: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/27.jpg)
from command
• You can also import everything from a module:from <module> import *
• Example:>>> from math import *>>> pi3.141592653589793>>> cos(pi)-1.0
2/2/2017 Functions & Modules 27
Module functions now behave like built-in functions
![Page 28: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/28.jpg)
2/2/2017 Functions & Modules 28
Dangers of Importing Everything
>>> e = 12345>>> from math import *>>> e2.718281828459045 e was overwritten!
![Page 29: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/29.jpg)
2/2/2017 Functions & Modules 29
Avoiding from Keeps Variables Separate
>>> e = 12345>>> import math>>> math.e2.718281828459045>>> e12345
![Page 30: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/30.jpg)
Ways of Executing Python Code
1. running the Python Interactive Shell2. importing a module3. NEW: running a script
2/2/2017 Functions & Modules 30
![Page 31: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/31.jpg)
Running a Script
• From the command line, type:python <script filename>
• Example:C:\> python module.pyC:\>
• Actually, something did happen Python executed all of module.py
2/2/2017 Functions & Modules 31
looks like nothing happened
![Page 32: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/32.jpg)
Running module.py as a script
module.py
# module.py
"""This is a simple module.It shows how modules work"""
x = 1+2x = 3*x
Command Line
C:\> python module.py
2/2/2017 Functions & Modules 32
Python does not execute (because of #)
Python does not execute(because of """ and """)
Python executes this. 3x
Python executes this. x 3 9x
![Page 33: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/33.jpg)
Running module.py as a script
module.py
# module.py
"""This is a simple module.It shows how modules work"""
x = 1+2x = 3*x
Command Line
C:\> python module.pyC:\>
2/2/2017 Functions & Modules 33
when the script ends, all memory used by module.py is deleted
thus, all variables get deleted (including x)
so there is no evidence that the script ran
![Page 34: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/34.jpg)
Creating Evidence that the Script Ran
• New (very useful!) command: printprint <expression>
• print evaluates the <expression> and writes the value to the console
2/2/2017 Functions & Modules 34
![Page 35: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/35.jpg)
module.py vs. script.py
module.py
# module.py
""" This is a simple module.It shows how modules work"""
x = 1+2x = 3*x
script.py
# script.py
""" This is a simple script.It shows why we use print"""
x = 1+2x = 3*xprint x
2/2/2017 Functions & Modules 35
Only difference
![Page 36: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/36.jpg)
Running script.py as a script
Command Line
C:\> python script.py9
C:\>
script.py
2/2/2017 Functions & Modules 36
# script.py
""" This is a simple script.It shows why we use print"""
x = 1+2x = 3*xprint x
![Page 37: Functions & Modules - Cornell University · 2/2/2017 · 2-hour weekly workshop work on problem sets related to the week’s content ... 2/2/2017 Functions & Modules 12. Function](https://reader034.fdocuments.in/reader034/viewer/2022042218/5ec45720e14be61cc0190017/html5/thumbnails/37.jpg)
Modules vs. Scripts
Module
• Provides functions, variables• import it into Python shell
Script
• Behaves like an application• Run it from command line
2/2/2017 Functions & Modules 37
Files look the same. Difference is how you use them.