Lecture 3 Functions & Modules
(Optional) Readings Reading for Next Week • Chapter 3 in the text • PLive : Activities 3-3.1, 3-3.2, 3-3.4 § But can skip section 3.9 (not 3-3.3), 3-4.1, 3-4.2. • Browse the Python API § Will learn what that is today • (Old) Lecture on VideoNote § Do not need to read all of it • Sections 8.1, 8.2, 8.5, 8.8 § Strings are needed for A1 § But Chap 8 mixes easy stuff with advanced stuff [xkcd.com] 8/30/18 Functions & Modules 2
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,…) function argument name • Examples (math functions that work in Python): § round(2.34) § max(a+3,24) 8/30/18 Functions & Modules 3
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,…) function argument name • Examples (math functions that work in Python): Arguments can be § round(2.34) any expression § max(a+3,24) 8/30/18 Functions & Modules 4
Built-In Functions • You have seen many functions already § Type casting functions: int() , float() , bool() § Dynamically type an expression: type() Arguments go in () , § Help function: help() but name() refers to § Quit function: quit() function in general • One of the most important functions is print() § print(exp) displays value of exp on screen § Will see later why this is important 8/30/18 Functions & Modules 5
Built-in Functions vs Modules • The number of built-in functions is small § http://docs.python.org/3/library/functions.html • Missing a lot of functions you would expect § Example : cos() , sqrt() • Module : file that contains Python code § A way for Python to provide optional functions § To access a module, the import command § Access the functions using module as a prefix 8/30/18 Functions & Modules 6
Example: Module math >>> import math >>> math.cos(0) 1.0 >>> cos(0) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'cos' is not defined >>> math.pi 3.141592653589793 >>> math.cos(math.pi) -1.0 8/30/18 Functions & Modules 7
Example: Module math To access math >>> import math functions >>> math.cos(0) 1.0 >>> cos(0) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'cos' is not defined >>> math.pi 3.141592653589793 >>> math.cos(math.pi) -1.0 8/30/18 Functions & Modules 8
Example: Module math To access math >>> import math functions >>> math.cos(0) Functions 1.0 require math >>> cos(0) prefix! Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'cos' is not defined >>> math.pi 3.141592653589793 >>> math.cos(math.pi) -1.0 8/30/18 Functions & Modules 9
Example: Module math To access math >>> import math functions >>> math.cos(0) Functions 1.0 require math >>> cos(0) prefix! Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'cos' is not defined >>> math.pi Module has variables too! 3.141592653589793 >>> math.cos(math.pi) -1.0 8/30/18 Functions & Modules 10
Example: Module math To access math >>> import math Other Modules functions >>> math.cos(0) Functions 1.0 • io require math >>> cos(0) § Read/write from files prefix! • random Traceback (most recent call last): § Generate random numbers File "<stdin>", line 1, in <module> § Can pick any distribution NameError: name 'cos' is not defined • string >>> math.pi Module has § Useful string functions variables too! 3.141592653589793 • sys >>> math.cos(math.pi) § Information about your OS -1.0 8/30/18 Functions & Modules 11
Using the from Keyword >>> import math • Be careful using from ! Must prefix with >>> math.pi • Using import is safer module name 3.141592653589793 § Modules might conflict >>> from math import pi (functions w/ same name) No prefix needed >>> pi § What if import both? for variable pi 3.141592653589793 • Example : Turtles >>> from math import * § Used in Assignment 4 >>> cos(pi) § 2 modules: turtle , tkturtle No prefix needed -1.0 § Both have func. Turtle() for anything in math 8/30/18 Functions & Modules 12
Reading the Python Documentation http://docs.python.org/library 8/30/18 Functions & Modules 13
Reading the Python Documentation http://docs.python.org/library 8/30/18 Functions & Modules 14
Reading the Python Documentation Function name Possible arguments Module What the function evaluates to http://docs.python.org/library 8/30/18 Functions & Modules 15
Interactive Shell vs. Modules • Write in a code editor • Launch in command line § We use Atom Editor • Type each line separately § But anything will work • Python executes as you type • Load module with import 8/30/18 Functions & Modules 16
Using a Module Module Contents """ A simple module. This file shows how modules work """ # This is a comment x = 1+2 x = 3*x x 8/30/18 Functions & Modules 17
Using a Module Module Contents """ A simple module. This file shows how modules work """ Single line comment (not executed) # This is a comment x = 1+2 x = 3*x x 8/30/18 Functions & Modules 18
Using a Module Module Contents """ A simple module. Docstring (note the Triple Quotes) Acts as a multiple-line comment Useful for code documentation This file shows how modules work """ Single line comment (not executed) # This is a comment x = 1+2 x = 3*x x 8/30/18 Functions & Modules 19
Using a Module Module Contents """ A simple module. Docstring (note the Triple Quotes) Acts as a multiple-line comment Useful for code documentation This file shows how modules work """ Single line comment (not executed) # This is a comment x = 1+2 Commands Executed on import x = 3*x x 8/30/18 Functions & Modules 20
Using a Module Module Contents """ A simple module. Docstring (note the Triple Quotes) Acts as a multiple-line comment Useful for code documentation This file shows how modules work """ Single line comment (not executed) # This is a comment x = 1+2 Commands Executed on import x = 3*x x Not a command. import ignores this 8/30/18 Functions & Modules 21
Using a Module Module Contents Python Shell """ A simple module. >>> import module x >>> This file shows how modules work """ # This is a comment x = 1+2 x = 3*x x 8/30/18 Functions & Modules 22
Using a Module Module Contents Python Shell """ A simple module. >>> import module x >>> This file shows how modules work Traceback (most recent call last): """ File "<stdin>", line 1, in <module> NameError: name 'x' is not defined # This is a comment x = 1+2 x = 3*x x 8/30/18 Functions & Modules 23
Using a Module Module Contents Python Shell """ A simple module. >>> import module x >>> This file shows how modules work Traceback (most recent call last): """ File "<stdin>", line 1, in <module> NameError: name 'x' is not defined # This is a comment module.x >>> “ Module data ” must be x = 1+2 9 prefixed by module name x = 3*x x 8/30/18 Functions & Modules 24
Using a Module Module Contents Python Shell """ A simple module. >>> import module x >>> This file shows how modules work Traceback (most recent call last): """ File "<stdin>", line 1, in <module> NameError: name 'x' is not defined # This is a comment module.x >>> “ Module data ” must be x = 1+2 9 prefixed by module name x = 3*x help(module) >>> Prints docstring and x module contents 8/30/18 Functions & Modules 25
Modules Must be in Working Directory! Module you want is in this folder 8/30/18 Functions & Modules 26
Modules Must be in Working Directory! Module you want is in this folder Have to navigate to folder BEFORE running Python 8/30/18 Functions & Modules 27
Modules vs. Scripts Module Script • Provides functions, variables • Behaves like an application § Example : temp.py § Example : helloApp.py • import it into Python shell • Run it from command line: >>> import temp python helloApp.py >>> temp.to_fahrenheit(100) 212.0 >>> 8/30/18 Functions & Modules 28
Recommend
More recommend