Download - CS 177 Week 5 Recitation
![Page 1: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/1.jpg)
1
CS 177 Week 5 Recitation
Booleans and Control Flow
![Page 2: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/2.jpg)
2
Announcements
![Page 3: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/3.jpg)
3
Contents
Booleans Decision structures While loop Project 2 preview
![Page 4: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/4.jpg)
Booleans
Boolean (logical) expressions: An expression that can assume only the true or false value We use logical expression in everyday language:
If today is raining, then bring an umbrella when you go out.
“today is raining” is a logical expression: its value can be either true or false.
Other examples: assume x=4 assume str=“abc”
x>3 type(str)==int (true) (false)
![Page 5: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/5.jpg)
Boolean operators: And, or, not
a and b
a and true
x > 0 and x <=2
y > 0 and y >= 3(overlapped)
Booleans
P Q P and Q
T T T
T F F
F T F
F F F
![Page 6: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/6.jpg)
Boolean operators: And, or, not
a or b
a or true
x <= 0 or x > 2
x > 5 or x < 10 (always true)
Booleans
P Q P or Q
T T T
T F T
F T T
F F F
![Page 7: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/7.jpg)
Boolean operators: And, or, not
not a
not (not a)
not x > 3
DeMorgan’s law
not (a or b) == (not a) and (not b)
not (a and b) == (not a) or (not b)
Booleans
P not P
T F
F T
![Page 8: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/8.jpg)
(P and (not Q)) or ((not P) and Q) It has a name: XOR Can you do this?
Booleans
P Q P xor Q
T T
T F
F T
F F
![Page 9: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/9.jpg)
Does the result look familiar? How about this: Let T=1, F=0
Yes, it is the sum of binary numbers
Booleans
P Q P xor Q
1 1 0
1 0 1
0 1 1
0 0 0
![Page 10: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/10.jpg)
If statement An if statement takes a logical expression and evaluates it. If it is true, the statements in if block are executed, otherwise,
they are not executed.
Simple decision
Decision Structures
x = 5if x>1: print(“print something”) The string is printed
x = 0if x>1: print(“print something”) Does nothing!
![Page 11: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/11.jpg)
Two-way decision
Decision Structures
a = 45if a < 100: print(“a is small”)else: print(“a is large”)
>>> a is small
a = 153if a < 100: print(“a is small”)else: print(“a is large”)
>>> a is large
![Page 12: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/12.jpg)
Two-way decision
Decision Structures
a < 100?
“a is large” “a is small”
no yes
![Page 13: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/13.jpg)
Multi-way decision
Decision Structures
a = 1.5if a > 2: print(“a>2”)else: if a > 1: print(“1<a<=2”) else: print(“a<=1”)
>>> 1<a<=2
a = 1.5if a > 2: print(“a>2”)elif a > 1: print(“1<a<=2”)else: print(“a<=1”)
>>> 1<a<=2
![Page 14: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/14.jpg)
Decision Structures
Multi-way decision
14
a >2 ?
“a>2”
no yes
a >1 ?
“1<a<=2”“a<1”
no yes
![Page 15: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/15.jpg)
“How long do I have to shower?” You have two replies you could make: a) 10 minutes b) Until you are clean
a) When programming, the first answer would be portrayed as a for-loop because we focus on exactly how long the shower will continue:
for minutes in range (0,9):shower
b) The second answer would be portrayed as a while-loop because the length of the shower is undetermined; we instead focus on the condition of cleanliness:
while you are not clean:shower
For vs While Loop
![Page 16: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/16.jpg)
For vs While Loop
count = 0 while count < 9: print(“The count is:”,count) count = count + 1
print(“while loop ended”)
for count in range(0,9): print(“The count is:”, count)
print(“for loop ended”)
![Page 17: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/17.jpg)
Rules of While Loops
Command name: while
Boolean condition (in the previous example: count < 9) determines the termination of loop
A colon (“:”)
And a block (the indented lines of code)
17
![Page 18: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/18.jpg)
Interactive loop
Using while loop, we can write interactive loops
18
count = 0str = “Yes” while str == “Yes”: print(“The count is:”,count) count = count + 1str = input(“continue? Yes or No:”)
print(“while loop ended”)
![Page 19: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/19.jpg)
Project 2 Preview
Population Growth Simulation The Geometric growth model
No limitation on the population For loop Show the population in characters
Limitation on the population While loop The limitation is the stopping criterion Show the population in graphs
19
0pp tt
![Page 20: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/20.jpg)
Project 2 Preview
The structure of a project should be like this:
20
Main function
Function 1
Function 2
.
.
.
![Page 21: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/21.jpg)
An Example from the Book
def main(): sing("Fred") print() sing("Lucy")
def sing(person): happy() happy() print("Happy birthday, dear", person + ".“) happy()
def happy(): print("Happy birthday to you!")
21
![Page 22: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/22.jpg)
An Example from the Book
Functions link to each other by their parameters and return values
What does f2(f1()) mean? Pass the function f1 to f2? No Pass the return value of f1 to f2? Yes
v=f1() f2(v)
22
f1 f2
f1 f2parameter return value parameter return value
![Page 23: CS 177 Week 5 Recitation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56812cfd550346895d91d282/html5/thumbnails/23.jpg)
Questions?
23