Lecture 13
Conditionals and Iteration
DATA 8
Spring 2020
Weekly Goals
Announcements
Comparison and Booleans
Comparison Operators
The result of a comparison expression is a bool value
x = 2 y = 3
x > 1 x > y y >= 3
x == y x != 2 2 < x < 5
Assignment statements
Comparison expressions
Aggregating Comparisons
Summing an array or list of bool values will count the True values only.
1 + 0 + 1 == 2
True + False + True == 2
sum([1 , 0 , 1 ]) == 2
sum([True, False, True]) == 2
(Demo)
Control Statements
Control Statements
These statements control the sequence of computations that are performed in a program
(Demo)
Random Selection
Random Selection
np.random.choice
np.random.choice(some_array, sample_size)
(Demo)
Appending Arrays
A Longer Array
(Demo)
Iteration
for Statements
(Demo)
Optional: Advanced where
A Closer Look at where
t.where(array_of_bool_values)
returns a table
with only the rows of t for which
the corresponding bool is True.
(Demo)