lec3_func_module_files
lec3_func_module_files
2
Today
• structuring programs
• functions
• local variables and scope
• modules
• file I/O
3
Functions
4
5
Functions
A function is a group of statements that exist within
a program for the purpose of performing a specific
task.
6
Benefits of modularizing a program with functions
7
Defining a function
Function names:
• conform to the same rules as to variable names:
• better be descriptive of the task carried out by the
function, e.g. get_input()
8
Calling a function
9
The main() function
• main() is called when program starts
• It calls other functions when needed
• It defines the mainline logic of the program
10
Passing an argument to the function
• Argument: piece of data that is sent into a function
• It’s placed in parentheses following the function name
11
Passing multiple arguments
• A function can accept multiple arguments
• Arguments are passed by position to corresponding parameters
12
Passing multiple arguments
??? 13
Writing your own value-returning functions
• Format:
• Examples:
def sum(num1, num2):
def sum(num1, num2):
result = num1 + num2
return num1 + num2
return result
14
Writing your own value-returning functions
Example: prompt user for input and return the user’s input
15
Writing your own value-returning functions
Example: Return Boolean values
16
Writing your own value-returning functions
Example: Return Boolean values
17
Returning multiple values
• A function can return multiple values
18
return vs print()
• return only has meaning • print() can be used inside or
inside a function outside a function
• only one return will be • many print() can be
executed inside a function executed inside a function
• code inside a function but
after the return statement • code inside a function can
will not be executed be executed after print()
• return has a value • print has a value associated
associated with it, which with it, which will be output
will be given to the function to the console
caller
19
Exercise 1
• return vs print
How many lines of output will show up if you run
the code below?
def main():
add(1, 2)
print(add(2, 3))
mult(3, 4)
main()
20
Exercise
Get a sequence from the user. If there is an A in the sequence, print
the number of times it appears in the sequence. Do the same for T,
C and G. If a base does not exist, don’t print anything.
22
Exercise (Solution)
def main():
input_seq = "ATGCATGATGCATGAAAGGTCG"
results = count_bases(input_seq)
for base in results:
print(f"{base} count: {results[base]}")
def count_bases(seq):
counts = {}
for base in seq:
if base not in counts:
counts[base] = 1
else:
counts[base] = counts[base] + 1
return counts
main()
23
Local variable and scope
24
Local variable and scope
• A local variable is created inside a function
• Cannot be accessed outside the function
def main(): main() scope f() scope
x = 3
z = f(x) x 3 x 4
3
print(“in main(): x =”, x )
z
Returns 4
def f(x):
x = x + 1
print(“in f(x): x =”, x)
return x main() scope
x 3
main()
z 4
25
Local variable and scope
• A local variable is created inside a function
• Cannot be accessed outside the function
26
Local variables and scope
What is printed?
27
Modules
28
Storing functions in a module
• A module is a file that contains Python code
• Contains function definitions but usually does not
contain code that calls those functions
• Rules for module names:
• File name should end in .py
• Cannot be the same as a Python keyword
• Import module in another script using import
statement
• e.g. import my-script.py
29
Storing functions in a module
The module file contains function definitions, but not code that calls the functions.
30
Exercise: Fill in the blanks in geometry-w_blanks.py
31
Exercise: Fill in the blanks in geometry-w_blanks.py
32
File I/O
33
Writing data to a file
34
Alternatively, use the with statement
35
Reading data from a file
36
General logic of reading a file
37
Read a file using while-loop
1000.00
2000.00
3000.00
4000.00
5000.00
38
Alternatively, use for-loop to read a
file
39
Reading Materials
• Chapters 5 & 6, Starting out with Python 4/e.
• good for beginners
40