Written by PIRATE KING
Starting the video will automatically play it from this chapter.
Comparison operators, or relational operators, compare two values and return True or False. Python has six of these:
For example, 5 == 10 is False because they aren't equal, while 5 != 10 is True since they are different. What about 5 == "5", though?
Instruction:
5 and the string "5" using the == operator.Expected output:
False
Make sure to compare the integer 5 and the string "5".
We get False because they have different types. I'll explain this in more detail later, but for now, just know that values of different types usually aren't equal.
== Equal to!= Not equal to> Greater than< Less than>= Greater than or equal to<= Less than or equal toPython provides three logical operators: and, or, and not. These operators are used to combine conditional statements and evaluate expressions, returning Boolean values True or False.
| A | B | A and B |
|---|---|---|
| T | T | T |
| T | F | F |
| F | T | F |
| F | F | F |
The and operator returns True only if both operands on either side of the operator are True. If either or both operands are False, the expression returns False. For example,
| A | B | A or B |
|---|---|---|
| T | T | T |
| T | F | T |
| F | T | T |
| F | F | F |
The or operator returns True if at least one of the operands is True. The expression returns False only if both operands are False. Let's execute this example and see.
| A | not A |
|---|---|
| T | F |
| F | T |
The not operator is a unary operator, meaning it operates on a single operand. It reverses the Boolean value of its operand; not True becomes False, and not False becomes True.
You can combine variables with logical operators to create expressions. For example, using boolean variables like password_auth and phone_auth, you can define different security levels with and/or operators. Sensitive data requires both password and phone authentication (and), while non-sensitive data needs just one (or). In code, this means:
password_auth and phone_auth are True, access to both sensitive and non-sensitive data are granted.True, access is limited to non-sensitive data.Let's write this in code.
We created a logical expression that since the user has authenticated with both password and mobile, they can access both sensitive and non-sensitive data. On the other hand, if authenticated with only one, access is limited to non-sensitive data. To implement this, let's flip the value of phone_auth to False in the next exercise.
Instruction:
phone_auth to False, calculate sensitive_data using and and non_sensitive_data using or, then print both results.Expected output:
False True
The and operator checks if both values are true, while or checks if at least one is true. Use these to assign values to your variables before printing.
In English, since the user has authenticated with only password, they only have access to non-sensitive data.
and Logical ANDor Logical ORnot Logical NOT| A | B | A and B | A or B | not A | not B |
|---|---|---|---|---|---|
| T | T | T | T | F | F |
| T | F | F | T | F | T |
| F | T | F | T | T | F |
| F | F | F | F | T | T |
Suppose you're checking the type of a variable — whether it's an integer, string, boolean, etc. That's where isinstance() comes in.
x is 100, which is an integer, "hello" is a string, and True is a boolean, so they all return True. But what about 3.14? isinstance(3.14, int) prints False because 3.14 is a decimal, and in Python, decimals are stored as floats.
Alright, you know Python has two Boolean values — True and False. But here's the twist: although not everything you use in Python is a Boolean, some things just behave like one. When Python evaluates a value in a condition, it quietly asks one question behind the scenes: "Hey, if I turned this into a Boolean… would it be True or False?"
True, that value is truthy.False, that value is falsy.And you can check this directly using bool().
"Hello", 25, and -1.23 are truthy while empty string "" and 0 are falsy.
So what exactly are falsy values? It's pretty much anything that's empty or “zero-like.”
Instruction:
bool() function to print the boolean value of 0, False, an empty string "", and None.Expected output:
False False False False
You can pass each value directly into the bool() function inside a print() statement.
As you can see, 0, False, empty string "", and None are all falsy values. Everything else? They're truthy!
True.False.bool() is your tool to check how Python interprets something.1. What does the expression 2 * 3 == 3 ** 2 evaluate to in Python?
2. Which of the following expressions will return True? (Select all that apply)
3. What is the result of bool(1)?
4. Given the following variables:
temp = 22 humidity = 55
Write Python code to check if temp is above 20 and humidity is less than or equal to 60. Print the result. Expected output:
True
Use the and operator to check if both conditions are true. You can pass the comparison expression directly into the print() function.
5. Write Python code to:
isinstance() to check if "2025" is an integerbool() to determine if the string "Pirate" is truthy or falsyExpected output:
False True
The isinstance() function takes the value and the target type (like int) as arguments. Remember that non-empty strings are always considered truthy.