Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python is not equal to operator how to use

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "python is not equal to operator how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "python is not equal to operator how to use" this article.

Python not equal operator returns True if two variables are of same type and have different values, if the values are same then it returns False.

If two variables have the same type and have different values, the Python is not equal to the operator will return True; if the values are the same, it will return False.

Python is dynamic and strongly typed language, so if the two variables have the same values but they are of different type, then not equal operator will return True.

Python is a dynamic strongly typed language, so if two variables have the same value but have different types, unequal operators will return True.

Python is not equal to operator (Python not equal operators)

Operator description includes not Equal operator, works in both Python 2 and Python 3.Not equal operator in Python 2, deprecated in Python 3. Operator description! = is not an Equal operator and can be used in Python 2 and Python 3. Not equal to operator in Python 2, deprecated in Python 3. Python 2 example (Python 2 Example)

Let's see some examples of not-equal operator in Python 2.7.

Let's take a look at some examples of unequal operators in Python 2.7s.

$python2.7Python 2.7.10 (default, Aug 17 2018, 19:45:58) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwinType "help", "copyright", "credits" or "license" for more information. > > 10 20True > 10 10False > 10! = 20True > 10! = 10False >'10'! = 10True > Python 3 example (Python 3 Example)

Here is some examples with Python 3 console.

These are some examples of the Python 3 console.

$python3.7Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) [Clang 2018 (clang-600.0.57)] on darwinType "help", "copyright", "credits" or "license" for more information. > > 10 20 File ", line 1 10 20 ^ SyntaxError: invalid syntax > > 10! = 20True > 10! = 10False >'10'! = 10True >

We can use Python not equal operator withf-strings too if you are using Python 3.6 or higher version.

If you are using Python 3.6or later, we can also use the Python not equal operator with the f string.

X = 10y = 10z = 20 print (f'x is not equal to y = {xylene}') flag = x = zprint (f'x is not equal to z = {flag}') # python is strongly typed languages = '10'print (f'x is not equal to s = {xylene}')

Output:

Output:

X is not equal to y = False

X is not equal to z = True

X is not equal to s = True

Python is not equal to custom object (Python not equal with custom object)

When we use not equal operator, it calls _ ne__ (self, other) function. So we can define our custom implementation for an object and alter the natural output.

When we use the unequal operator, it calls the _ _ ne__ (self, other) function. Therefore, we can define a custom implementation for the object and change the natural output.

Let's say we have Data class with fields-id and record. When we are using the not-equal operator, we just want to compare it for record value. We can achieve this by implementing our own _ ne__ () function.

Suppose we have Data classes with fields-id and record. When we use the non-equal operator, we just want to compare its record value. We can do this by implementing our own _ _ ne _ () function.

Class Data: id = 0 record =''def _ _ init__ (self, I, s): self.id = I self.record = s def _ ne__ (self Other): # return true if different types if type (other)! = type (self): return True if self.record! = other.record: return True else: return False D1 = Data (1, 'Java') D2 = Data (2,' Java') D3 = Data (3, 'Python') print (D1! = D2) print (D2! = D3)

Output:

Output:

False

True

Notice that D1 and D2 record values are same but "id" is different. If we remove _ _ ne__ () function, then the output will be like this:

Notice that the D1 and D2 record values are the same, but the "id" is different. If you delete the _ _ ne _ _ () function, the output will look like this:

True

True

The above is all the contents of the article "python is not equal to how to use operators". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report