< Return to Video

Python #25 - Logical Operators [By Mosh Hamedani]

  • 0:00 - 0:03
    [MUSIC PLAYING]
  • 0:03 - 0:04
  • 0:04 - 0:06
    MOSH HAMEDANI: In Python,
    just like the other languages,
  • 0:06 - 0:09
    we have three logical operators.
  • 0:09 - 0:15
    We have the logical and,
    the logical or, and not.
  • 0:15 - 0:17
    Let's take a look
    at a few examples.
  • 0:17 - 0:20
    So I'm going to start by
    defining a variable name,
  • 0:20 - 0:21
    setting it to Mosh.
  • 0:21 - 0:25
    Now, let's say this is the
    input that we get from the user.
  • 0:25 - 0:28
    We want to check to see if
    name is an empty string.
  • 0:28 - 0:30
    How do we do that?
  • 0:30 - 0:34
    Well, earlier I talked about
    the falsy values in Python.
  • 0:34 - 0:41
    So we have 0, empty string,
    none, and an empty list.
  • 0:41 - 0:43
    These are all considered falsy.
  • 0:43 - 0:47
    So let's talk about
    an empty string.
  • 0:47 - 0:51
    If this is interpreted
    as false, then
  • 0:51 - 0:55
    if we apply the not operator
    on it, we will get true, right?
  • 0:55 - 0:59
    So to check to see if
    name is an empty string,
  • 0:59 - 1:04
    we can simply write a statement
    like this, if not name colon.
  • 1:04 - 1:05
    That's it.
  • 1:05 - 1:09
    Print Name is empty.
  • 1:09 - 1:11
    Now, let's run this program.
  • 1:11 - 1:13
    Obviously, we don't
    get that message.
  • 1:13 - 1:17
    However, if I change
    this to an empty string
  • 1:17 - 1:20
    and rerun the program,
    we get Name is empty.
  • 1:20 - 1:23
    But what if I add
    a whitespace here?
  • 1:23 - 1:26
    Well, whitespace is
    not considered truthy
  • 1:26 - 1:28
    because it's not
    an empty string.
  • 1:28 - 1:32
    So, look, if I run the program,
    we don't get the message.
  • 1:32 - 1:38
    To fix this issue, we can simply
    call .strip method to get rid
  • 1:38 - 1:39
    of all the whitespace.
  • 1:39 - 1:42
    Now, let's run it one more time.
  • 1:42 - 1:43
    Beautiful.
  • 1:43 - 1:45
    So this is our not operator.
  • 1:45 - 1:48
    Let's take a look at
    an example of and.
  • 1:48 - 1:51
    So I'm going to set age to 22.
  • 1:51 - 1:56
    Now let's see if age is
    greater than or equal to 18
  • 1:56 - 2:01
    and age is less than 65.
  • 2:01 - 2:04
    We say you are eligible.
  • 2:04 - 2:05
    Good.
  • 2:05 - 2:07
    Pretty easy, right?
  • 2:07 - 2:08
    Now, here's a question.
  • 2:08 - 2:12
    How do we write that
    expression in math?
  • 2:12 - 2:14
    We write it like this.
  • 2:14 - 2:17
  • 2:17 - 2:20
    Well, more accurately, we
    should add the equal sign here.
  • 2:20 - 2:24
    So age is between
    18 and 65, right?
  • 2:24 - 2:26
    Well, I have good news for you.
  • 2:26 - 2:28
    In Python, you can simply
    write this expression.
  • 2:28 - 2:30
    That's perfectly fine.
  • 2:30 - 2:34
    This is what we call chaining
    comparison operators.
  • 2:34 - 2:38
    So we can rewrite this
    expression in a simpler
  • 2:38 - 2:39
    and more meaningful way.
  • 2:39 - 2:41
    This is one of the
    reasons that Python
  • 2:41 - 2:44
    is a highly popular language,
    because the syntax is
  • 2:44 - 2:46
    so clean, without any clutter.
  • 2:46 - 2:48
    It's easy to
    understand, and it comes
  • 2:48 - 2:51
    with all these best
    practices and standards
  • 2:51 - 2:53
    for writing clean code.
  • 2:53 - 2:58
    So let's rewrite this
    to 18, like this.
  • 2:58 - 2:59
    Good.
  • 2:59 - 3:02
    Now, let's run the program.
  • 3:02 - 3:05
    Obviously, this
    person is eligible.
  • 3:05 - 3:06
Title:
Python #25 - Logical Operators [By Mosh Hamedani]
Description:

more » « less
Video Language:
English
Duration:
03:07

English subtitles

Revisions