python – part 3 functions 1. function calls function – a named sequence of statements that...
TRANSCRIPT
![Page 1: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/1.jpg)
1
Prepared by Department of Preparatory year
Python – Part 3
Functions
![Page 2: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/2.jpg)
Prepared by Department of Preparatory year
2
Function Calls
• Function– A named sequence of statements that performs a
computation– Name– Sequence of statements
• “call” function by name>>> type(32)<type ‘int’>– Function name – type– Argument - 32
![Page 3: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/3.jpg)
Prepared by Department of Preparatory year
3
Function Calls
• Commonly “takes” an argument• “returns” a result• Result called the return value.
![Page 4: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/4.jpg)
Prepared by Department of Preparatory year
4
Type conversion functions
• Built-in functions that convert values from one type to another
>>>int (’32’)32>>>int (‘Hello’)ValueError: invalid literal for int(): Hello>>>int (3.9999)3>>> int (-2.3)-2
![Page 5: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/5.jpg)
Prepared by Department of Preparatory year
5
Type conversion functions
>>>float (32)32.0>>>float (‘3.14159’)3.14159>>> str (32)’32’>>> str (3.14159)‘3.14159’
![Page 6: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/6.jpg)
Prepared by Department of Preparatory year
6
Math functions
• Math module – provides most of the familiar mathematical functions
• Module – a file that contains a collection of related functions
![Page 7: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/7.jpg)
Prepared by Department of Preparatory year
7
Math Functions
• Import the module before using it>>> import mathCreates a module object named math>>> print math<module ‘math’ (build-in)>Prints some information about it
![Page 8: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/8.jpg)
Prepared by Department of Preparatory year
8
Math functions
>>> ratio = signal_power / noise_power>>> decibels = 10 * math.log10(ratio) #computes logarithm base 10 of ratio
>>> radians = 0.7 >>> height = math.sin(radians) #computes sine of radians
![Page 9: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/9.jpg)
Prepared by Department of Preparatory year
9
Math functions
>>> degrees=45>>> radians=degrees/360.0*2*math.pi>>>math.sin(radians)0.707106781187
>>>math.sqrt(2)/2.00.707106781187
![Page 10: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/10.jpg)
Prepared by Department of Preparatory year
10
Composition
• Can use functions to compose more complex expressions
x=math.sin(degrees/360.0*2*math.pi)
>>>minutes=hours*60 #right>>>hours*60=minutes #wrong!SyntaxError: can’t assign to operator
![Page 11: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/11.jpg)
Prepared by Department of Preparatory year
11
Adding new functions
• Function definition– Name of a new function– Sequence of statements that execute when
function is called
def print_lyrics(): print ("I'm a lumberjack, and I'm okay.“) print ("I sleep all night and I work all day." )
![Page 12: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/12.jpg)
Prepared by Department of Preparatory year
12
Adding new functions
• def – keyword for function definition• print_lyrics – name of the function• () – no arguments
• Function name –same rules as for variables• Avoid using variable and function with same
name
![Page 13: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/13.jpg)
Prepared by Department of Preparatory year
13
Adding new functions
• Header – first line of the function definition– Ends in colon
• Body – the rest– Has to be indented (always four spaces)
• Empty line to end the function (not necessary in a script)
![Page 14: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/14.jpg)
Prepared by Department of Preparatory year
14
Adding new functions
• Defining a function creates a variable with the same name
>>> print_lyrics()
def repeat_lyrics(): print_lyrics() print_lyrics()
![Page 15: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/15.jpg)
Prepared by Department of Preparatory year
15
Parameters and Arguments
• math.pow(2,3)• 8.0
• def print_twice(bruce):• print bruce• print bruce
• Assigns the argument to a parameter named bruce
![Page 16: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/16.jpg)
Prepared by Department of Preparatory year
16
Parameters and Arguments
>>>print_twice (‘spam’)spamspam
>>>print_twice(17)1717
![Page 17: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/17.jpg)
Prepared by Department of Preparatory year
17
Parameters and Arguments
>>>print_twice(‘spam’*2)spam spamspam spam
Argument evaluated before function is called
![Page 18: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/18.jpg)
Prepared by Department of Preparatory year
18
Parameters and Arguments
• Can also use a variable as an argument
>>>number=17>>>print_twice(number)1717
![Page 19: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/19.jpg)
Prepared by Department of Preparatory year
19
Local variables and parameters
• Variable inside a function is local• Exists only inside the function• def cat_twice(part1, part2):• cat = part1 + part2 • print_twice(cat)
• This function takes two arguments, concatenates them, and prints the result twice.
![Page 20: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/20.jpg)
Prepared by Department of Preparatory year
20
Local variables (cont’d…)
>>> line1 = 'Bing‘>>> line2 = 'bang.' >>> cat_twice(line1, line2) Bing bang. Bing bang.
![Page 21: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/21.jpg)
Prepared by Department of Preparatory year
21
Local variables (cont’d …)
When cat_twice terminates, the variable cat is destroyed. If we try to print it, we get an exception:
• >>> print cat NameError: name 'cat' is not defined
![Page 22: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/22.jpg)
Prepared by Department of Preparatory year
22
Void functions
• print_twice • Perform and action but does not return a
value
• If function returns value, almost always use it as part of an expression:
• x = math.cos(radians) • golden = (math.sqrt(5) + 1) / 2
![Page 23: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/23.jpg)
Prepared by Department of Preparatory year
23
Void functions
• >>> result = print_twice('Bing')• Bing • Bing • >>> print result • None
![Page 24: Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”](https://reader035.vdocuments.us/reader035/viewer/2022062309/56649ea35503460f94ba729c/html5/thumbnails/24.jpg)
24
Prepared by Department of Preparatory year
Part 3
End