Lecture 5: Booleans and Conditionals
Craig Zilles (Computer Science) February 23, 2020 https://go.illinois.edu/cs105sp19
CS 105 Lecture 5: Booleans and Conditionals Craig Zilles (Computer - - PowerPoint PPT Presentation
CS 105 Lecture 5: Booleans and Conditionals Craig Zilles (Computer Science) https://go.illinois.edu/cs105sp19 February 23, 2020 To Today 1. Booleans 2. Simple conditionals if, else, elif 3. Boolean Expressions and Operators
Craig Zilles (Computer Science) February 23, 2020 https://go.illinois.edu/cs105sp19
2
them, why they're used, etc.
3
4
x = 1 if x < 7: print(x) print(7)
5
What does this code print? a) 1 b) 7 c) 1 7 a) Something else b) An error occurs
"I am confused when to use indentation" "I was a bit confused on code blocks. Is hitting the tab key equivilent to hitting the space bar 3 or 4 times? Does it matter which one you use?" "What happens if you mix tabs and spaces in your code?"
6
x = 2 if x > 8: x = x - 2 print(x) else: print(8)
7
What does this code print? a) 0 b) 8 c) 0 8 a) Something else b) An error occurs
x = 1 if x < 8: print('less than 8') elif x > 20: print('greater than 20') else: print('from 8 to 20') Include as many elifs as you want, between if and else
8
between the two? When do you use ==, and =?
9
Suppose young is a variable with a Boolean value why does if young == true: not work when if young: does?
10
letters?
thing1.lower() < thing2.lower()
11
if "hello":
12
grade = 98 if (grade >= 90): print(“You got an A!”) if (grade >= 80): print(“You got a B!”) else: print(“You got something else”)
13
A) You got an A! B) You got a B! C) You got something else D) More than one of the above
grade = 98 if (grade >= 90): print(“You got an A!”) if (grade >= 80 and grade < 90): print(“You got a B!”) else: print(“You got something else”)
14
A) You got an A! B) You got a B! C) You got something else D) More than one of the above
with this concept.
then you can go out."
and
not
15
when it has both Boolean operators and other kinds of
'and' or 'or' to a symbol, it is hard to tell which one i should evaluate first.
x == 7 and y == 3 or x > 12 and y < -12
16
17
18
def welcome_message(first, last): message = "Welcome " + first + " " + last message += " to CS 105!" return message msg = welcome_message("Harry", "Potter")
19
def test(num): if num > 0: return True return False A) True B) False C) first True and then False D) the tuple (True, False) E) an error occurs
20
def f(x): return 3 * x What value is returned by f(f(2))? A) 3 B) 6 C) 9 D) 12 E) 18
21
when you don't have the return statement.
function to return the value of "none"?
something like this by accident: x = print("hi there!")
22
my_list = [0, 1, 2, 3] my_list.append(22)
len(my_list)
23
def add_one(x): return x + 1 x = 2 x = x + add_one(x) A) No bugs. The code is fine. B) The function body is not indented. C) We use x as both a parameter and a variable, but we are not allowed to do that D) Both B and C
24
True or anything() is True False and anything() is False
(len(my_str) > 10) and (my_str[10] == 'a')
25
26
hello there hello there
print('hello') and print('there')
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.
27
Asked my TA to send email to all students in the class that didn't take Exam 0.
if student in exam0_takers: send_email(student)
28
Company 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)
29
Final 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'
30
If you were choosing between 6 possibilities, what is the fewest elif statements you coud have: A) 1 B) 2 C) 3 D) 4 E) 5
31
32
When to use elif and when to use else? I think there should be
if sales_type == 2: if sales_bonus < 5: sales_bonus = 10 else: sales_bonus = sales_bonus + 2 else: sales_bonus = sales_bonus + 1 Can I change the first 'else' into elif?
33
for val in ['good', '105', 'class']: print(val)
range(6) # -> [0, 1, 2, 3, 4, 5]
34
few lines
testing any of it
bug
35