CSE 160 Section 3
Functions, Lists, & Nested Lists
TA & TA
CSE 160
CSE 160: Section 3
Logistics
CSE 160: Section 3
Lecture Review: Functions
CSE 160: Section 3
Functions: an overview
CSE 160: Section 3
Why do we use functions?
CSE 160: Section 3
Function Syntax
# Writing a function
def func_name(parameter1,...):
function body
return value
# To call that function:
func_name(parameter1,...)
def | word needed to define a function |
func_name | a name you give the function |
parameter1, | a list of parameters you pass in a function. (Optional) |
function body | the code inside the function |
return | tells the function to return value back to the caller. (Optional) |
CSE 160: Section 3
Functions run only when called
def winter():
x = “Happy Winter!”
print(x)
Output:
def fall():
x = “Happy Fall!”
print(x)
fall()
Output:
Happy Fall!
CSE 160: Section 3
Functions run only when called
def winter():
x = “Happy Winter!”
print(x)
Output:
def fall():
x = “Happy Fall!”
print(x)
fall()
Output:
Happy Fall!
Include parentheses in function call
CSE 160: Section 3
Parameters
def print_twice(x):
print(x)
print(x)
print_twice(“Hi!”)
Output:
Hi!
Hi!
CSE 160: Section 3
Return Value
This function does have a return value, what will this print?
def two_times_seven():
x = 2 * 7
return x
print(two_times_seven())
Output:
14
CSE 160: Section 3
Return Value
This function does have a return value, what will this print?
def two_times_seven():
x = 2 * 7
return x
print(two_times_seven())
Output:
14
You must have a return statement if you want your function call to have access to x in two_times_seven()! Otherwise, it will return None
CSE 160: Section 3
Return vs Print in Functions
Function with a return
Function with a print statement
Output: ?
Can you see the difference?
def add(a, b):
return a + b
def add_print(a, b):
print(a + b)
CSE 160: Section 3
Function with a Return
Function with a return
def add(a, b):
return a + b
print(add(1,2))
Output:
3
CSE 160: Section 3
Function with a Print
Function with a print statement
print(add_print(1,2))
Output:
3
None
def add_print(a, b):
print(a + b)
CSE 160: Section 3
Docstrings
def abs(x):
‘’’Takes in a number (x), and returns its absolute value’’’
if x < 0:
return x * -1
else:
return x
CSE 160: Section 3
Mod Operator Overview
CSE 160: Section 3
Mod
Modulo operator, denoted as %, is a mathematical operation that calculates the remainder when one number is divided by another.
CSE 160: Section 3
Mod
CSE 160: Section 3
Mod
What is the output of the following code?
for i in range(5):
print(i % 3)
CSE 160: Section 3
Mod
What is the output of the following code?
for i in range(5):
print(i % 3)
Answer:
0
1
2
0
1
CSE 160: Section 3
Lecture Review:
Lists & Nested Lists
CSE 160: Section 3
Basic List Operations
CSE 160: Section 3
Functions that Modify Lists
CSE 160: Section 3
Making a List
listname = [item1, item2, ..., item2]
Example:
lst = [22, 45, 65, 89, 5]
index: | 0 | 1 | 2 | 3 | 4 |
value: | 22 | 45 | 65 | 89 | 5 |
CSE 160: Section 3
Access Items
listname[i] returns the item at index i
Example:
lst = [22, 45, 65, 89, 5]
print(lst[1])
index: | 0 | 1 | 2 | 3 | 4 |
value: | 22 | 45 | 65 | 89 | 5 |
Output:
45
CSE 160: Section 3
List Slicing
(2 parameters)
listname[a : b] returns a new copy of the list in between indexes a and b.
Example:
Output:
lst = [22, 45, 65, 89, 5]
print(lst[1:3])
[45, 65]
index: | 0 | 1 | 2 | 3 | 4 |
value: | 22 | 45 | 65 | 89 | 5 |
CSE 160: Section 3
List Slicing
(3 parameters)
listname[a : b : c] returns a new copy of the list in between indexes a and b with a step size c.
Example:
Output:
lst = [22, 45, 65, 89, 5]
print(lst[0:3:2])
[22, 65]
index: | 0 | 1 | 2 | 3 | 4 |
value: | 22 | 45 | 65 | 89 | 5 |
CSE 160: Section 3
List Slicing (Empty parameters)
lst = [22, 45, 65, 89, 5]
print(lst[:2:3])
print(lst[0::2])
print(lst[::])
Output:
[22]
[22, 65, 5]
[22, 45, 65, 89, 5]
CSE 160: Section 3
Changing Lists
What if we wanted to change a list?
Example:
lst = [22, 45, 65, 89, 5]
lst[1] = 10
index: | 0 | 1 | 2 | 3 | 4 |
value: | 22 | 10 | 65 | 89 | 5 |
CSE 160: Section 3
list + list
list1 + list2 will return a new list that has both of the lists together
Example:
Output:
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
lst3 = lst1 + lst2
print(lst3)
[1, 2, 3, 4, 5, 6]
CSE 160: Section 3
.index()
listname.index(item) returns the first index i, that the item was found at
Example:
lst = [22, 45, 65, 89, 5]
print(lst.index(45))
index: | 0 | 1 | 2 | 3 | 4 |
value: | 22 | 45 | 65 | 89 | 5 |
Output:
1
CSE 160: Section 3
.insert()
listname.insert(i, item) puts the item at index i, and moves everything else to the right, in-place.
Example:
Output:
lst = [22, 45, 65, 89, 5]
lst.insert(4, 999)
print(lst)
[22, 45, 65, 89, 999, 5]
index: | 0 | 1 | 2 | 3 | 4 | 5 |
value: | 22 | 45 | 65 | 89 | 999 | 5 |
CSE 160: Section 3
.extend()
list1.extend(list2) will add list2 to the end of list1
Example:
Output:
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
lst1.extend(lst2)
print(lst1)
[1, 2, 3, 4, 5, 6]
CSE 160: Section 3
.reverse()
listname.reverse() reverses a list
Example:
Output:
lst = [1, 2, 3]
lst.reverse()
print(lst)
[3, 2, 1]
CSE 160: Section 3
.sort()
listname.sort() sorts a list
Example:
Output:
lst = [2, 1, 3]
lst.sort()
print(lst)
[1, 2, 3]
CSE 160: Section 3
Note About In Place Functions
insert(), extend(), reverse(), and sort() are all called in place functions.
Example:
Output:
“In-place” means that the list is modified, but the result it returns is None
lst = [1, 2, 3]
result = lst.reverse()
print(result)
print(lst)
None
[3, 2, 1]
CSE 160: Section 3
Nested Lists
nested[0] → [1, 2]
nested[1] → [3, 4]
nested = [[1, 2], [3, 4], [5, 6]]
print(nested[1][0])
Output: 3
CSE 160: Section 3
Changing Elements in a Nested List
heights = [["Alice", 95], ["Bob", 88], ["Catherine", 76]]
print(heights[2][1])
heights[1][1] = 90
CSE 160: Section 3
Looping Through a Nested List
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in matrix:
for item in row:
print(item)
Output: 1
2
…
CSE 160: Section 3
Section Handout Problems
CSE 160: Section 3
Problem 1
Write a function avg_age(ages) that calculates and returns the average of a list of ages. You are not allowed to use Python’s built in sum() function. Your function should take the list ages as a parameter and return the average. For example, given ages = [20, 21, 22], avg_age(ages) should return 21.
CSE 160: Section 3
Problem 2
Write a function called count_character(word, search), where given a string called word, return the number of times the character called search appears.
For example, count_character(“apple pie”, “p”) return 3.
CSE 160: Section 3
Problem 3
What values would be printed when you run the following lines of code?
list_1 = [1, 2, 3, 4, 5]
list_2 = list_1
list_3 = list_1[:] # equivalent to list_1[0:5]
list_2[0] = 98
list_1[4] = 99
print("List 1:", list_1)
print("List 2:", list_2)
print("List 3:", list_3)
CSE 160: Section 3
Problem 4
Write a function called count(lst, value) that takes in a list and a value. It should return the number of times the value is in the list. For example: if my_lst = [1, 3, 5, 3, 2, 5, 6, 3, 3] when you call count(my_lst, 3) it should return 4.
CSE 160: Section 3
Additional Problems
CSE 160: Section 3
Problem 5
Given a list of students’ names (student_lst), write a function max_height(student_lst) that returns the maximum height in inches. Assume a function get_height(student) is given to you, and will return the height (in inches) of any student when given a student name.
For example: get_height(“Nicholas”) will return 75.
a) Implement the function max_height(student_lst)
b) What is the return type of max_height(student_lst)?
c) Suppose you printed the max height instead of returning it. What would be the return
type of max_height(student_lst)
CSE 160: Section 3
Problem 6 Dot Product
Create a function "dot_product" which takes in two lists of integers and returns the dot product of the two lists. You can assume the lists are of equal length, and contain only integer values.
So if these were your two lists:
The dot product would be:
index: | 0 | 1 |
value: | 1 | 2 |
index: | 0 | 1 |
value: | 3 | 4 |
CSE 160: Section 3
Problem 6 Dot Product
Create a function "dot_product" which takes in two lists of integers and returns the dot product of the two lists. You can assume the lists are of equal length, and contain only integer values.
So if these were your two lists:
The dot product would be:
1*3
index: | 0 | 1 |
value: | 1 | 2 |
index: | 0 | 1 |
value: | 3 | 4 |
CSE 160: Section 3
Problem 6 Dot Product
Create a function "dot_product" which takes in two lists of integers and returns the dot product of the two lists. You can assume the lists are of equal length, and contain only integer values.
So if these were your two lists:
The dot product would be:
1*3 + 4*2
index: | 0 | 1 |
value: | 1 | 2 |
index: | 0 | 1 |
value: | 3 | 4 |
CSE 160: Section 3
Problem 6 Dot Product
Create a function "dot_product" which takes in two lists of integers and returns the dot product of the two lists. You can assume the lists are of equal length, and contain only integer values.
So if these were your two lists:
The dot product would be:
1*3 + 4*2 = 11 Python Tutor
index: | 0 | 1 |
value: | 1 | 2 |
index: | 0 | 1 |
value: | 3 | 4 |
CSE 160: Section 3
Section Code: �GoodLuck
CSE 160: Section 3
Looking ahead: �Advanced Data Structures
Be sure to remember the Section Code!
Want more information on Basic List Operations and Functions that Modify Lists? See the skipped slides in the deck when its posted!
CSE 160: Section 3