Glossary
HTML
CSS
JavaScript
Python

PYTHON

Python Not Equal: The Inequality Operator in Python

The Python not equal operator (!=) checks if two variables or values are not equal. The result of the evaluation is either True or False. The operator returns True if the values are not equal, and False if they are equal.

How to Use the Not Equal Operator in Python

The syntax for using the not equal operator is straightforward:

# Checking if two values are not equal
result = (a != b)
print(result)
  • !=: The symbol for the not equal operator.
  • a, b: The variables or values to compare.

When to Use the Not Equal Operator

The not equal operator is great for creating conditional logic or validating data inputs. != is particularly helpful when actions are dependent on values being different.

Conditional Statements

You can use the != operator in conditional statements to only act when conditions are notably different.

if user_permission != "admin":
    print("Access denied. Admin rights required.")

While Loops

The not equal operator can keep while loops running until a particular exit condition evaluates to True.

while user_input != "quit":
		user_input = input("Enter your command (type 'quit' to exit): ") 

Data Validation

The != operator is also helpful for validating inputs against unwanted values, like insecure passwords.

password = input("Create your password: ")
if password != "password123":  # Prevents using a common weak password
    print("Password set successfully.")
else:
    print("Choose a stronger password.")

Examples of the Not Equal Operator in Python

User Input Validation

The not equal operator can help prevent certain user inputs. An example might be checking a password against a blocklisted value:

entered_password = input("Enter your password: ")
if entered_password != "12345":
    print("Access granted.")
else:
    print("Access denied. Choose a stronger password.")

Application Settings

In application development, the not equal operator can help adjust a setting only if the current state does not equal the desired state:

current_setting = "low"
desired_setting = "high"
if current_setting != desired_setting:
    print("Updating setting to high.")

Command-Line Applications

In a command-line application, != might control when a loop should terminate by checking if the loop condition is no longer met:

command = ""
while command != "exit":
    command = input("Enter command (type 'exit' to quit): ")
    if command != "exit":
        print(f"Executing {command}")
    else:
        print("Exiting program.")

Learn More About the Not Equal Operator in Python

Comparing Different Data Types with Not Equals

Python recognizes values of different data types as inherently different. Therefore, when using the not equal operator to compare different data types, the result is always True.

# Comparing integer and string
number = 10
string_number = "10"
print(number != string_number)  # Outputs: True

# After conversion
print(number != int(string_number))  # Outputs: False

Use in Data Structures

The not equal operator is also useful when working with data structures, checking if two collections are not identical:

list1 = [1, 2, 3]
list2 = [1, 2, 4]
print(list1 != list2)  # Outputs: True

Combining with Logical Operators

You can combine != with logical operators (and, or, and not) for more complex boolean expressions:

username = "admin"
access_level = "user"

if username != "admin" or access_level != "admin":
    print("Restricted access.")
else:
    print("Full access granted.")

Python Not Equal vs. the Not Operator

The not equal operator (!=) and the not operator have similarities but often different use cases. != checks if two values are not equal while not inverts the truth value of a boolean expression.

Using != to check for inequality is often more readable than combining not with ==. Using not to invert a boolean value, however, is often more readable than using != with True or False.

# Using '!=' to compare values for inequality
status_code = 200
if status_code != 200:
		print("Error occurred.")
		
# Using 'not' to invert a boolean expression
is_admin = True
if not is_admin:
		print("User is not an administrator.")
Learn to Code in Python for Free
Start learning now
To advance beyond this tutorial and learn Python by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2023 Mimo GmbH