Mini-Lecture 4 Functions
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/31/18 Functions 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): Arguments can be § round(2.34) any expression § max(a+3,24) 8/31/18 Functions 3
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 function is print() § print(exp) displays value of exp on screen § Will see later why this is important 8/31/18 Functions 4
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/31/18 Functions 5
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/31/18 Functions 6
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/31/18 Functions 7
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/31/18 Functions 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 Module has variables too! 3.141592653589793 >>> math.cos(math.pi) -1.0 8/31/18 Functions 9
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/31/18 Functions 10
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 : cos >>> from math import * § 2 modules: math , numpy >>> cos(pi) § Both have func. cos() No prefix needed -1.0 § Each has tradeoffs for anything in math 8/31/18 Functions 11
Other Variations >>> import math as m >>> m.cos(0) 1.0 >>> from math import cos >>> cos(0) 1.0 >>> sin(0) ERROR 8/31/18 Functions 12
Reading the Python Documentation http://docs.python.org/library 8/31/18 Functions 13
Reading the Python Documentation http://docs.python.org/library 8/31/18 Functions 14
Reading the Python Documentation Function name Possible arguments Module What the function evaluates to http://docs.python.org/library 8/31/18 Functions 15
Recommend
More recommend