![Page 1: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/1.jpg)
Computational Structures in Data Science
UC Berkeley EECSLecturer
Michael Ball
UC Berkeley | Computer Science 88 | Michael Ball
Lecture 3:Loops and Functions
![Page 2: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/2.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Announcements• Attend Labs! • No class Monday. J
2
![Page 3: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/3.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Let’s Talk About Python• Expression 3.1 * 2.6
• Call expression max(0, x)
• Variables• Assignment Statement x = <expression>
• Define Function: def <function name> (<parameter list>):
• Control Statements: if … while …
3
![Page 4: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/4.jpg)
Computational Structures in Data Science
UC Berkeley EECSLecturer
Michael Ball
UC Berkeley | Computer Science 88 | Michael Ball
Python: Definitions and Control
![Page 5: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/5.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Learning Objectives• Create your own functions.• Write a loop to run the same code multiple times• Use conditionals to control when a loop stops
5
![Page 6: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/6.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Conditional Statement• Do some statements, conditional on a predicate expression
• Example:
6
if <predicate>:<true statements>
else:<false statements>
if (temperature>37.2):print(“fever!”)
else:print(“no fever”)
![Page 7: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/7.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Defining Functions
• Abstracts an expression or set of statements to apply to lots of instances of the problem
• A function should do one thing well
7
expression
def <function name> (<argument list>) :
return
![Page 8: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/8.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Functions: Example
8
![Page 9: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/9.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
How to Write a Good Function• Give a descriptive name
– Function names should be lowercase. If necessary, separate words by underscores to improve readability. Names are extremely suggestive!
• Chose meaningful parameter names– Again, names are extremely suggestive.
• Write the docstring to explain what it does– What does the function return? What are corner cases for parameters?
• Write doctest to show what it should do– Before you write the implementation.
9
Python Style Guide: https://www.python.org/dev/peps/pep-0008
![Page 10: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/10.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Functions: Calling and Returning Results
10
Python Tutor
def max(x, y):return x if x > y else y
x = 3y = 4 + max(17, x + 6) * 0.1z = x / y
![Page 11: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/11.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Doctests• Write the docstring to explain what it does
– What does the function return? What are corner cases for parameters?
• Write doctest to show what it should do– Before you write the implementation.– python3 –m doctest [-v] file.py
11
![Page 12: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/12.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Returns and Values
• All functions always return SOME value.• If you don’t specify return, the value is None.
12
![Page 13: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/13.jpg)
Computational Structures in Data Science
UC Berkeley EECSLecturer
Michael Ball
UC Berkeley | Computer Science 88 | Michael Ball
Iteration with while Loops
![Page 14: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/14.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Learning Objectives• Use a while loop to repeat some task.• Write an expression to control when a while loop stops executing
14
![Page 15: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/15.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
while Statement – Iteration Control• Repeat a block of statements until a predicate expression is satisfied
15
<initialization statements>
while <predicate expression>:<body statements>
<rest of the program>
![Page 16: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/16.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Sum The Numbers• This is a task we'll see many times!
total = 0n = 1while n <= 10:
total += nn += 1
print(total)
![Page 17: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/17.jpg)
Computational Structures in Data Science
UC Berkeley EECSLecturer
Michael Ball
UC Berkeley | Computer Science 88 | Michael Ball
Iteration With for Loops
![Page 18: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/18.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Learning Objectives• Compare a for loop and a while loop.• Learn to use range()• Use a string as a sequence of letters
18
![Page 19: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/19.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
for Statement – Iteration Control• Repeat a block of statements for a structured sequence of variable
bindings<initialization statements>
for <variables> in <sequence expression>:<body statements>
<rest of the program>
![Page 20: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/20.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
<sequence expression> — What's that?• Sequences are a type of data that can broken down into smaller parts.• Common sequences:
– range() – gimme all the numbers– strings– lists (next week!)
• We'll start with two basic facts:– range(10) is the numbers 0 to 9, or range(0, 10)– [] means "indexing" an item in a sequence.– "Hello"[0] == "H"
20
![Page 21: UC Berkeley EECS Lecture 3: Lecturer Loops and Functions](https://reader031.vdocuments.site/reader031/viewer/2022012412/616c545f8fc44a62f736924d/html5/thumbnails/21.jpg)
UC Berkeley | Computer Science 88 | Michael Ball
Data-Driven Iteration• describe an expression to perform on each item in a sequence• let the data dictate the control
21
[ <expr with loop var> for <loop var> in <sequence expr > ]