Lecture 3: Functions & Modules
(Sections 3.1-3.3) CS 1110 Introduction to Computing Using Python
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
Lecture 3: Functions & Modules (Sections 3.1-3.3) CS 1110 - - PowerPoint PPT Presentation
http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 3: Functions & Modules (Sections 3.1-3.3) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] CS1110 Spring
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
http://www.cs.cornell.edu/courses/cs1110/2018sp
2
3
§ A function in an expression is a function call § Will explain the meaning of this later
function name argument
4
5
6
7
C:\> python >>> import math >>> math.cos(2.0)
C:\> python >>> math.cos(2.0) Traceback (most recent call last): File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined
Python is unaware
This is the Windows command line (Mac looks different)
8
9
10
int() float() str() type() print() …
11
int() float() str() type() print() … x 7
12
int() float() str() type() print() … x 7
cos() sqrt() e pi …
13
https://docs.python.org/3/library/math.html
14
https://docs.python.org/3/library/math.html Function name Possible arguments What the function evaluates to Module
15
§ Read/write from files
§ Generate random numbers § Can pick any distribution
§ Useful string functions
§ Information about your OS
16
Write in a text editor
We use Komodo Edit… …but any editor will work
17
when import is called
Section 2.4 in your textbook discusses a few differences
18
# my_module.py """This is a simple module. It shows how modules work""" x = 1+2 x = 3*x Single line comment (not executed) Docstring (note the Triple Quotes) Acts as a multi-line comment Useful for code documentation Commands Executed on import
19
20
# my_module.py """This is a simple module. It shows how modules work""" x = 1+2 x = 3*x
>>>
21
# my_module.py """This is a simple module. It shows how modules work""" x = 1+2 x = 3*x
>>> import my_module Needs to be the same name as the file without the “.py”
22
# my_module.py """This is a simple module. It shows how modules work""" x = 1+2 x = 3*x
>>> import module Python does not execute (because of #) Python does not execute (because of """ and """) Python executes this. 3 x Python executes this. 9
variable x stays “within” the module
23
# my_module.py """This is a simple module. It shows how modules work""" x = 1+2 x = 3*x
>>> import my_module After you hit “Return” here what will python print next? (A) >>> (B) 9 >>> (C) an error message (D) The text of my_module.py (E) Sorry, no clue.
24
# my_module.py """This is a simple module. It shows how modules work""" x = 1+2 x = 3*x
>>> import my_module After you hit “Return” here what will python print next? (A) >>> (B) 9 >>> (C) an error message (D) The text of my_module.py (E) Sorry, no clue.
25
# my_module.py """This is a simple module. It shows how modules work""" x = 1+2 x = 3*x
>>> import my_module >>> my_module.x
module name
The variable we want to access
26
# my_module.py """This is a simple module. It shows how modules work""" x = 1+2 x = 3*x
>>> import my_module >>> my_module.x 9
27
C:\> python >>> import my_module >>> my_module.x 9 C:\> python >>> my_module.x
Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'my_module' is not defined
28
>>> import my_module >>> my_module.x 9 >>> import my_module >>> x
Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x' is not defined
29
# my_module.py """This is a simple module. It shows how modules work""" x = 1+2 x = 3*x
30
31
32
33
34
35
36
# my_module.py """This is a simple module. It shows how modules work""" x = 1+2 x = 3*x
C:\> python module.py Python does not execute (because of #) Python does not execute (because of """ and """) Python executes this. 3 x Python executes this. 9
37
# my_module.py """This is a simple my_module. It shows how modules work""" x = 1+2 x = 3*x
C:\> python my_module.py C:\>
when the script ends, all memory used by my_module.py is deleted thus, all variables get deleted (including x) so there is no evidence that the script ran
38
# my_module.py """This is a simple my_module. It shows how modules work""" x = 1+2 x = 3*x
C:\> python my_module.py C:\> my_module.x After you hit “Return” here what will be printed next? (A) >>> (B) 9 >>> (C) an error message (D) The text of my_module.py (E) Sorry, no clue.
39
# my_module.py """This is a simple my_module. It shows how modules work""" x = 1+2 x = 3*x
C:\> python my_module.py C:\> my_module.x After you hit “Return” here what will be printed next? (A) >>> (B) 9 >>> (C) an error message (D) The text of my_module.py (E) Sorry, no clue.
40
41
# my_module.py """ This is a simple module. It shows how modules work""" x = 1+2 x = 3*x
# script.py """ This is a simple script. It shows why we use print""" x = 1+2 x = 3*x print(x) Only difference
42
C:\> python script.py 9 C:\>
# script.py """ This is a simple script. It shows why we use print""" x = 1+2 x = 3*x print(x)
43
C:\> python >>> x = 1+2 >>> x = 3*x >>> x 9 >>> print(x) 9 >>>
# script.py """ This is a simple script. It shows why we use print""" x = 1+2 x = 3*x print(x) # note: in script mode, you will # not get output if you just type x
44
45
we want to make functions like this
46
47