Boolean


Written by PIRATE KING

Comparison Operators

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? What do you think we'll get?

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.

Comparison Operators Review

  • == Equal to
  • != Not equal to
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to

Logical Operators

Python 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.

and

ABA and B
TTT
TFF
FTF
FFF

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,

or

ABA or B
TTT
TFT
FTT
FFF

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.

not

Anot A
TF
FT

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.

Logical Expressions

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:

  • If both password_auth and phone_auth are True, access to both sensitive and non-sensitive data are granted.
  • If only one is 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, as in the example below.

In English, since the user has authenticated with only password, they only have access to non-sensitive data.

Logical Operators Review

  • and Logical AND
  • or Logical OR
  • not Logical NOT
ABA and BA or Bnot Anot B
TTTTFF
TFFTFT
FTFTTF
FFFFTT

isinstance()

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.

Truthy & Falsy

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?"

  • If it turns into True, that value is truthy.
  • If it turns into 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.

Falsy Values

So what exactly are falsy values? It's pretty much anything that's empty or “zero-like.”

As you can see, 0, False, empty string "", and None are all falsy values. Everything else? They're truthy!

Quick Recap

  • Truthy means the value acts like True.
  • Falsy means it acts like False.
  • bool() is your tool to check how Python interprets something.
  • Anything empty or zero → falsy. Everything else → truthy.

Quiz

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

5. Write Python code to:

  • Use isinstance() to check if "2025" is an integer
  • Use bool() to determine if the string "Pirate" is truthy or falsy
  • Print both results, one per line

Expected output:

False
True
© PIRATE KING 2025
GOLDSTONE STUDIO LLC
Privacy Policy