Comparisons and Control
1
Data 6 Fall 2025
LECTURE 14
Boolean Operators and Compound Expressions
2
Lecture 14, Data 6, Fall 2025
[Warmup] Comparison expressions result in a bool
Let x = 3 and y = 2. In words, what is each expression asking?
3
Comparison Expression | Value |
x == y | False |
x == y + 1 | True |
x != y | True |
2 < x < 5 | True |
y >= x - 1 | True |
y > x - 1 | False |
Boolean operators
Boolean operators allow us to combine the result of multiple booleans.
and and or operate on two (or more) operands; not operates on just one.
4
Operator | Usage | Value |
and | a and b | True if both a and b are True, False otherwise |
or | a or b | True if at least one of a and b are True, False otherwise |
not | not a | True if a is False False if a is True |
(demo)
Analogy, from number lines
5
# 2 < x < 5
(x > 2) and (x < 5)
(x < 2) or (x > 5)
2
5
2
5
Boolean Practice
6
These are summary slides; see the notebook for demo
Lecture 14, Data 6, Fall 2025
Custom Boolean Predicates and Filters
We can write more filters for tables with custom functions.
“Apply-Filter-Drop”:
7
(demo)
Truth Tables
To understand boolean expressions, it is useful to create truth tables to consider how the expression will evaluate with all possible boolean value inputs:
8
x | y | (boolean expression involving x and y) |
False | False | ??? |
False | True | ??? |
True | True | ??? |
False | False | ??? |
(demo)
Also a boolean expression
NO FOOD
OR
DRINK
9
Boolean quirks
10
(demo)
Boolean Short Circuiting
Recall that Python evaluates expressions left to right, after prioritizing parentheses.
When evaluating a boolean expression, Python will “short circuit” (or stop early) when the final result of the boolean expression is known.
1 / 0 # ZeroDivisionError
(1 < 2) or (1 / 0) # True, short circuits
(2 < 2) and (1 / 0) # False, short circuits
11
Conditional Statements (if-statements)
12
Lecture 14, Data 6, Fall 2025
Control your computations with an if statement
x = 2
if x > 0:
print('Positive')
elif x < 0:
print('Negative')
elif x == 0:
print('Neither positive nor negative')
Positive
If you have many conditions, put else at the end!
city = 'St. Petersburg'
if city == 'San Francisco':
print('The Bay!')
elif city == 'Anaheim':
print('Disneyland!')
else:
print('I don’t know where that is...')
'I don’t know where that is...'
We often use if statements inside of functions
if x > 0:
print('Positive')
elif x < 0:
print('Negative')
elif x == 0:
print('Neither positive nor negative')
def sign(x):
(demo)
If Statements: An Example
If statements allow our code to make decisions.
16
Practice, Explained. Let x = 7
if x > 5:
print('somewhat big!')
if x % 2 == 0:
print('and even too!')
else:
print('tiny.')
if x > 5:
print('somewhat big!')
if x % 2 == 0:
print('and even too!')
else:
print('tiny.')
17
somewhat big!
somewhat big!
tiny.
(left-hand-side)
(right-hand-side)
Practice, Explained
18
x > 5?
tiny.
somewhat big!
x % 2 == 0?
and even too!
x > 5?
tiny.
somewhat big!
x % 2 == 0?
and even too!
(left-hand-side)
(right-hand-side)
The right-hand one is two conditional statements
# clearer, with whitespace
if x > 5:
print('somewhat big!')
if x % 2 == 0:
print('and even too!')
else:
print('tiny.')
19
(right-hand-side)
Here’s a recap on if statements!
Here’s a recap on if statements!
Announcements
Project 1 due date tomorrow evening (because of Amazon AWS server outage)
Quiz 2 grades out later today
Soon: a gradebook, so you can see your progress in the course
22
[at home] More on Strings
23
Lecture 14, Data 6, Fall 2025
String Containment
The in keyword allows us to check if one string is a substring of another, or if an element is present in our array/list.
'berkeley' in 'uc berkeley' # True
'stanford' in 'uc berkeley' # False
'berkeley' in 'UC BERKELEY' # False
1 in [1, 2, 3] # True
24
String Methods
Just like tables have table methods, strings have string methods.
s = 'JuNiOR12' # already run
25
Method | Example Call | Example Return Value |
upper | s.upper() | 'JUNIOR12' |
lower | s.lower() | 'junior12' |
replace | s.replace('i', 'iii') | 'JuNiiiOR12' |
split * | s.split('iO') | ['JuN', 'R12'] |
join * | ' '.join(['hello',� 'world', '!']) | 'hello world !' |
*square brackets denote the list datatype.�For Data 6, lists share some characteristics with arrays.
(demo)
Square bracket notation
some_list[index]
some_list[start:end] # slice
some_list[start:end:step] # slice, skipping elements
26