Python之int內部功能介紹

來源:互聯網
上載者:User

標籤:res   介紹   oat   imp   ted   絕對值   intern   返回   保留   

int內部功能的介紹

  通過dir(int類型的值)或者使用開發工具Ctrl+單擊“int”即可查看int內部的所有功能。內部功能很多,但是很多的內部功能都不怎麼常用,特介紹如下常用到的一些內部功能

type()

1.基礎資料型別 (Elementary Data Type)使用type()函數時,得到相應的資料類型
a = 12
b = 12.01
c = "123"
print(type(a)) >>> int
print(type(b)) >>> float
print(type(c)) >>> str
2.其它類使用type()函數時,得到這個類所在的位置
from twisted.internet import reactor
print(type(reactor)) >>> twisted.internet.selectreactor.SelectReactor

bit_length()

返回表示該數字佔用的最少位元
age = 18
print(bin(18)) >>> 0b10010
0001 0010
print(age.bit_length()) >>> 5

__abs__()

返回絕對值
age = 18
score = -100
print(age.__abs__()) 或者 print(abs(age)) >>> 18
print(score.__abs__()) 或者 print(abs(score)) >>> 100

__add__(self,y)

兩個數相加
a = 1
b = 2
print(a.__add__(b)) 或者 print(a+b) >>> 3

__and__(self,y)

求兩個數的與
a = 1
b = 2
print(a.__and__(b)) >>> 0

__divmod__()

計算兩個數相除,得到一個元祖,元祖的第一個是商,第二個是餘數。這個方法在分頁中比較常用,比較重要。
all_item = 95
pager = 10
result = all_item.__divmod__(pager)
print(result) >>>(9,5)

__rdivmod__()

交換兩個數位位置然後相除

注意:像__radd__、__rand__、__rdiv__....前面加了r的都是交換兩個數的位置,然後再做運算

__eq__()

判斷兩個數是否相等
a = 18
result = a.__eq__(19)
print(result) >>> False
print(18==19) >>> False

__float__()

將int轉變成float
age = 18
print(type(age)) >>>int
result = age.__float__()
print(type(result)) >>>float

__floordiv__()

兩個數相除,只保留商
age = 5
result = age.__floordiv__(6)
print(result) >>> 0
print(5//6) >>> 0

__init__()

int類的構造方法
執行
age = int(19)
就會執行構造方法

__pow__()

求冪
a = 2
b = 2
print(a.__pow__(b)) >>> 4
print(a**b) >>> 4

........

其它的int內部功能可以自行瞭解,以上都是經常用到的!

 

Python之int內部功能介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.