Back to Course

Introduction to Python

0% Complete
0/0 Steps
  1. Python: Basics
    14Topics
    |
    15 Quizzes
  2. Python: Control Flow
    12Topics
    |
    12 Quizzes
  3. Python: Errors
    5Topics
    |
    5 Quizzes
Lesson 2, Topic 2
In Progress

Python: Equals and Not Equals

Yasin Cakal 5 Oct 2021
Lesson Progress
0% Complete

After defining boolean expressions, let’s learn how to create them in Python. By utilizing relational operators, we can create a boolean expression. Relational operators perform a comparison between two items and return True or False. As a result, they are occasionally referred to as comparators.

The first two relational operators we’ll discuss are:

Equals      == 
Not Equals  !=

These operators compare two objects and return True or False depending on whether they are equal. By comparing two values with the following operators, we can create boolean expressions. Each of the following is a boolean expression in its own right:

print(2 + 2 == 4) # Prints True
print(2 * 2 != 4) # Prints False
print(4 != 5) # Prints True
print("1" == 1) # Prints False

The first statement is True because 2 + 2 does equal 4. The second statement is False because 2 * 2 does equal 4. The third statement is True for the same reason. Even though 4 does not equal 5, the statement claims that “4 is not equal to 5″. Why is the last statement incorrect? The ” ” marks in “1” convert it to a string, which is not equal to the integer value 1. It is critical always to keep type in mind when using relational operators.

%d bloggers like this: