日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中str和int区别_Python如何比较string和int?

發布時間:2025/4/16 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中str和int区别_Python如何比较string和int? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python 2 manual:

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.

當您訂購兩個字符串或兩個數字類型時,排序是以預期的方式完成的(字符串的字典排序,整數的數字排序)。

當您訂購數字和非數字類型時,數字類型優先。

>>> 5 < 'foo'

True

>>> 5 < (1, 2)

True

>>> 5 < {}

True

>>> 5 < [1, 2]

True

當您訂購兩個不兼容的類型(兩者都不是數字)時,它們按其類型名的字母順序排序:

>>> [1, 2] > 'foo' # 'list' < 'str'

False

>>> (1, 2) > 'foo' # 'tuple' > 'str'

True

>>> class Foo(object): pass

>>> class Bar(object): pass

>>> Bar() < Foo()

True

一個例外是舊式類,它總是在新式類之前。

>>> class Foo: pass # old-style

>>> class Bar(object): pass # new-style

>>> Bar() < Foo()

False

Is this behavior mandated by the language spec, or is it up to implementors?

Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily.

所以這是一個實現細節。

Are there differences between any of the major Python implementations?

我不能回答這一個,因為我只使用官方的CPython實現,但還有其他的Python實現,如PyPy。

Are there differences between versions of the Python language?

在Python 3.x中,行為已經改變,因此嘗試排序整數和字符串將引發錯誤:

>>> '10' > 5

Traceback (most recent call last):

File "", line 1, in

'10' > 5

TypeError: unorderable types: str() > int()

總結

以上是生活随笔為你收集整理的python中str和int区别_Python如何比较string和int?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。