CSE 160 Section
Welcome :)
Logistics
Due Friday 10/13: Activity #2
Due Monday 10/16: HW2 (DNA Analysis)
Lecture Key Points Review
Conditionals: structure review
is_raining = False
is_sprinkling = True
if is_raining:
print(“Bring an umbrella”)
elif is_sprinkling:
print(“Bring a raincoat”)
else:
print(“Bring sunglasses”)
Conditionals: “Boolean Zen”
num = 0
def is_num_2(num):
if num == 2:
return True
else:
return False
num = 0
def is_num_2(num):
return (num == 2)
Without boolean zen:
With boolean zen:
Functions: an overview
Why do we use functions?
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) |
Functions run only when called
def winter():
x = “Happy Winter!”
print(x)
Output:
def winter():
x = “Happy Winter!”
print(x)
winter()
Output:
Happy Winter!
Functions run only when called
def winter():
x = “Happy Winter!”
print(x)
Output:
def winter():
x = “Happy Winter!”
print(x)
winter()
Output:
Happy Winter!
Include parentheses in function call
Parameters
def print_twice(x):
print(x)
print(x)
print_twice(“Hi!”)
Output:
Hi!
Hi!
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
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
Docstrings
def abs(x):
‘’’Takes in a number (x), and returns it’s absolute value’’’
if x < 0:
return x * -1
else:
return x
Week 2 Review
Data Type
a. 42
b. 42 + 91 / 3.0
c. 42 / 5 + 2
d. True
e. 42 < 45
f. not 42 < 91
g. "May the force be with you."
h. float(3) < 9
Data Type
Solutions:
a. 42 | a. 42 integer |
b. 42 + 91 / 3.0 | b. 72.333 float |
c. 42 / 5 + 2 | c. 10.4 float |
d. True | d. True bool (or truth value) |
e. 42 < 45 | e. True bool (or truth value) |
f. not 42 < 91 | f. False bool (or truth value) |
g. "May the force be with you." | g. "May the force be with you." string |
h. float(3) < 9 | h. True bool (or truth value) |
If and Loops
If and Loops
Section Handout Problems
Problem 1
Write a function odd(num) that returns True if a number is odd and False if a number is even. Your function should take in an integer num and return a boolean.
Problem 3
Given a function get_height(student) that computes the height of the student passed in, write a new function max_height(student_lst) that finds the maximum height of all the people in the class. Your function should take in a list of student names and return the maximum height. You can assume height is in inches. For example, get_height(‘nicholas’) will return 75.
Problem 3 Solutions
Problem 5
Write a function called budget_saver(cost, budget) that takes the cost of a product and a budget. The function should return “too expensive” if the price is more than the budget, “great deal” if the price is less than the budget, and “okay” if the cost and budget are equal.
For example, budget_saver(250, 100) returns “too expensive”.
Problem 6
Write a function called among_us(crewmates, imposter), where given a list of crewmates and the names of an imposter, returns True if the name of that imposter is in the list of crewmates. Do not use the Python keyword “in”. For example, among_us([“cyan”, “yellow”, “pink”], “pink”) returns True.
Thank you!