Return to Video

Type bool

  • 0:02 - 0:07
    Hi. This lecture is all about true and
    false values, also known as Boolean
  • 0:07 - 0:14
    values. In this lecture, we'll explore the
    Python's type bool, and the operators that
  • 0:14 - 0:20
    we can apply to Boolean values. Earlier,
    we used Python's arithmetic operators like
  • 0:20 - 0:26
    multiplication and subtraction. And now,
    we're going to use some of Python's
  • 0:26 - 0:33
    comparison operators. For example, let's
    compare values three and four using the
  • 0:33 - 0:40
    less than operator. When this expression
    is evaluated, we're going to get a true
  • 0:40 - 0:46
    value or false value back. The type of
    value that we get is type bool. Let's
  • 0:46 - 0:53
    compare three with eight, asking if three
    is greater than eight, and it's not. So,
  • 0:53 - 1:00
    the value that this expression evaluates
    to is false. When we evaluate eight
  • 1:00 - 1:08
    greater than three, we get true. And if we
    were to evaluate 3.5 greater than or equal
  • 1:08 - 1:15
    to 3.4, it's also true. Lets compare two
    ends, seven with seven. Notice that the
  • 1:15 - 1:23
    operation that we're performing now is the
    equality operation. And, we need to use
  • 1:23 - 1:29
    two equal signs, not one, to signify
    equality. That's because the single equal
  • 1:29 - 1:36
    sign is already used for the assignment
    operation. Seven is equal to seven. How
  • 1:36 - 1:43
    about seven and 7.0? I type into operand
    with a type float operand. This is also
  • 1:43 - 1:50
    true. Let's assign a couple of variable
    values, x gets seven, y gets eight. And
  • 1:50 - 1:56
    now, we can apply the same equality
    operator to two variable operands. First,
  • 1:56 - 2:02
    we work up the value that x refers to,
    which is seven. And y refers to eight. And
  • 2:02 - 2:08
    then, seven is compared with eight.
    Another operator is the inequality
  • 2:08 - 2:14
    operator. We can check whether three is
    not equal to four, and that's true. The
  • 2:14 - 2:20
    comparison operators take two values and
    return a Boolean value, either true or
  • 2:20 - 2:25
    false. Python also has three logical
    operators, which are operators that are
  • 2:25 - 2:30
    applied to Boolean values and yield
    Boolean results. The first logical
  • 2:30 - 2:35
    operator that we'll use is the not
    operator. And we'll begin by creating a
  • 2:35 - 2:40
    variable grade and assigning it the value
    80. So, grade refers to 80. Now, let's
  • 2:40 - 2:46
    write up an expression that checks to see
    whether the grade i s a passing grade. Is
  • 2:46 - 2:51
    grade greater than or equal to 50? And
    that's true. In Canada, 50 is a pass.
  • 2:51 - 2:56
    We'll apply the not operator to that
    expression now. So, we're going to check
  • 2:56 - 3:01
    to see whether grade is not greater than
    or equal to 50. The order that this
  • 3:01 - 3:08
    expression is evaluated works from inside
    out. So, the grade greater than or equal
  • 3:08 - 3:14
    to 50 part of the expression is evaluated
    first and that gives the value true and
  • 3:14 - 3:20
    then the not operator is applied to true.
    Something that is not true is false. And
  • 3:20 - 3:26
    that's the result that we get back. We can
    apply this not operator two times in a
  • 3:26 - 3:32
    row, saying that this is asking whether
    this is not, not true, which is equivalent
  • 3:32 - 3:38
    to just saying, is grade greater than or
    equal to 50? So, rather than including two
  • 3:38 - 3:44
    nots in a row, eliminate double negation,
    instead write the simpler version of that,
  • 3:44 - 3:50
    which is to just say, it's great, greater
    than or equal to 50. Next, let's use the
  • 3:50 - 3:56
    and operator. First, we'll make another
    variable named grade two. That refers to
  • 3:56 - 4:03
    the value 70. And now, we'll write an
    expression involving both variables grade
  • 4:03 - 4:09
    and grade two. This expression, we'll
    check to see whether both of these are
  • 4:09 - 4:16
    passing grades. So, is grade greater than
    or equal to 50, and is grade two also
  • 4:16 - 4:23
    greater than or equal to 50? And evaluates
    True, if both operands are true. So,
  • 4:23 - 4:30
    first, this expression is evaluated and it
    is true, so then, this expression is
  • 4:30 - 4:38
    evaluated and it is also true, making this
    entire result a true result. Let's change
  • 4:38 - 4:47
    the value of the variable grade for moment
    and set it to 40. We'll rerun this Boolean
  • 4:47 - 4:55
    expression involving the and, and check to
    see what we get. Because this first
  • 4:55 - 5:04
    operand is false, the Boolean expression
    is false. And we don't even go on to check
  • 5:04 - 5:12
    the second operand's value. Now, let's set
    grade back to 80, and this time, change
  • 5:12 - 5:20
    grade two to be a failing grade. When this
    is expression is evaluated, first, this
  • 5:20 - 5:28
    part of the expression is evaluated. And
    that's true so we move on to evaluating
  • 5:28 - 5:36
    this part of the expression which is
    false. And so, the expression ev aluates
  • 5:36 - 5:43
    to false. To summarize, and again, only
    evaluates to true if both of its operands
  • 5:43 - 5:50
    are true. Otherwise. it evaluates to
    false. Finally, lets use the logical
  • 5:50 - 5:59
    operator or which also applies to two
    operands. We'll start by assign grade and
  • 5:59 - 6:07
    grade two to passing grades. And now,
    we'll write the same expression as before
  • 6:07 - 6:16
    replacing the and with an or. This
    expression will evaluate to true if at
  • 6:16 - 6:24
    least one of the operands is true. So in
    this case, we get true. Now, lets assign
  • 6:24 - 6:30
    to grade a failing grade and reevaluate
    the expression. Python will first evaluate
  • 6:30 - 6:37
    the first part of this expression and
    determine that it is false. So, it will go
  • 6:37 - 6:43
    on to evaluate the second part of the
    expression, which is true, and because, at
  • 6:43 - 6:50
    least one operand is true, the expression
    evaluates to true. If we set grade to a
  • 6:50 - 6:56
    passing grade, and grade two to a failing
    grade. Then, when the expression is
  • 6:56 - 7:02
    evaluated, it works as follows. Because
    grade is a passing grade, the, the
  • 7:02 - 7:08
    expression is evaluated to true at this
    point, without even having to go on to
  • 7:08 - 7:15
    look as the second operand. So, to
    summarize, a bool, the Boolean operator or
  • 7:15 - 7:21
    evaluates to true if at least one of its
    operands is true. And it evaluates to
  • 7:21 - 7:28
    false otherwise. Now, let's combine the
    operators into single expressions. I've
  • 7:28 - 7:34
    assigned grade and grade two passing
    grades of 80 and 90, and I'd like to
  • 7:34 - 7:40
    evaluate this expression. I'm going to
    apply not to grade greater than or equal
  • 7:40 - 7:46
    to 50, or grade two greater than or equal
    to 50. And there are a couple of different
  • 7:46 - 7:51
    ways that we can interpret this
    expression, depending on order of
  • 7:51 - 7:57
    precedence. The first would be to have the
    or operator applied first, and I'll use
  • 7:57 - 8:05
    parentheses to signal that, followed by
    the not operator. The second would be to
  • 8:05 - 8:16
    actually have the not operator apply first
    to the first part of the expression. And
  • 8:16 - 8:26
    then, the or operator apply second. So,
    not first, followed by or, or, or first
  • 8:26 - 8:35
    followed by not. Let's evaluate the
    expression and see what happens. [typing
  • 8:35 - 8:40
    sound] The value that the expression
    evaluates to is true. An d let's use the
  • 8:40 - 8:46
    parentheses to see which of the two
    operators applied first. We'll begin by
  • 8:46 - 8:51
    putting the parentheses around the or part
    of the expression, ensuring that or
  • 8:51 - 8:58
    applies before not. And when we do that,
    the result is false. So, that's not what
  • 8:58 - 9:04
    happened when we left off the parentheses,
    parentheses. And that means that that's
  • 9:04 - 9:11
    not the order of precedence. Instead, the
    order of precedence is that not is applied
  • 9:11 - 9:18
    first, as I can show here, followed by or.
    The order of precedence for logical
  • 9:18 - 9:24
    operators is not, and, and then or. And
    when we're working with multiple logical
  • 9:24 - 9:30
    operators within an expression, we can use
    parentheses to ensure that the operations
  • 9:30 - 9:35
    apply in the order we'd like without
    having to worry about what the order of
  • 9:35 - 9:40
    precedence is. Sometimes, we'll use
    parentheses to make an expression more
  • 9:40 - 9:45
    readable. For example, in this expression,
    the arithmetic operators have higher
  • 9:45 - 9:51
    precedence than the Boolean operators or
    logical operators, so these parentheses
  • 9:51 - 9:56
    are unnecessary but we include them for
    readability. Instead, we could have left
  • 9:56 - 10:02
    off the parentheses and had the following,
    where some, I find a little harder to read
  • 10:02 - 10:03
    and understand.
Title:
Type bool
Video Language:
English
stanford-bot edited English subtitles for Type bool
stanford-bot edited English subtitles for Type bool
stanford-bot edited English subtitles for Type bool
stanford-bot added a translation

English subtitles

Revisions