1. 程式人生 > >Python 整數 長整數 浮點數 字串 列表 元組 字典的各種方法

Python 整數 長整數 浮點數 字串 列表 元組 字典的各種方法

對於Python, 一切事物都是物件,物件基於類建立!!

注:檢視物件相關成員var,type, dir

一、整數

如: 18、73、84

每一個整數都具備如下需要知道的功能:

def bit_length(self): 
        """ 返回表示該數字的時佔用的最少位數 """
        """ int.bit_length() -> int Number of bits necessary to represent self in binary. >>> bin(37) '0b100101' >>> (37).bit_length() """ return 0
age = 18 
age.bit_length() --> 5
def conjugate(self, *args, **kwargs): # real signature unknown
        """ 返回該複數的共軛複數 """
        """ Returns self, the complex conjugate of any int. """
        pass
#瞭解
def __abs__(self):
        """ 返回絕對值 """
        """ x.__abs__() <==> abs(x) 
""" pass
age = -18
age.__abs__() --> 18
print(abs(age)) --> 18

 __add__ , __and__, __cmp__都是這樣的用法

重點說一下,__divmod__()

def __divmod__(self, y): 
        """ 相除,得到商和餘數組成的元組 """ 
        """ x.__divmod__(y) <==> divmod(x, y) """
        pass
age=18
age.__divmod__(2) --> (9,0)
divmod(18,2) --> (9, 0)

 __hex__(), __int__(), __float__(), __hex__(), __long__()這些方法都有預設的自帶函式方法如int(),float(), long()等。。。