python常用数据类型内置方法介绍

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python常用数据类型内置方法介绍相关的知识,希望对你有一定的参考价值。

  熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功。

 

一、整型

a = 100

a.xxx()

技术分享
class int(object):

    def bit_length(self):
        ##如果将某个整数用2进制表示,返回这个2进制所占bit位数。
        return 0

    def conjugate(self, *args, **kwargs):
        ##共轭复数

    @classmethod # known case
    def from_bytes(cls, bytes, byteorder, *args, **kwargs):
        ##好像进制转换有关,傻傻搞不懂,百度也没有答案
        pass

    def to_bytes(self, length, byteorder, *args, **kwargs):
        ##上面那个的反向方法
        pass

    def __abs__(self, *args, **kwargs):
        ##数学函数:绝对值
        """ abs(self) """
        pass

    def __add__(self, *args, **kwargs):
        ##加法
        """ Return self+value. """
        pass

    def __and__(self, *args, **kwargs):
        ##逻辑与运算
        """ Return self&value. """
        pass

    def __bool__(self, *args, **kwargs):
        ##
        """ self != 0 """
        pass

    def __ceil__(self, *args, **kwargs):
        """ Ceiling of an Integral returns itself. """
        pass

    def __divmod__(self, *args, **kwargs):
        """ Return divmod(self, value). """
        pass

    def __eq__(self, *args, **kwargs):
        """ Return self==value. """
        pass

    def __float__(self, *args, **kwargs):
        """ float(self) """
        pass

    def __floordiv__(self, *args, **kwargs):
        """ Return self//value. """
        pass

    def __floor__(self, *args, **kwargs):
        """ Flooring an Integral returns itself. """
        pass

    def __format__(self, *args, **kwargs):
        pass

    def __getattribute__(self, *args, **kwargs):
        """ Return getattr(self, name). """
        pass

    def __getnewargs__(self, *args, **kwargs):
        pass

    def __ge__(self, *args, **kwargs):
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs):
        """ Return self>value. """
        pass

    def __hash__(self, *args, **kwargs):
        """ Return hash(self). """
        pass

    def __index__(self, *args, **kwargs):
        """ Return self converted to an integer, if self is suitable for use as an index into a list. """
        pass

    def __init__(self, x, base=10):
       ##构造方法,可以指定进制,采用base = 进制数的形式。


    def __int__(self, *args, **kwargs):
        """ int(self) """
        pass

    def __invert__(self, *args, **kwargs):
        """ ~self """
        pass

    def __le__(self, *args, **kwargs):
        """ Return self<=value. """
        pass

    def __lshift__(self, *args, **kwargs):
        """ Return self<<value. """
        pass

    def __lt__(self, *args, **kwargs):
        """ Return self<value. """
        pass

    def __mod__(self, *args, **kwargs):
        """ Return self%value. """
        pass

    def __mul__(self, *args, **kwargs):
        """ Return self*value. """
        pass

    def __neg__(self, *args, **kwargs):
        """ -self """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs):
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs):
        """ Return self!=value. """
        pass

    def __or__(self, *args, **kwargs):
        """ Return self|value. """
        pass

    def __pos__(self, *args, **kwargs):
        """ +self """
        pass

    def __pow__(self, *args, **kwargs):
        """ Return pow(self, value, mod). """
        pass

    def __radd__(self, *args, **kwargs): #
        """ Return value+self. """
        pass

    def __rand__(self, *args, **kwargs):
        """ Return value&self. """
        pass

    def __rdivmod__(self, *args, **kwargs):
        """ Return divmod(value, self). """
        pass

    def __repr__(self, *args, **kwargs):
        """ Return repr(self). """
        pass

    def __rfloordiv__(self, *args, **kwargs):
        """ Return value//self. """
        pass

    def __rlshift__(self, *args, **kwargs):
        """ Return value<<self. """
        pass

    def __rmod__(self, *args, **kwargs):
        """ Return value%self. """
        pass

    def __rmul__(self, *args, **kwargs):
        """ Return value*self. """
        pass

    def __ror__(self, *args, **kwargs):
        """ Return value|self. """
        pass

    def __round__(self, *args, **kwargs):
        """
        Rounding an Integral returns itself.
        Rounding with an ndigits argument also returns an integer.
        """
        pass

    def __rpow__(self, *args, **kwargs):
        """ Return pow(value, self, mod). """
        pass

    def __rrshift__(self, *args, **kwargs):
        ##左移
        """ Return value>>self. """
        pass

    def __rshift__(self, *args, **kwargs):
        """ Return self>>value. """
        pass

    def __rsub__(self, *args, **kwargs):
        """ Return value-self. """
        pass

    def __rtruediv__(self, *args, **kwargs):
        """ Return value/self. """
        pass

    def __rxor__(self, *args, **kwargs):
        """ Return value^self. """
        pass

    def __sizeof__(self, *args, **kwargs):
        """ Returns size in memory, in bytes """
        pass

    def __str__(self, *args, **kwargs):
        """ Return str(self). """
        pass

    def __sub__(self, *args, **kwargs):
        """ Return self-value. """
        pass

    def __truediv__(self, *args, **kwargs):
        """ Return self/value. """
        pass

    def __trunc__(self, *args, **kwargs):
        """ Truncating an Integral returns itself. """
        pass

    def __xor__(self, *args, **kwargs):
        """ Return self^value. """
        pass

    denominator = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the denominator of a rational number in lowest terms"""

    imag = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the imaginary part of a complex number"""

    numerator = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the numerator of a rational number in lowest terms"""

    real = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the real part of a complex number"""
整数

INT类型的内置方法多数为数学运算使用,不需太多记忆,随用随查。

 

二、浮点型(float)

a = 1.424242

技术分享
class float(object):

    def as_integer_ratio(self):
        ##将一个浮点数表示为最大近似的两个整数的除,例如1.5可以表示为3/2
        pass

    def conjugate(self, *args, **kwargs): 
        ##共轭复数
        pass

    def fromhex(self, string):
        ##将一个十六进制的字符串转换为浮点数
        return 0.0

    def hex(self): 
        ##将一个浮点数表示为十六进制的字符串
        return ""

    def is_integer(self, *args, **kwargs): 
        ##判断某个浮点数是否同时也是整型
        pass

    def __abs__(self, *args, **kwargs): 
        """ abs(self) """
        pass

    def __add__(self, *args, **kwargs): 
        """ Return self+value. """
        pass

    def __bool__(self, *args, **kwargs): 
        """ self != 0 """
        pass

    def __divmod__(self, *args, **kwargs): 
        """ Return divmod(self, value). """
        pass

    def __eq__(self, *args, **kwargs): 
        """ Return self==value. """
        pass

    def __float__(self, *args, **kwargs): 
        """ float(self) """
        pass

    def __floordiv__(self, *args, **kwargs): #
        """ Return self//value. """
        pass

    def __format__(self, format_spec): 
        """
        float.__format__(format_spec) -> string

        Formats the float according to format_spec.
        """
        return ""

    def __getattribute__(self, *args, **kwargs): 
        """ Return getattr(self, name). """
        pass

    def __getformat__(self, typestr):  
        ##这是一个连python官方都不知道干什么的方法,忘记它吧
        return ""

    def __getnewargs__(self, *args, **kwargs): 
        pass

    def __ge__(self, *args, **kwargs): 
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): 
        """ Return self>value. """
        pass

    def __hash__(self, *args, **kwargs): 
        """ Return hash(self). """
        pass

    def __init__(self, x): 
        pass

    def __int__(self, *args, **kwargs): 
        """ int(self) """
        pass

    def __le__(self, *args, **kwargs): 
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): 
        """ Return self<value. """
        pass

    def __mod__(self, *args, **kwargs): 
        """ Return self%value. """
        pass

    def __mul__(self, *args, **kwargs):
        """ Return self*value. """
        pass

    def __neg__(self, *args, **kwargs): 
        """ -self """
        pass

    @staticmethod 
    def __new__(*args, **kwargs): 
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): 
        """ Return self!=value. """
        pass

    def __pos__(self, *args, **kwargs): 
        """ +self """
        pass

    def __pow__(self, *args, **kwargs): 
        """ Return pow(self, value, mod). """
        pass

    def __radd__(self, *args, **kwargs): 
        """ Return value+self. """
        pass

    def __rdivmod__(self, *args, **kwargs):
        """ Return divmod(value, self). """
        pass

    def __repr__(self, *args, **kwargs): 
        """ Return repr(self). """
        pass

    def __rfloordiv__(self, *args, **kwargs): 
        """ Return value//self. """
        pass

    def __rmod__(self, *args, **kwargs): 
        """ Return value%self. """
        pass

    def __rmul__(self, *args, **kwargs): 
        """ Return value*self. """
        pass

    def __round__(self, *args, **kwargs): 
        """
        Return the Integral closest to x, rounding half toward even.
        When an argument is passed, work like built-in round(x, ndigits).
        """
        pass

    def __rpow__(self, *args, **kwargs): 
        """ Return pow(value, self, mod). """
        pass

    def __rsub__(self, *args, **kwargs): 
        """ Return value-self. """
        pass

    def __rtruediv__(self, *args, **kwargs): 
        """ Return value/self. """
        pass

    def __setformat__(self, typestr, fmt): 
        ##不要用这个方法
        pass

    def __str__(self, *args, **kwargs): 
        """ Return str(self). """
        pass

    def __sub__(self, *args, **kwargs): 
        """ Return self-value. """
        pass

    def __truediv__(self, *args, **kwargs): 
        """ Return self/value. """
        pass

    def __trunc__(self, *args, **kwargs): 
        """ Return the Integral closest to x between 0 and x. """
        pass

    imag = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the imaginary part of a complex number"""

    real = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """the real part of a complex number"""
float

float和int类似,基本都是数学运算方法,他们都有语法糖,不需要这些冗长的方法。

 

三、字符串

a = ‘hello world‘

 

技术分享
class str(object):

    def capitalize(self):
        ##首字母大写
        return ""

    def casefold(self):
        ##不知道干啥用
        return ""

    def center(self, width, fillchar=None):
        ##设定宽度,让字符串打印的时候居中,可以设定填补的字符,例如:
        ##———————商品展示—————————
        return ""

    def count(self, sub, start=None, end=None):
        ##在字符串中统计子串的个数,可以设定起始和结束位置。
        return 0

    def encode(self, encoding=utf-8, errors=strict):
        ##将字符串编码成指定的编码格式,默认为utf-8
        return b""

    def endswith(self, suffix, start=None, end=None):
        ##测试字符串是否以指定的字符串结尾,可以指定判断的起始和结束位置。
        return False

    def expandtabs(self, tabsize=8):
        ##将tab转换成空格,默认一个tab转换成8个空格,可以自己指定转换数量。
        return ""

    def find(self, sub, start=None, end=None):
        ##查找子串在原字符串中的最近的位置下标,可以指定起始位置。,如果没找到,返回 -1
        return 0

    def format(self, *args, **kwargs):
        ##字符串格式化,接收动态参数。例如:
        ‘‘‘
        s = ‘hello {0},this is {1}‘
        s.format(‘andy‘,‘jack‘)
        或者:
        s = ‘hello {name1},this is {name2}‘
        s.format(name2=‘jack‘,name1=‘andy‘)
        ‘‘‘
        pass

    def format_map(self, mapping):
        """
        S.format_map(mapping) -> str

        Return a formatted version of S, using substitutions from mapping.
        The substitutions are identified by braces (‘{‘ and ‘}‘).
        """
        return ""

    def index(self, sub, start=None, end=None):
        ##和find方法一样也是查找子串的位置。不同的是,如果没找到会跳出异常。
        return 0

    def isalnum(self):
        ##测试字符串是否全由数字和字母组成,返回True或False。
        return False

    def isalpha(self):
        #测试字符串是否全由字母组成,返回True或False。
        return False

    def isdecimal(self):
        ##测试字符串是否全由10进制数字组成,返回True或False。
        return False

    def isdigit(self):
        ##测试字符串是否全由数字组成,返回True或False。
        return False

    def isidentifier(self):
        ##测试字符串是否由标识符组成,返回True或False。
        return False

    def islower(self):
        ##测试字符串是否全由小写字母组成,返回True或False。
        return False

    def isnumeric(self):

        return False

    def isprintable(self):
        ##判断字符串是否可打印
        return False

    def isspace(self):
        ##判断字符串是否为空白
        return False

    def istitle(self):
        ##判断字符串是否是标题
        return False

    def isupper(self):
         ##判断字符串是否全是大写
        return False

    def join(self, iterable):
        ##极为重要的字符串方法,可以将对象的元素连接起来。例如:
        ##s = ‘‘.join([‘a‘,‘b‘,‘c‘])
        ##s = ‘abc‘
        return ""

    def ljust(self, width, fillchar=None):
        ##字符串左对齐的同时,右边用指定的字符填充指定的宽度。
        return ""

    def lower(self):
        ##将字符串全部变成小写
        return ""

    def lstrip(self, chars=None):
        ##去除字符串左边的东东
        return ""

    def maketrans(self, *args, **kwargs):
        ##一个复杂的字符替换方法
        pass

    def partition(self, sep):
        ##以某个指定的子串为分割处,将字符串分割成各个部分,并返回一个元组。
        pass

    def replace(self, old, new, count=None):
        ##用新的子串替换旧的子串,可以指定数量
        return ""

    def rfind(self, sub, start=None, end=None):
        ##从右往左查找子串
        return 0

    def rindex(self, sub, start=None, end=None):
        ##从右往左查找子串的下标
        return 0

    def rjust(self, width, fillchar=None):
        ##字符串右对齐的同时,左边用指定字符填充指定宽度。
        return ""

    def rpartition(self, sep):
        ##从右往左分割字符串
        pass

    def rsplit(self, sep=None, maxsplit=-1):
        ##从右往左分割
        return []

    def rstrip(self, chars=None):
        ##去除字符串右边的东东
        return ""

    def split(self, sep=None, maxsplit=-1):
        ##极为重要的字符串方法。将字符串以指定字符为标志进行分割,默认是空格。返回一个列表。
        return []

    def splitlines(self, keepends=None):
        ##以行为单位分割字符串
        return []

    def startswith(self, prefix, start=None, end=None):
        ##判断字符串是否由指定的子串开始,可以指定起始位置。
        return False

    def strip(self, chars=None):
        ##极为重要的字符串方法,将字符串的前后指定字符去除,默认是去空格和换行符。
        return ""

    def swapcase(self):
        ##字符串大小写反转
        return ""

    def title(self):
        ##将字符串转换成标题格式
        return ""

    def translate(self, table):
        ‘‘‘
        借用武神的例子:
        转换,与上面的maketrans方法配合,需要先做一个对应表,
        最后一个表示删除字符集合
        intab = "aeiou"
        outtab = "12345"
        trantab = maketrans(intab, outtab)
        str = "this is string example....wow!!!"
        print str.translate(trantab, ‘xm‘)
        ‘‘‘
        return ""

    def upper(self):
        ##将字符串全部转换成大写
        return ""

    def zfill(self, width): 
        ##返回指定长度的字符串,原字符串右对齐,前面填充0
        return ""

    def __add__(self, *args, **kwargs): # real signature unknown
        """ Return self+value. """
        pass

    def __contains__(self, *args, **kwargs): # real signature unknown
        """ Return key in self. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __format__(self, format_spec): # real signature unknown; restored from __doc__
        """
        S.__format__(format_spec) -> str

        Return a formatted version of S as described by format_spec.
        """
        return ""

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __getitem__(self, *args, **kwargs): # real signature unknown
        """ Return self[key]. """
        pass

    def __getnewargs__(self, *args, **kwargs): # real signature unknown
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __hash__(self, *args, **kwargs):
        """ Return hash(self). """
        pass

    def __init__(self, value=‘‘, encoding=None, errors=strict): 
        ##字符串构造方法
        """
        str(object=‘‘) -> str
        str(bytes_or_buffer[, encoding[, errors]]) -> str

        Create a new string object from the given object. If encoding or
        errors is specified, then the object must expose a data buffer
        that will be decoded using the given encoding and error handler.
        Otherwise, returns the result of object.__str__() (if defined)
        or repr(object).
        encoding defaults to sys.getdefaultencoding().
        errors defaults to ‘strict‘.
        # (copied from class doc)
        """
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    def __mod__(self, *args, **kwargs): # real signatu

以上是关于python常用数据类型内置方法介绍的主要内容,如果未能解决你的问题,请参考以下文章

python 开发 -- 11数字类型内置方法

Python数据类型-1 数据类型介绍

数据类型内置方法

Python从简单案列中揭示常用内置函数以及数据类型

python学科课程分几个部分?

数据类型的常用操作和内置方法