1 of 26

CSE 160 Section

Welcome :)

2 of 26

Logistics

Due Friday 10/13: Activity #2

Due Monday 10/16: HW2 (DNA Analysis)

  • Parsing data, loops, data analysis

3 of 26

Lecture Key Points Review

4 of 26

Conditionals: structure review

  • Checks that a condition is True/False, and executing code based on that condition

is_raining = False

is_sprinkling = True

if is_raining:

print(“Bring an umbrella”)

elif is_sprinkling:

print(“Bring a raincoat”)

else:

print(“Bring sunglasses”)

5 of 26

Conditionals: “Boolean Zen”

  • Minimize the use of if statements where possible!
    • More readable
    • Easier to debug

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:

6 of 26

Functions: an overview

  • A function is a block of code which only runs when it is called.
  • You can pass data, known as parameters, into a function.
  • A function can return data as a result (but it doesn’t have to)

7 of 26

Why do we use functions?

  • Don’t Repeat Yourself (DRY)
    • If you’re doing the same thing over and over again, it might be easier to make it into a function
  • Abstractions
    • It’s easier to call a function (like sqrt() or sum()) than writing all the code
    • It’s easier to reason about your program when you break it down into smaller chunks

8 of 26

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)

9 of 26

Functions run only when called

  • What would be the output of this program?

def winter():

x = “Happy Winter!”

print(x)

Output:

def winter():

x = “Happy Winter!”

print(x)

winter()

Output:

Happy Winter!

10 of 26

Functions run only when called

  • What would be the output of this program?

def winter():

x = “Happy Winter!”

print(x)

Output:

def winter():

x = “Happy Winter!”

print(x)

winter()

Output:

Happy Winter!

Include parentheses in function call

11 of 26

Parameters

  • We can pass a function a value to work with, called a “parameter”

def print_twice(x):

print(x)

print(x)

print_twice(“Hi!”)

Output:

Hi!

Hi!

12 of 26

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

13 of 26

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

14 of 26

Docstrings

  • You should comment your functions so others (and yourself) can understand what they do without having to read through the code. You should include:
    • What are the inputs? What types should they be?
    • What does it do (does it print something?)
    • What does it return?

def abs(x):

‘’’Takes in a number (x), and returns it’s absolute value’’’

if x < 0:

return x * -1

else:

return x

15 of 26

Week 2 Review

16 of 26

Data Type

  1. Evaluate each expression as if you were to enter it into a Python interpreter. What is the data type (e.g., integer, string) of each resulting value?

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

17 of 26

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)

18 of 26

If and Loops

19 of 26

If and Loops

20 of 26

Section Handout Problems

  • We won’t get through all problems, but solutions will be posted!
  • Great practice - go to OH if you have questions!

21 of 26

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.

22 of 26

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.

  1. What is the return type of max_height(student_lst)?�
  2. Suppose you modified your function to print the max height instead of return the max height, what would be the return type of max_height(students)?

PythonTutor

23 of 26

Problem 3 Solutions

  1. int�
  2. None

24 of 26

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”.

Python Tutor

25 of 26

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.

PythonTutor

26 of 26

Thank you!

  • Start HW 2 right away!
  • Come visit in OH ;)