Lecture 10 – Conditional Statements and Iteration

DSC 10, Spring 2023

Announcements

Agenda

Note:

Booleans

Recap: Booleans

Boolean operators; not

There are three operators that allow us to perform arithmetic with Booleans – not, and, and or.

not flips True ↔️ False.

The and operator

The and operator is placed between two bools. It is True if both are True; otherwise, it's False.

The or operator

The or operator is placed between two bools. It is True if at least one is True; otherwise, it's False.

Order of operations

Booleans can be tricky!

For instance, not (a and b) is different than not a and not b! If you're curious, read more about De Morgan's Laws.

Note: & and | vs. and and or

Concept Check ✅ – Answer at cc.dsc10.com

Suppose we define a = True and b = True. What does the following expression evaluate to?

not (((not a) and b) or ((not b) or a))

A. True

B. False

C. Could be either one

Aside: The in operator

Sometimes, we'll want to check if a particular element is in a list/array, or a particular substring is in a string. The in operator can do this for us:

Conditionals

if-statements

if <condition>:
    <body>

else

If you want to do something else if the specified condition is False, use the else keyword.

elif

What if we use if instead of elif?

Example: Percentage to letter grade

Below, complete the implementation of the function, grade_converter, which takes in a percentage grade (grade) and returns the corresponding letter grade, according to this table:

Letter Range
A [90, 100]
B [80, 90)
C [70, 80)
D [60, 70)
F [0, 60)

Your function should work on these examples:

>>> grade_converter(84)
'B'

>>> grade_converter(60)
'D'


✅ Click here to see the answer after you've tried it yourself.
def grade_converter(grade):
    if grade >= 90:
        return 'A'
    elif grade >= 80:
        return 'B'
    elif grade >= 70:
        return 'C'
    elif grade >= 60:
        return 'D'
    else:
        return 'F'

Activity

def mystery(a, b):
    if (a + b > 4) and (b > 0):
        return 'bear'
    elif (a * b >= 4) or (b < 0):
        return 'triton'
    else:
        return 'bruin'

Without running code:

  1. What does mystery(2, 2) return?
  2. Find inputs so that calling mystery will produce 'bruin'.

Iteration

for-loops

for-loops

Example: Squares

The line print(num, 'squared is', num ** 2) is run four times:

This happens, even though there is no num = anywhere.

Activity

Using the array colleges, write a for-loop that prints:

Revelle College
John Muir College
Thurgood Marshall College
Earl Warren College
Eleanor Roosevelt College
Sixth College
Seventh College


✅ Click here to see the solution after you've tried it yourself.
for college in colleges:
    print(college + ' College')

Ranges

Example: Goldilocks and the Three Bears

We don't have to use the loop variable!

Randomization and iteration

np.append

name_of_array = np.append(name_of_array, element_to_add)

Example: Coin flipping

The function flip(n) flips n fair coins and returns the number of heads it saw. (Don't worry about how it works for now.)

Let's repeat the act of flipping 10 coins, 10000 times.

Now, heads_array contains 10000 numbers, each corresponding to the number of heads in 10 simulated coin flips.

The accumulator pattern

for-loops in DSC 10

Working with strings

String are sequences, so we can iterate over them, too!

Example: Vowel count

Below, complete the implementation of the function vowel_count, which returns the number of vowels in the input string s (including repeats). Example behavior is shown below.

>>> vowel_count('king triton')
3

>>> vowel_count('i go to uc san diego')
8
✅ Click here to see the answer after you've tried it yourself.
def vowel_count(s):
    # We need to keep track of the number of vowels seen so far. Before we start, we've seen zero vowels.
    number = 0

    # For each of the 5 vowels:
    for vowel in 'aeiou':
        # Count the number of occurrences of this vowel in s.
        num_vowel = s.count(vowel)
        # Add this count to the variable number.
        number = number + num_vowel
    # Once we've gotten through all 5 vowels, return the answer.
    return number

Summary, next time

Summary

Next time