< Return to Video

Evaluating compound boolean expressions | Intro to computer science - Python | Khan Academy

  • 0:00 - 0:02
    - [Presenter] How does the
    computer evaluate expressions
  • 0:02 - 0:05
    with the logical operators and or and not?
  • 0:05 - 0:08
    To find out, let's explore
    the order of operations
  • 0:08 - 0:10
    for compound Boolean expressions.
  • 0:10 - 0:12
    Imagine we're working
    on a program to check
  • 0:12 - 0:14
    if a specific song matches the filters
  • 0:14 - 0:16
    for a specific playlist.
  • 0:16 - 0:19
    This expression asks, are
    the song's beats per minute
  • 0:19 - 0:21
    both greater than or equal to 150,
  • 0:21 - 0:24
    and less than or equal to 180?
  • 0:24 - 0:26
    Logical operators like this and
  • 0:26 - 0:28
    come last in the order of operations.
  • 0:28 - 0:31
    So the computer evaluates the
    two sides separately first.
  • 0:31 - 0:35
    Let's say the variable BPM
    contains the value 200.
  • 0:35 - 0:37
    The comparison operators
    greater than or equal to
  • 0:37 - 0:39
    and less than or equal to have
    the same level of precedence,
  • 0:39 - 0:42
    so the computer evaluates left to right.
  • 0:42 - 0:44
    So we substitute in that 200
  • 0:44 - 0:46
    on the left-hand side first and simplify.
  • 0:46 - 0:49
    200 is greater than or equal to 150,
  • 0:49 - 0:51
    so this evaluates to true.
  • 0:51 - 0:53
    Then we jump to the right-hand side.
  • 0:53 - 0:56
    200 is not less than or equal to 180,
  • 0:56 - 0:59
    so this evaluates to false.
  • 0:59 - 1:00
    Now that we've simplified both sides down
  • 1:00 - 1:03
    to a Boolean value, we evaluate the and.
  • 1:03 - 1:06
    An expression with the and
    operator only evaluates to true
  • 1:06 - 1:08
    if both sides are true.
  • 1:08 - 1:10
    So a true and a false evaluates to false.
  • 1:10 - 1:13
    And boom, the computer has its answer.
  • 1:13 - 1:14
    Let's try an expression
  • 1:14 - 1:16
    where there are multiple
    logical operators.
  • 1:16 - 1:17
    This asks the question,
  • 1:17 - 1:20
    is the genre any of
    the spellings of lo-fi?
  • 1:20 - 1:25
    Let's say the variable genre
    contains the string lo-fi.
  • 1:25 - 1:26
    We evaluate the expressions around
  • 1:26 - 1:29
    the logical operators first
    and then apply the ors.
  • 1:29 - 1:31
    We start with the leftmost expression.
  • 1:31 - 1:34
    These strings are not equal,
    so this evaluates to false.
  • 1:34 - 1:36
    We jump to the second expression.
  • 1:36 - 1:39
    These strings are equal,
    so this evaluates to true.
  • 1:39 - 1:41
    And then the third part,
    these strings are not equal,
  • 1:41 - 1:43
    so this evaluates to false.
  • 1:43 - 1:46
    Now that we're all simplified,
    we take a look at the ors.
  • 1:46 - 1:48
    An expression with the OR
    operator evaluates to true
  • 1:48 - 1:51
    if at least one of the sides is true.
  • 1:51 - 1:54
    False or true evaluates to true and then
  • 1:54 - 1:57
    true or false evaluates to true.
  • 1:57 - 1:58
    Now you may be thinking,
  • 1:58 - 2:01
    "Whoa, whoa, did I even need
    to evaluate that last part?"
  • 2:01 - 2:04
    Once I knew that second
    expression evaluated to true,
  • 2:04 - 2:07
    I knew that my final answer
    was going to be true.
  • 2:07 - 2:08
    As it's evaluating,
  • 2:08 - 2:10
    the computer makes the same optimization.
  • 2:10 - 2:14
    We called this short circuit
    evaluation or lazy evaluation.
  • 2:14 - 2:16
    In its laziness, the
    computer stops evaluating
  • 2:16 - 2:18
    as soon as it knows the final answer.
  • 2:18 - 2:21
    With the or operator the
    computer stops evaluating
  • 2:21 - 2:23
    as soon as it finds a side
    that evaluates to true
  • 2:23 - 2:25
    because no matter what's
    on the other side,
  • 2:25 - 2:28
    the expression will
    always evaluate to true.
  • 2:28 - 2:29
    True or true evaluates to true
  • 2:29 - 2:32
    and true or false also evaluates to true.
  • 2:32 - 2:34
    With the and operator
    we have the opposite.
  • 2:34 - 2:36
    The computer stops as soon as it finds
  • 2:36 - 2:38
    a side that evaluates to false.
  • 2:38 - 2:39
    No matter what's on the other side
  • 2:39 - 2:41
    the whole expression
    will evaluate to false.
  • 2:41 - 2:43
    Because false and true evaluates to false
  • 2:43 - 2:47
    and false and false
    also evaluates to false.
  • 2:47 - 2:49
    Okay, so the computer's
    saving itself some work.
  • 2:49 - 2:52
    Why do I care? Consider
    this Boolean expression.
  • 2:52 - 2:53
    It looks pretty sensible,
  • 2:53 - 2:56
    but what if the variable group
    size contains the value zero?
  • 2:56 - 2:58
    The computer can't divide by zero.
  • 2:58 - 2:59
    So this gives a runtime error.
  • 2:59 - 3:01
    Well, we could just not do that,
  • 3:01 - 3:04
    but we might not control
    the value of group size.
  • 3:04 - 3:06
    Maybe it's set by user input.
  • 3:06 - 3:08
    To solve this, we can
    check that group size
  • 3:08 - 3:10
    doesn't equal zero first.
  • 3:10 - 3:12
    If group size is equal to
    zero, the left-hand side
  • 3:12 - 3:14
    will evaluate to false and the
    computer will short circuit.
  • 3:14 - 3:16
    It'll jump to the conclusion
  • 3:16 - 3:17
    that the whole expression
    must evaluate to false
  • 3:17 - 3:19
    and won't bother evaluating
    the right-hand side,
  • 3:19 - 3:21
    thus avoiding the division by zero.
  • 3:21 - 3:24
    We can use this pattern
    across our programs,
  • 3:24 - 3:26
    taking advantage of short
    circuit evaluation to check
  • 3:26 - 3:29
    for preconditions that allow
    us to avoid possible errors.
  • 3:30 - 3:32
    Last bit. What about the not operator?
  • 3:32 - 3:34
    The not operator takes precedence
  • 3:34 - 3:36
    over the and and or operators.
  • 3:36 - 3:38
    That means this expression really asks,
  • 3:38 - 3:39
    is the genre not equal to rock
  • 3:39 - 3:42
    and is the BPM greater than 130?
  • 3:42 - 3:45
    Now, we wanna be careful about
    overusing the not operator
  • 3:45 - 3:47
    when we don't need to because it tends
  • 3:47 - 3:49
    to make things more confusing.
  • 3:49 - 3:50
    For example, this expression
    is just equivalent
  • 3:50 - 3:55
    to genre not equals rock
    and BPM greater than 130.
  • 3:55 - 3:56
    If instead I put parentheses here,
  • 3:56 - 3:59
    I would be negating the whole expression.
  • 3:59 - 4:03
    This asks, is it not both a
    rock song and a fast song?
  • 4:03 - 4:05
    That's equivalent to the
    expression is the genre
  • 4:05 - 4:09
    not equal to rock or is the
    BPM less than or equal to 130?
  • 4:09 - 4:11
    If either of these conditions is true,
  • 4:11 - 4:14
    then this and expression
    would evaluate to false,
  • 4:14 - 4:17
    which means not it would evaluate to true.
  • 4:17 - 4:18
    Now you're probably starting to understand
  • 4:18 - 4:21
    why I said to use the
    not operator sparingly.
  • 4:21 - 4:23
    If you do need to negate a
    compound Boolean expression,
  • 4:23 - 4:25
    it's often easier to understand
  • 4:25 - 4:27
    if you break it down into multiple parts.
  • 4:27 - 4:28
    Otherwise, the not operator
  • 4:28 - 4:30
    tends to make your program
    a bit less readable.
  • 4:30 - 4:34
    Just like if I said something
    like "I prefer not blue colors
  • 4:34 - 4:36
    or I'm going to not
    not in this video now."
Title:
Evaluating compound boolean expressions | Intro to computer science - Python | Khan Academy
Description:

more » « less
Video Language:
English
Team:
Khan Academy
Duration:
04:36

English subtitles

Revisions