Code has been added to clipboard!

The Python Not Equal Comparison Operator

Reading time 2 min
Published Feb 17, 2020
Updated Feb 17, 2020

TL;DR – In Python, not equal is a comparison operator used to determine if two variables are equal in value.

Using Python not equal

Not equal in Python is one of the comparison operators. It can have one of two return values:

  • True means one variable in Python does not equal the other
  • False means both variables are the same in value
Example
A = 5
B = 5
comparison = A!=B
print(comparison)

When comparing variables, you need to take both their values and their datatypes into consideration. In the example below, you can see two integers, both with the value of 5:

Example
A = 5
B = 5

if ( A != B ):
   print("A is not equal to B")

Now in this next example, you can see two variables with a value of 5 again. However, this time, A is an integer, and B is a string. As a rule, one datatype in Python does not equal a different one:

Example
A = 5
B = "5"

if ( A != B ):
   print("A is not equal to B")
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

The syntax for not equal in Python

There are two ways to write the Python not equal comparison operator:

  • !=
  • <>

Most developers recommend sticking with != in Python, because both Python 2 and Python 3 support this syntax. <>, however, is deprecated in Python 3, and only works in older versions:

Example
A != B #working
A <> B #deprecated

Python not equal: useful tips

  • You can use the not equal Python operator for formatted strings (f-strings), introduced in Python 3.6.
  • To return an opposite boolean value, use the equal operator ==.
  • Keep in mind that some fonts change != to look like !

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 24/01/2021
3176 People Used
Only 97 Left
Rating
5.0