1 of 26

Comparisons and Control

1

Data 6 Fall 2025

LECTURE 14

2 of 26

Boolean Operators and Compound Expressions

2

  • Boolean Operators and Compound Expressions
  • Boolean Practice
  • Conditional Statements (if-statements)
  • [at home] More on Strings

Lecture 14, Data 6, Fall 2025

3 of 26

[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

4 of 26

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)

5 of 26

Analogy, from number lines

5

# 2 < x < 5

(x > 2) and (x < 5)

(x < 2) or (x > 5)

2

5

2

5

6 of 26

Boolean Practice

6

These are summary slides; see the notebook for demo

  • Boolean Operators and Compound Expressions
  • Boolean Practice
  • Conditional Statements (if-statements)
  • [at home] More on Strings

Lecture 14, Data 6, Fall 2025

7 of 26

Custom Boolean Predicates and Filters

We can write more filters for tables with custom functions.

“Apply-Filter-Drop”:

  1. Define a function that returns a boolean value based on a row's values.
  2. apply this function to create a new (but temporary) boolean column of Trues and Falses.
  3. Filter (use where) rows based on this new Boolean column.
  4. drop the temporary boolean column so that our resulting table has the original table's columns.

7

(demo)

8 of 26

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)

9 of 26

Also a boolean expression

NO FOOD

OR

DRINK

9

10 of 26

Boolean quirks

  • Short-circuiting (see next slide)
  • True counts as 1, False counts as 0
    • When casting booleans to ints
  • Truthy values (see last time)

10

(demo)

11 of 26

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

12 of 26

Conditional Statements (if-statements)

12

  • Boolean Operators and Compound Expressions
  • Boolean Practice
  • Conditional Statements (if-statements)
  • [at home] More on Strings

Lecture 14, Data 6, Fall 2025

13 of 26

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

14 of 26

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...'

15 of 26

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)

16 of 26

If Statements: An Example

If statements allow our code to make decisions.

16

17 of 26

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)

18 of 26

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)

19 of 26

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)

20 of 26

Here’s a recap on if statements!

  • Watch your syntax!
    • colons
    • indentation

21 of 26

Here’s a recap on if statements!

  • Begin with an if expression
  • Add elif (else-if) expressions
  • else is optional
    • use if there are too many cases to count

22 of 26

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

23 of 26

[at home] More on Strings

23

  • Boolean Operators and Compound Expressions
  • Boolean Practice
  • Conditional Statements (if-statements)
  • [at home] More on Strings

Lecture 14, Data 6, Fall 2025

24 of 26

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

25 of 26

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)

26 of 26

Square bracket notation

some_list[index]

some_list[start:end] # slice

some_list[start:end:step] # slice, skipping elements

  • Get elements of a list at a particular index
    • Also used for strings
    • Lists and strings are zero-indexed, just like arrays
  • Many more details; see homework

26