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 signature unknown
        """ Return self%value. """
        pass

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

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

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

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

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

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

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ S.__sizeof__() -> size of S in memory, in bytes """
        pass

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

字符串的方法

  对于字符串而言,最重要的方法莫过于index    split   join   strip     replace   center    isdigit   count   find   formate   encode 这几个了。字符串如果用得得心应手,那么python数据结构的三分之一你就掌握了。

未完待续

时间: 2024-09-29 18:13:51

python常用数据类型内置方法介绍的相关文章

[python] 类常用的内置方法

内置方法 说明 __init__(self,...) 初始化对象,在创建新对象时调用 __del__(self) 释放对象,在对象被删除之前调用 __new__(cls,*args,**kwd) 实例的生成操作 __str__(self) 在使用print语句时被调用 __getitem__(self,key) 获取序列的索引key对应的值,等价于seq[key] __len__(self) 在调用内联函数len()时被调用 __cmp__(stc,dst) 比较两个对象src和dst __ge

数据类型内置方法之数据类型与字符串类型

数据类型内置方法 数字类型内置方法 整型 1.用途:年龄.身份证号码.手机号... 2.定义方式: age1 = 18 age2 = int(18) print(age1) print(id(age1)) print(type(age1)) print(type(age2)) 18 1723559936 <class 'int'> <class 'int'> 3.常用操作和内置方法:+ - * / // ** % :无内置方法 4.存一个值or多个值:一个值 5.有序or无序(有索

数据类型内置方法

目录 数据类型内置方法总结 数字类型内置方法 整型int() 浮点型 float() 字符串类型内置方法 列表类型内置方法 元组类型内置方法 字典类型内置方法 集合类型内置方法 数据类型总结 拷贝 深浅拷贝 数据类型内置方法总结 数字类型内置方法 整型int() 定义方式 age = int(28) int()函数可以将数字转换为整型,直接省去小数部分 常用操作 算术操作 + 比较运算符 长整型 python2 中有长整型概念,python3中没有 存一个值 or 多个值 : 一个值 可变 or

数据类型内置方法(1)

数据类型内置方法 为什么要有数据类型 对于不同类型的数据需要不同的数据类型去描述 数字类型内置方法 整形 作用:描述身高.体重.号码等整数 定义:id = 111111 方法:+ - * / % // 存储一个值or多个值:一个值 有序or无序:没有此概念 可变or不可变:不可变 有序无序指数据是否有索引,可变不可变指数据改变后内存地址是否改变 浮点型 作用:描述金额.导弹轨迹等效等小数 定义:salary = 2222.222 方法:+ - * / % // 存储一个值or多个值:一个值 有序

NO.4:自学python之路------内置方法、装饰器、迭代器

引言 是时候开始新的Python学习了,最近要靠英语,可能不会周更,但是尽量吧. 正文 内置方法 Python提供给了使用者很多内置方法,可以便于编程使用.这里就来挑选其中大部分的内置方法进行解释其用途. abs() #求取绝对值,例子. a = -5 abs(a)#求绝对值 all() #迭代对象全为真,则返回Ture,否则返回False,例子. all([1,5,6])#True all([5,0,6])#False any() #与all类似,但是有真则返回Ture,全假返回False,例

part2:Python 变量及简单类型,print 函数介绍,Python 关键字、内置函数介绍

Python是弱类型语言,关于弱类型有两个含义:(1).所有的变量无须声明即可使用,或者说对从末用过的变量赋值就是声明了该变量:(2).变量的数据类型可以随时改变,同一个变量可以进行多次赋值,可以赋数值型和字符串型值. 一. 单行注释和多行注释 注释可提高程序可读性,用于解释某行或某部分程序的作用和功能.此外注释也是调试程序的重要方式,在调试时可将不希望编译.执行的代码注释掉.注释还可以为别人或自己过一段时间后能读懂代码的目的提供帮助.合理的代码注释占源代码 1/3 左右. Python语言不能

Python基础 ---(5)Python常用的内置函数

1. abs( )函数返回数字的绝对值. print( abs(-45)) # 返回 45print("abs(0.2):",abs(0.2)) # 返回 abs(0.2): 0.2 2. all( ) 函数用于判断给定的参数中的所有元素是否都为 TRUE,如果是返回 True,否则返回 False.元素除了是 0.空.None.False 外都算 True:空元组.空列表返回值为True. print( all( [0.1,1,-1] ) ) # 返回 True print( all

数据类型内置方法:列表/元组/集合

list列表类型内置方法 作用:多个装备.多个爱好.多门课程,甚至是多个女朋友 定义方式:[]内用逗号隔开,可以有多个任意数据类型的元素 friends_list = ['longzeluola','canglaoshi','qiaobenai','nick'] lis = list('abcd') 方法: 优先掌握 索引取值(正向取值+反向取值) 即可取又可存 #list之索引取值 name_list={'nick','jason','tank','sean'} name_list[0]='n

Python字符串的内置方法

如何查看字符串有哪些内置方法? 我们可以先创建一个类型为字符串的变量,然后查看该变量的类型. 然后用dir()方法查看字符串的内置方法有哪些.最后可以用help()查看各个内置方法的用法 >>> string='hello' >>> type(string) <type 'str'> >>> dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__',