Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is not equal in Python

Understanding Equality and Inequality in Python

In the world of programming, especially for beginners, understanding how to compare things is as crucial as knowing how to count. In Python, as in real life, we often need to determine if two things are the same or not. When we talk about equality, we're asking if one thing is the same as another. Conversely, inequality is the opposite: it's when we want to know if one thing is different from another.

The Equality Operator (==)

Let's start with the concept of equality. In Python, we use the double equals sign == to check if two values are equal. It's like asking, "Are these two things the same?"

# Checking if two numbers are equal
number1 = 10
number2 = 10
print(number1 == number2)  # Output: True

# Checking if two strings are the same
string1 = "hello"
string2 = "hello"
print(string1 == string2)  # Output: True

In the above examples, we're asking Python to compare numbers and strings. When the values are the same, Python tells us that's True. It's similar to when you match two socks from your laundry; if they're the same color and size, you have a pair.

The Inequality Operator (!=)

Now, let's talk about inequality, which is like the sibling of equality, but instead of looking for a match, we're looking for a difference. In Python, we use the exclamation mark followed by the equals sign != to check if two values are not equal.

# Checking if two numbers are not equal
number1 = 10
number2 = 20
print(number1 != number2)  # Output: True

# Checking if two strings are not the same
string1 = "hello"
string2 = "world"
print(string1 != string2)  # Output: True

Here, we're asking Python, "Are these two things different?" If they are, Python responds with True. Think of it as looking at two different fruits, like an apple and an orange. They're not the same, so they're not equal.

Comparing Different Types

What happens when you compare things of different types? Like comparing apples to oranges, Python allows you to compare different data types, but they will not be equal.

# Comparing a number and a string
number = 10
string = "10"
print(number == string)  # Output: False

# Here, even though the string "10" looks like the number 10, they're different types.
# It's like comparing a toy car to a real car. They might look the same, but they're not.

Inequality in the Real World

Inequality isn't just about finding differences; it's about understanding that things can be unique. It's like having different flavors of ice cream. They're all ice cream, but each flavor is not equal to the other—they're unique.

Using Inequality with Lists

Python also allows you to compare more complex data structures, like lists. Lists are like a collection of items, such as a shopping list.

# Comparing two lists
list1 = [1, 2, 3]
list2 = [1, 2, 4]
print(list1 != list2)  # Output: True

# The lists have different items in them, so they're not equal.

Inequality in Conditional Statements

Inequality becomes particularly useful in conditional statements, where you can perform different actions depending on whether things are equal or not.

# Using inequality in an if statement
age = 18
if age != 21:
    print("You are not 21 years old.")
# This will print "You are not 21 years old." because age is not equal to 21.

The Importance of Context

Understanding the context is key when dealing with equality and inequality. In some situations, you might want to know if something is exactly the same, while in others, you might be interested in any difference.

Common Mistakes

One common mistake is using the assignment operator = instead of the equality operator ==. Remember, = is used to give a value to a variable, and == is used to compare two values.

# A common mistake
number = 10
# This is an assignment, not a comparison.
# The following line would result in an error because it's not a valid comparison.
# if number = 10:
#     print("Number is 10")

Practice Makes Perfect

The best way to get comfortable with equality and inequality is to practice. Try writing some comparisons yourself and see what Python tells you. Remember, it's okay to make mistakes; that's how you learn.


In the journey of learning to code, grasping the concepts of equality and inequality in Python is a fundamental step. It's like learning to distinguish between different colors in a painting or different instruments in a symphony. Each comparison gives us valuable information that we can use to make decisions in our code. As you continue to practice and experiment, these concepts will become second nature, and you'll be able to use them to bring your programming ideas to life. Remember, in the vast world of coding, every != is just as important as a ==, highlighting the beautiful diversity of logic that paints the masterpiece we call software.