CodeSutraHub

Boolean Data Type and Relational Operators

Relational operators are used to evaluate conditions. The result of a condition is either True or False (Boolean type). In this topic, we will learn about Boolean data type, relational operators, and logical operators.

Boolean Data Type

The Boolean data type has only two values: True and False. In Python, the keywords True and False are used to represent Boolean values. Boolean values are mainly used in conditions.

Example of Boolean Values in Python


# Example of Boolean values in Python

is_pass = True
is_promoted = False

print("Is he passed?", is_pass)
print("Is he promoted?", is_promoted)
Output:
Is he passed? True
Is he promoted? False
  1. is_pass = True
    Indicates that the person has passed.
  2. is_promoted = False
    Indicates that the person is not promoted.
  3. print("Is he passed?", is_pass)
    Output: Is he passed? True
  4. print("Is he promoted?", is_promoted)
    Output: Is he promoted? False

Relational Operators

A relational operator checks the relationship between two operands. The result will be either True or False. Python provides six relational operators, which are listed below.
Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b
The relational operators shown above are used in conditions. One important point to note is that the == operator is used to compare two values. If we use the = operator, it becomes an assignment operator. For example, a = b assigns the value of b to a. In most programming languages, a single = is used for assignment, and double == is used for comparison.
Note: Relational operators are used in conditions.

Example: In 10 > 5, since 10 is greater than 5, the result of the condition is True.

Examples of Relational Operators


# Example of relational operators in Python

a = 10
b = 5

print("a == b:", a == b)
print("a != b:", a != b)
print("a > b:", a > b)
print("a < b:", a < b)
print("a >= b:", a >= b)
print("a <= b:", a <= b)
Output:
a == b: False
a != b: True
a > b: True
a < b: False
a >= b: True
a <= b: False
  1. a == b
    Checks whether a and b are equal.
    10 == 5 → False
  2. a != b
    Checks whether a and b are not equal.
    10 != 5 → True
  3. a > b
    Checks whether a is greater than b.
    10 > 5 → True
  4. a < b
    Checks whether a is less than b.
    10 < 5 → False
  5. a >= b
    Checks whether a is greater than or equal to b.
    10 >= 5 → True
  6. a <= b
    Checks whether a is less than or equal to b.
    10 <= 5 → False

Logical Operators in Python

Logical operators are used in conditions. They are used to combine multiple conditions, and the result is always a Boolean value (True or False). Python provides three logical operators, as shown below.
Operator Description Example
and Result is True if both conditions are True a > 18 and a < 60
or Result is True if at least one condition is True a <= 18 or a > 60
not Reverses the condition result.
True becomes False and False becomes True.
not a > 18

Examples of Logical Operators


# Example of logical operators in Python
a = 10
b = 15

print("a > 5 and b > 10:", a > 5 and b > 10)
print("a < 5 or b > 10:", a < 5 or b > 10)
print("not a > 5:", not a > 5)
Output:
a > 5 and b > 10: True
a < 5 or b > 10: True
not a > 5: False
  1. a > 5 and b > 10
    Both conditions are True, so the result is True.
    True and True → True
  2. a < 5 or b > 10
    At least one condition is True, so the result is True.
    False or True → True
  3. not a > 5
    The condition is True, so the result becomes False.
    not True → False

What We Have Learned So Far

  • Boolean Data Type
  • Relational Operators
  • Logical Operators
  • Python loop statements will be covered in the next topic

Below Video Explains Boolean Data Type, Relational and Logical Operators