Logical Operators
We have already used some logical operators throughout the previous lessons without formally introducing them. In this part we are going to look at them properly and understand what each one does.
Logical operators are symbols or keywords that Python uses to compare
values or combine conditions. The result of any logical operation is
always a Boolean, True or False. Here are
the operators we will cover:
andboth conditions must be Trueorat least one condition must be True<less than>greater than=assignment==equal to===does not exist in Python, but we will explain why
= vs ==
This is one of the most common sources of confusion for beginners and it is worth taking the time to understand the difference clearly.
A single = is the assignment operator.
It is used to store a value in a variable. You are not asking a
question, you are giving Python an instruction.
A double == is the equality operator.
It is used to compare two values and ask the question "are these the
same?" Python will evaluate the expression and return either
True or False.
As for ===, this operator exists in languages like
JavaScript where it is used for strict equality checking. It does not
exist in Python. If you try to use it Python will throw a
SyntaxError. In Python == already handles
strict comparison, so a third operator is not needed.
Try each of the following in the box below and run it each time to see what Python returns:
name = 'Eric'thenprint(name)print(name == 'Eric')print(name == 'John')print(name === 'Eric')and note the error
Less Than and Greater Than
The < and > operators compare two
numerical values. Just like ==, they return either
True or False. You can use them directly
inside a print() statement and Python will evaluate the
expression and print the result.
Try each of the following expressions in the box below and run it each time to see what Python returns:
print(3 > 10)print(5 < 9)print(10 > 10)print(10 >= 10)print(7 < 3)
The not Operator
The not operator is the simplest of all
the logical operators. It does exactly what it sounds like. It reverses
the Boolean value of a condition. If something is True,
not makes it False. If something is
False, not makes it True.
You have already seen conditions like this:
if is_logged_in:
With not you can flip that condition without changing
the variable itself:
if not is_logged_in:
This reads very naturally in plain English. "If the user is not logged
in, do something." This makes not a very readable and
practical tool for writing conditions that check for the absence of
something rather than the presence of it.
Try each of the following in the box below and run it each time to see what Python returns:
print(not True)print(not False)print(not 0)print(not 1)print(not '')print(not 'hello')
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named logical_operators.py in your part42 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1: Comparing Strings
- Task: Run the following expressions and observe the results:
print('a' > 'b')print('a' > 'A')
-
Goal: Write a comment explaining why
'a'is not greater than'b'and why'a'is greater than'A'. Research the term ASCII value to help you understand why.
Exercise 2: Chained Comparisons
- Task: Run the following expressions and observe the results:
print(1 < 2 < 3 < 4)print(1 < 2 > 3 < 4)print(1 < 2 > 1 < 4)
-
Goal: Write a comment explaining what happens at
the value
2in the second expression and why the result changes. Explain how Python evaluates chained comparisons from left to right.
Exercise 3: <=, >=, !=
- Task: Run the following expressions and observe the results:
print(10 >= 10)print(9 <= 10)print(5 != 5)print(5 != 6)
-
Goal: Write a comment next to each expression
explaining what the operator does and why Python returned that result.
Pay close attention to
!=and explain in your own words what "not equal to" means in a condition.
Exercise 4: Combining Operators
-
Task: Create three variables
age = 17,has_id = True,is_member = False. -
Goal: Write a conditional block that prints
"Full access granted."if the age is 18 or above and the user has an id. Print"Members only area."if the user is a member but under 18. Print"Access denied."for everything else. Test by changing each variable and write a comment explaining which operators you used and why.
Exercise 5: Student Check
-
Task: Create two variables
is_student = Falseandis_enrolled = False. - Goal: Write a conditional block that covers the following three outcomes:
- If the user is a student and enrolled: print
"You are a student." - If the user is a student but not enrolled: print
"Pick a course to begin." - If the user is not a student: print
"You cannot learn if you are not a student." -
Goal: Test all combinations and write a comment
explaining how the
notoperator is used to check for the absence of enrollment.
Don't Forget to commit and Push!