L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not...
Transcript of L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not...
![Page 1: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/1.jpg)
Lecture 4: Functions and Conditionals
Craig Zilles (Computer Science)
September 20, 2019
https://go.illinois.edu/cs105fa19
CS 105
![Page 2: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/2.jpg)
Are you sitting next to someone to talk to for the clicker
questions?
Today I'm using: pythontutor.com
2
![Page 3: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/3.jpg)
Big Picture (Muddiest Points)
• if the challenge questions are too hard can we leave some of it?
• In general, I'd say that there is no single concept that confuses me most as of right now. I kind of understand why I need to do what I need to do in coding, but I am still foggy as to where each and every little character goes, and what makes something a syntax error and what doesn't.
3
![Page 4: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/4.jpg)
Today1. Warmup2. Functions
• Indentation / Code Blocks• Parameters and Arguments• Return Values
3. Boolean Expressions• Relational operators: ==, !=, <, >, <=, >=• Boolean operators: and, or, not
4. Conditionals• if, elif, else• Nesting
4
![Page 5: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/5.jpg)
Negative Indexing
What is the value of the above expression?
A) 'a'B) 'b'C) 'c'D) 'd'E) 'e'
5
"abcde"[-2]
![Page 6: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/6.jpg)
User-defined Functionsusing recipes in recipes
6
![Page 7: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/7.jpg)
User-defined Functions• Sequences of operations for use elsewhere in program• Function definition says what to do
def get_input_and_print():name = input("Your name?\n")print("Hello " + name + "!")
• Function calls/invocations actually run the function
get_input_and_print()
7
![Page 8: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/8.jpg)
Representative Muddiest Points
• The sections on parameters and returns made no sense to me. I don't understand when to indent something, or what to return and when.
• I feel like I do not understand what code block and indentations stand for in Python.
8
![Page 9: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/9.jpg)
Code Blocks• Need a way to tell Python "this statements are related"• Python uses indentation
9
Code Block A
Code Block B(Execution determined by control flow construct)
Control flow construct:
Code Block C(Same indentation as A)
![Page 10: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/10.jpg)
Indentation• In other prog. languages, indentation is just good style• In Python, it is syntactic and semantic
• These three programs are all different• Text is same, white space and behavior is different
10
def test():print('first')print('second')
test()
def test():print('first')
print('second')
test()
def test():print('first')print('second')
test()
![Page 11: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/11.jpg)
What does this program output?
• A) it raises an error
• B)
• C)
• D)
• E)
11
first
firstsecond
secondfirst
firstsecond
def test():print('first')
print('second')
test()
![Page 12: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/12.jpg)
Parameters, Arguments, Return Values
def welcome_message(first, last):
message = "Welcome " + first + " " + last
message += " to CS 105!"
return message
welcome_message("Harry", "Potter")
12
![Page 13: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/13.jpg)
Function Compositiondef f(x):
return 3 * x
What value is returned by f(f(2))?A) 3B) 6C) 9D) 12E) 18
13
![Page 14: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/14.jpg)
None• I don't understand the value of None, and what it means
when you don't have the return statement.• Would there ever be a time that we would need a
function to return the value of "none"?
• Mostly this is important to know because you might do something like this by accident:
x = print("hi there!")
14
![Page 15: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/15.jpg)
Functions vs. Methods• Methods are functions that are part of an object/type• They use dot notation• For example:
my_list = [0, 1, 2, 3]my_list.append(22)
• Functions, in contrast:len(my_list)
15
![Page 16: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/16.jpg)
Announcements• Quiz 1 next week
• Taken at home on PrairieLearn• To be done Alone!
16
![Page 17: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/17.jpg)
How much total time did you spend in the past week on CS 105?• Lecture + Lab = 3 hours• Readings + Preflights + HW + Office hours + Exam
• Which of these Boolean expressions are True for youA) hours_spent < 6 hoursB) 6 hours <= hours_spent < 9 hoursC) 9 hours <= hours_spent < 11 hoursD) 11 hours <= hours_spent < 13 hoursE) hours_spent >= 13 hours
17
![Page 18: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/18.jpg)
How was Exam 0 for you?• A) Great, no problems at all!• B) Fine• C) Whatever• D) Not a fan• E) Terrible, I hated it!
18
![Page 19: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/19.jpg)
Exam 0• Seemed to go pretty smoothly
19
![Page 20: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/20.jpg)
Booleans• I don't understand what Booleans are and how to use
them, why they're used, etc.
• There are two Boolean values: True, False
• Used for making decisions:• Do something or don't do something
20
![Page 21: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/21.jpg)
Boolean Expressions• Expressions that evaluate to True or False
(1 + 6) < (2 + 5)
• A) True• B) False• C) Raises an error
21
![Page 22: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/22.jpg)
Relational and membership operators
• When to use = and when use == ?
22
![Page 23: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/23.jpg)
Suppose young is a Boolean variable
why doesif young == true:not work whenif youngdoes?
23
![Page 24: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/24.jpg)
Relational Ops on non-numbers• Why lower case letters are greater than upper case
letters?
• People often normalize case before comparisons
thing1.lower() < thing2.lower()
24
![Page 25: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/25.jpg)
Truthy and Falsy• Python will convert non-Boolean types to Booleans
if "hello":• You can force conversion using bool() function.• All values are truthy (convert to True) except:
25
Falsy
valu
es
![Page 26: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/26.jpg)
Boolean operators• Why is x==3 or 4 always True? I am still confused
with this concept.
• "If you've finished your homework and done your chores then you can go out."
• Binary operators: and or• Unary operator: not
• Operate on Booleans (or coerce to Booleans)
26
![Page 27: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/27.jpg)
Precedence• I'm still confused about the sequence of the operations
when it has both Boolean operators and other kinds of operators.
• Order of evaluation is confusing. When I was comparing 'and' or 'or' to a symbol, it is hard to tell which one ishould evaluate first.
• Relational operators evaluate before Boolean ops.• and evaluates before orx == 7 and y == 3 or x > 12 and y < -12• Avoid relying on operator precedence; use parentheses
27
![Page 28: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/28.jpg)
x==3 or 4
28
![Page 29: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/29.jpg)
Short Circuiting• i am confused about the concept of short circuit
• Python is lazy (which is a good thing if you understand it)• It won't evaluate Boolean expressions it doesn't need to
True or anything() is TrueFalse and anything() is False
• Python won't evaluate the anything() part• Can use this to avoid running code that would get errors
(len(my_str) > 10) and (my_str[10] == 'a')
29
![Page 30: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/30.jpg)
What does this program output?
• A) it raises an error
• B)
• C)
• D)
• E) it prints nothing, but raises no errors
30
hello
there
hellothere
print('hello') and print('there')
![Page 31: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/31.jpg)
Conditionals• if, elif, else• I'm confused about when to use multiple distinct if
statements or if-elif-else.• I'm not too sure about the concept of "elif"
• I don't understand how each of the components of the chapter can be used in real world cases. A thing I like to do to help me better grasp the concepts, is imagine them happening in this world. So giving me more mundane scenarios of where we would be using these things would help a lot.
31
![Page 32: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/32.jpg)
Shape of "decision tree": if w/o elseAsked my TA to send email to all students in the class that didn't take Exam 0.• Step 1: make a set of all students that took Exam 0• Step 2: check each student in class if in the set
if student in exam0_takers:send_email(student)
32
![Page 33: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/33.jpg)
Shape of "decision tree": if w/elseCompany sends recruiting invitations to students with Python in their resume, sends 'nack' email to others
if 'python' in resume.lower():send_invitation(student)
else:send_polite_decline(student)
33
![Page 34: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/34.jpg)
Choosing between many alternativesFinal exam location based on first letter of netid:
[a-j] Loomis 100[k-o] DCL 1320[p-z] English 214
first_char = netid.lower()[0]if first_char <= 'j':
location = 'Loomis 100'elif first_char <= o:
location = 'DCL 1320'else:
location = 'English 214'
34
![Page 35: L4 Conditionals - GitHub...then you can go out." •Binary operators: and or •Unary operator: not •Operate on Booleans (or coerce to Booleans) 26 Precedence •I'm still confused](https://reader033.fdocuments.us/reader033/viewer/2022050108/5f46872c05ae4645755024db/html5/thumbnails/35.jpg)
Multi-way branches in general?If you were choosing between 6 possibilities, how many elif statements would you have:
A) 1B) 2C) 3D) 4E) 5
35