最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python不等于運(yùn)算符的具體使用

 更新時(shí)間:2021年12月28日 15:48:02   作者:cunchi4221  
在Python語言中,用 != 表示不等于,本文主要介紹了python不等于運(yùn)算符的具體使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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.

如果兩個(gè)變量具有相同的類型并且具有不同的值 ,則Python不等于運(yùn)算符將返回True ;如果值相同,則它將返回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是動(dòng)態(tài)的強(qiáng)類型語言,因此,如果兩個(gè)變量具有相同的值,但它們的類型不同,則不相等的運(yùn)算符將返回True 。

Python不等于運(yùn)算符 (Python not equal operators)

Operator Description
!= Not Equal operator, works in both Python 2 and Python 3.
<> Not equal operator in Python 2, deprecated in Python 3.
操作員 描述
!= 不是Equal運(yùn)算符,可在Python 2和Python 3中使用。
<> 在Python 2中不等于運(yùn)算符,在Python 3中已棄用。

Python 2示例 (Python 2 Example)

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

我們來看一些Python 2.7中不等于運(yùn)算符的示例。

$ python2.7
Python 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 darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 <> 20
True
>>> 10 <> 10
False
>>> 10 != 20
True
>>> 10 != 10
False
>>> '10' != 10
True
>>>

Python 3示例 (Python 3 Example)

Here is some examples with Python 3 console.

這是Python 3控制臺(tái)的一些示例。

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

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

如果您使用的是Python 3.6或更高版本,我們也可以將Python不等于運(yùn)算符與f字符串一起使用。

x = 10
y = 10
z = 20
 
print(f'x is not equal to y = {x!=y}')
 
flag = x != z
print(f'x is not equal to z = {flag}')
 
# python is strongly typed language
s = '10'
print(f'x is not equal to s = {x!=s}')

Output:

輸出:

x is not equal to y = False
x is not equal to z = True
x is not equal to s = True

Python不等于自定義對(duì)象 (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.

當(dāng)我們使用不等于運(yùn)算符時(shí),它將調(diào)用__ne__(self, other)函數(shù)。 因此,我們可以為對(duì)象定義自定義實(shí)現(xiàn)并更改自然輸出。

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.

假設(shè)我們有帶字段的Data類-id和record。 當(dāng)我們使用不等于運(yùn)算符時(shí),我們只想比較它的記錄值。 我們可以通過實(shí)現(xiàn)自己的__ne __()函數(shù)來實(shí)現(xiàn)這一點(diǎn)。

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:

輸出:

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:

請(qǐng)注意,d1和d2記錄值相同,但“ id”不同。 如果刪除__ne __()函數(shù),則輸出將如下所示:

True
True

翻譯自: https://www.journaldev.com/25101/python-not-equal-operator

到此這篇關(guān)于python不等于運(yùn)算符的具體使用的文章就介紹到這了,更多相關(guān)python不等于運(yùn)算符內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python攻防-破解附近局域網(wǎng)WIFI密碼實(shí)現(xiàn)上網(wǎng)自由

    python攻防-破解附近局域網(wǎng)WIFI密碼實(shí)現(xiàn)上網(wǎng)自由

    本文將記錄學(xué)習(xí)如何通過 Python 腳本實(shí)破解附近局域網(wǎng) WIFI 密碼的暴力破解,隨時(shí)隨地免費(fèi)蹭網(wǎng),再也不被WiFi密碼困擾,實(shí)現(xiàn)蹭網(wǎng)自由
    2021-08-08
  • Django點(diǎn)贊的實(shí)現(xiàn)示例

    Django點(diǎn)贊的實(shí)現(xiàn)示例

    本文主要介紹了Django點(diǎn)贊的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Python內(nèi)置函數(shù)delattr的具體用法

    Python內(nèi)置函數(shù)delattr的具體用法

    本篇文章主要介紹了Python內(nèi)置函數(shù)delattr的具體用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • windows系統(tǒng)中python使用rar命令壓縮多個(gè)文件夾示例

    windows系統(tǒng)中python使用rar命令壓縮多個(gè)文件夾示例

    這篇文章主要介紹了windows系統(tǒng)中python使用rar命令壓縮多個(gè)文件夾示例,需要的朋友可以參考下
    2014-05-05
  • Python關(guān)于excel和shp的使用在matplotlib

    Python關(guān)于excel和shp的使用在matplotlib

    今天小編就為大家分享一篇關(guān)于Python關(guān)于excel和shp的使用在matplotlib,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 如何一鍵升級(jí)Python所有包

    如何一鍵升級(jí)Python所有包

    這篇文章主要介紹了如何一鍵升級(jí)Python所有包,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-11-11
  • Python自動(dòng)化測(cè)試中yaml文件讀取操作

    Python自動(dòng)化測(cè)試中yaml文件讀取操作

    這篇文章主要介紹了Python自動(dòng)化測(cè)試中yaml文件讀取操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Python for循環(huán)你了解嗎

    Python for循環(huán)你了解嗎

    這篇文章主要為大家介紹了Python for循環(huán),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Python文件處理的魔法之旅

    Python文件處理的魔法之旅

    Python文件處理在數(shù)據(jù)持久化、配置管理和日志記錄方面發(fā)揮著至關(guān)重要的作用,通過學(xué)習(xí)如何讀取、寫入和修改文件,開發(fā)者可以更加高效地處理各種數(shù)據(jù)和配置文件,需要的朋友可以參考下
    2024-11-11
  • Python報(bào)錯(cuò)ImportError:?IProgress?not?found.?Please?update?jupyter?and?ipywidgets解決

    Python報(bào)錯(cuò)ImportError:?IProgress?not?found.?Please?update

    在使用Jupyter Notebook或JupyterLab進(jìn)行交互式編程時(shí),我們可能會(huì)遇到各種導(dǎo)入錯(cuò)誤,本文就來介紹一下Python報(bào)錯(cuò)ImportError:?IProgress?not?found.?Please?update?jupyter?and?ipywidgets解決,感興趣的可以了解一下
    2024-06-06

最新評(píng)論

甘肃省| 木兰县| 博客| 启东市| 霞浦县| 临邑县| 六枝特区| 崇明县| 休宁县| 林州市| 昔阳县| 观塘区| 青川县| 虹口区| 青川县| 聂拉木县| 合山市| 沙雅县| 龙陵县| 竹北市| 天长市| 新建县| 广德县| 驻马店市| 万山特区| 台湾省| 比如县| 砚山县| 英吉沙县| 清苑县| 永清县| 库伦旗| 宜兰市| 锦州市| 河曲县| 达尔| 陕西省| 岳阳县| 东海县| 宜君县| 浙江省|