< Return to Video

Logical operators in Python are easy �

  • 0:00 - 0:01
    PROFESSOR: All right.
  • 0:01 - 0:03
    Another day, another video.
  • 0:03 - 0:06
    In this topic, I'm going to
    explain logical operators.
  • 0:06 - 0:09
    Logical operators are used
    on conditional statements
  • 0:09 - 0:11
    such as an if statement.
  • 0:11 - 0:13
    There's three types
    we'll talk about--
  • 0:13 - 0:15
    AND, OR, NOT.
  • 0:15 - 0:18
    Each exhibits
    different behavior,
  • 0:18 - 0:21
    and we'll check to
    see if two or more
  • 0:21 - 0:25
    conditions are true, or checks
    if at least one condition is
  • 0:25 - 0:26
    true.
  • 0:26 - 0:28
    NOT is a little different.
  • 0:28 - 0:30
    NOT will change the
    result of a condition.
  • 0:30 - 0:33
    If a condition is
    true, it's now false.
  • 0:33 - 0:36
    If it was originally
    false, it's now true.
  • 0:36 - 0:38
    Here's an example.
  • 0:38 - 0:40
    Say we have a program to
    check the temperature.
  • 0:40 - 0:43
    Temp equals 25.
  • 0:43 - 0:45
    The temperature will be in
    Celsius in this example.
  • 0:45 - 0:49
    If the temperature falls
    within a certain range,
  • 0:49 - 0:50
    I would like to
    print a message that
  • 0:50 - 0:53
    says the temperature is good.
  • 0:53 - 0:55
    I can check more
    than one condition
  • 0:55 - 0:58
    using the AND logical operator.
  • 0:58 - 1:01
    Let's check if our temperature
    falls between the range
  • 1:01 - 1:04
    of between 0 and 30.
  • 1:04 - 1:14
    If temp is greater than 0
    and temp is less than 30,
  • 1:14 - 1:23
    then we will print the
    temperature is good.
  • 1:23 - 1:28
    If this condition is true
    and this condition is true,
  • 1:28 - 1:32
    then we will execute this code.
  • 1:32 - 1:40
    Else we will print the
    temperature is bad.
  • 1:40 - 1:42
    Our temp is currently 25.
  • 1:42 - 1:44
    The temperature is good.
  • 1:44 - 1:48
    What if it was negative
    10 degrees Celsius?
  • 1:48 - 1:50
    Well, the temperature is bad.
  • 1:50 - 1:55
    What if it was over 30,
    like 40 degrees Celsius?
  • 1:55 - 1:57
    Well, then the
    temperature is still bad.
  • 1:57 - 2:00
    Using an AND logical operator.
  • 2:00 - 2:02
    If one of these
    conditions is false,
  • 2:02 - 2:07
    then this entire statement
    will evaluate to be false.
  • 2:07 - 2:10
    We would then skip over any
    sub-code and continue on.
  • 2:10 - 2:13
    Using the AND logical
    operator is pretty convenient
  • 2:13 - 2:15
    if you need to check
    to see if something
  • 2:15 - 2:18
    is within a certain
    range, like a temperature.
  • 2:18 - 2:20
    There is another way
    we can write this, too,
  • 2:20 - 2:22
    using the OR logical operator.
  • 2:22 - 2:25
    Let's replace AND with OR.
  • 2:25 - 2:28
    If at least one of these
    conditions is true,
  • 2:28 - 2:30
    then the entire
    statement is true.
  • 2:30 - 2:36
    How about if temp is less
    than or equal to 0, or if temp
  • 2:36 - 2:38
    is greater than or equal to 30?
  • 2:38 - 2:44
    Then the temperature is bad,
    else the temperature is good.
  • 2:44 - 2:48
    Currently, our
    temperature is set to 40.
  • 2:48 - 2:50
    Well, the temperature is bad.
  • 2:50 - 2:53
    This is false, but this is true.
  • 2:53 - 2:57
    Using the OR logical operator,
    only one of these conditions
  • 2:57 - 2:59
    needs to be true.
  • 2:59 - 3:02
    If our temperature
    was negative 15, well,
  • 3:02 - 3:05
    the temperature is still bad.
  • 3:05 - 3:10
    But if it was 20, well, then
    the temperature is good.
  • 3:10 - 3:13
    AND, as well as OR,
    are fairly similar.
  • 3:13 - 3:16
    With AND, two or
    more conditions need
  • 3:16 - 3:18
    to be true for the
    statement to be true.
  • 3:18 - 3:22
    With OR only, one of
    them needs to be true.
  • 3:22 - 3:25
    Is this true, or is this true?
  • 3:25 - 3:27
    Now, NOT is a little different.
  • 3:27 - 3:31
    Let's say we have a Boolean
    variable named sunny.
  • 3:31 - 3:32
    Is it sunny outside?
  • 3:32 - 3:38
    I'll set this to be true
    using an if statement
  • 3:38 - 3:41
    if sunny is equal to true.
  • 3:41 - 3:43
    But if you're working
    with a Boolean,
  • 3:43 - 3:46
    you can just shorten this
    to the Boolean itself.
  • 3:46 - 3:56
    If sunny, then print
    it is sunny outside.
  • 3:56 - 4:05
    Else, print it is
    cloudy outside.
  • 4:05 - 4:08
    Sunny is set to true.
  • 4:08 - 4:11
    Therefore, it is sunny outside.
  • 4:11 - 4:15
    If sunny were set to false,
    it is cloudy outside.
  • 4:15 - 4:21
    Using the NOT logical operator
    that would flip the result.
  • 4:21 - 4:27
    So if not sunny, then
    it is cloudy outside.
  • 4:27 - 4:31
    Else, it is sunny outside.
  • 4:31 - 4:32
    Sunny is set to false.
  • 4:32 - 4:35
    It is cloudy outside.
  • 4:35 - 4:38
    If sunny were true, well,
    then it's sunny outside.
  • 4:38 - 4:42
    The NOT logical operator
    will change something
  • 4:42 - 4:43
    that's true to be false.
  • 4:43 - 4:47
    Or if it was originally
    false, it's now true.
  • 4:47 - 4:48
    All right, everybody.
  • 4:48 - 4:49
    So those are logical operators.
  • 4:49 - 4:52
    They're used with
    conditional statements.
  • 4:52 - 4:55
    There's AND, OR, NOT.
  • 4:55 - 4:57
    We'll have more practice
    with these in the future.
  • 4:57 - 5:02
    And yeah, those are a few
    logical operators in Python.
  • 5:02 - 5:03
Title:
Logical operators in Python are easy �
Description:

more » « less
Video Language:
English
Duration:
05:04

English subtitles

Revisions