I come from a c style languages, so I am natural in using != as not equal, but when I came to Python, from the documentation I read, I learned that for this purpose the <> operator is used.
Recently, I have seen a lot of code using !=, so my question is if one of them is preferred over the other or is one of them deprecated.
Also, I would like to know if there is any difference between them.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Python 2 supports both, in python 3 the <> operator has been removed.
There is no difference between the two, but != is the preferred form.
Method 2
From the official docs you linked
!= can also be written <>, but this is an obsolete usage kept for
backwards compatibility only. New code should always use !=.
I believe the rationale for originally accepting <> was that it looked more natural for someone coming from a mathematical background than the common C-style != operator.
Method 3
I don’t know what documentation you read, but I’m not aware of any that recommends <> over !=. PEP8, the main style guide, doesn’t mention any such recommendation.
Method 4
Just for the record,<> has been obsolete since at least as early as version 1.4, which was released in October 1996.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0