1 of 18

LESSON #3

2 of 18

LOGICAL OPERATORS

3 of 18

Python Logical Operators

  • Logical operators are used to combine conditional statements (which we will learn about later!)

and

    • Combines two statements that are both true

or

    • One of the statements is true

not

    • Reverse the result, returns False if the result is True

4 of 18

AND

5 of 18

OR

6 of 18

AND

7 of 18

OR

8 of 18

AND

NOT

NOT

9 of 18

AND

=

10 of 18

or

=

11 of 18

COMPARISONS

12 of 18

COMPARISONS

== Equals

!= Does not equal

>= Greater than or equal to

> Greater than

<= Less than or equal to

< Less than

13 of 18

What is the difference between the == and = operators in Python?

14 of 18

Difference between == and =

  • In Python, the == operator is used for comparison and checks if two values are equal, whereas the = operator is used for assignment and assigns a value to a variable.
  • For example, if we want to check if two variables x and y have the same value, we would use the == operator like this:

x = 5

y = 5

print(x==y) # This would print True!

  • On the other hand, if we want to assign the value of 5 to the variable x, we would use the = operator like this:

x = 5

It is important to note that using = instead of == in a comparison statement can result in unintended consequences or errors.

15 of 18

#COMPARISONS

>>> print(2 != 2)

>>> print(4 >= 2)

>>> print(“Hi” == “Hello”)

>>> print(10 < 11)

False

True

False

True

16 of 18

Now Let’s Add

Logical Operators!

17 of 18

#COMPARISONS

x = 5

>>> print(x > 3 and x < 10)

>>> print(x > 3 or x < 4)

>>> print(x >= 7 and x < 3)

>>> print(x <= 3 or x >= 5)

True

True

False

True

18 of 18

Google Slides

Activity!