Python常用数据类型方法调用
来源:互联网 发布:念奴娇东方朔知乎 编辑:程序博客网 时间:2024/06/07 04:06
数值类型的方法调用
abs(x) # 返回数字的绝对值,如abs(-10) 返回 10ceil(x) # 返回数字的上入整数,如math.ceil(4.1) 返回 5cmp(x, y) # 如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1exp(x) #返 回e的x次幂(ex),如math.exp(1) 返回2.718281828459045fabs(x) # 返回数字的绝对值,如math.fabs(-10) 返回10.0floor(x) # 返回数字的下舍整数,如math.floor(4.9)返回 4log(x) # 如math.log(math.e)返回1.0,math.log(100,10)返回2.0log10(x) # 返回以10为基数的x的对数,如math.log10(100)返回 2.0max(x1, x2,...) # 返回给定参数的最大值,参数可以为序列。min(x1, x2,...) # 返回给定参数的最小值,参数可以为序列。modf(x) # 返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。pow(x, y) # x**y 运算后的值。round(x [,n]) # 返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。sqrt(x) # 返回数字x的平方根,数字可以为负数,返回类型为实数,如math.sqrt(4)返回 2+0j
字符串类型的函数调用
string.capitalize() # 把字符串的第一个字符大写string.center(20,"*") # 把一个原字符串居中,并使用指定符号填充至指定长度的新字符串string.count(str, beg=0, end=len(string)) # 返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数string.decode(encoding='UTF-8', errors='strict') # 以 encoding 指定的编码格式解码 string,如果出错默认报一个 ValueError 的 异 常 , 除 非 errors 指 定 的 是 'ignore' 或 者'replace'string.encode(encoding='UTF-8', errors='strict') # 以 encoding 指定的编码格式编码 string,如果出错默认报一个ValueError 的异常,除非 errors 指定的是'ignore'或者'replace'string.endswith(obj, beg=0, end=len(string)) # 检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False.string.expandtabs(tabsize=8) # 把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8。string.find(str, beg=0, end=len(string)) # 检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1string.index(str, beg=0, end=len(string)) # 跟find()方法一样,只不过如果str不在 string中会报一个异常.string.isalnum() # 如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 Falsestring.isalpha() # 如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 Falsestring.isdecimal() # 如果 string 只包含十进制数字则返回 True 否则返回 False.string.isdigit() # 如果 string 只包含数字则返回 True 否则返回 False.string.islower() # 如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 Falsestring.isnumeric() # 如果 string 中只包含数字字符,则返回 True,否则返回 Falsestring.isspace() # 如果 string 中只包含空格,则返回 True,否则返回 False.string.istitle() # 如果 string 是标题化的(见 title())则返回 True,否则返回 Falsestring.isupper() # 如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 Falsestring.join(seq) # 以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串string.ljust(width) # 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串string.lower() # 转换 string 中所有大写字符为小写.string.lstrip() # 截掉 string 左边的空格string.maketrans(intab, outtab]) # maketrans() 方法用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。max(str) # 返回字符串 str 中最大的字母。min(str) # 返回字符串 str 中最小的字母。string.partition(str) # 有点像 find()和 split()的结合体,从 str 出现的第一个位置起,把 字 符 串 string 分 成 一 个 3 元 素 的 元 组 (string_pre_str,str,string_post_str),如果 string 中不包含str 则 string_pre_str == string.string.replace(str1, str2, num=string.count(str1)) # 把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次.string.rfind(str, beg=0,end=len(string) ) # 类似于 find()函数,不过是从右边开始查找.string.rindex( str, beg=0,end=len(string)) # 类似于 index(),不过是从右边开始.string.rjust(width) # 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串string.rpartition(str) # 类似于 partition()函数,不过是从右边开始查找.string.rstrip() # 删除 string 字符串末尾的空格.string.split(str="", num=string.count(str)) # 以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串string.splitlines(num=string.count('\n')) # 按照行分隔,返回一个包含各行作为元素的列表,如果 num 指定则仅切片 num 个行.string.startswith(obj, beg=0,end=len(string)) # 检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查.string.strip([obj]) # 在 string 上执行 lstrip()和 rstrip()string.swapcase() # 翻转 string 中的大小写string.title() # 返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())string.translate(str, del="") # 根据 str 给出的表(包含 256 个字符)转换 string 的字符,要过滤掉的字符放到 del 参数中string.upper() # 转换 string 中的小写字母为大写print('123' in string) # 判断'123'是否存在len(string) # 获取字符串长度
查看类所有的功能与使用方法
#!/usr/bin/env python# -*- coding:utf-8 -*-i = 123print(type(i))print(dir(type(i))) # 这是这个数据类型的所有功能# ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__',# '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__',# '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__',# '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__',# '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__',# '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__',# '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__',# '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__',# '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__',# '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']help(dir(type(i))) # 这是这个数据类型的所有功能使用方法# Help on list object:# # class list(object)# | list() -> new empty list# | list(iterable) -> new list initialized from iterable's items# | # | Methods defined here:# | # | __add__(...)# | x.__add__(y) <==> x+y# | # | __contains__(...)# | x.__contains__(y) <==> y in x# | # | __delitem__(...)# | x.__delitem__(y) <==> del x[y]# | # | __delslice__(...)# | x.__delslice__(i, j) <==> del x[i:j]# | # | Use of negative indices is not supported.# | # | __eq__(...)# | x.__eq__(y) <==> x==y# | # | __ge__(...)# | x.__ge__(y) <==> x>=y# | # | __getattribute__(...)# | x.__getattribute__('name') <==> x.name# | # | __getitem__(...)# | x.__getitem__(y) <==> x[y]# | # | __getslice__(...)# | x.__getslice__(i, j) <==> x[i:j]# | # | Use of negative indices is not supported.# | # | __gt__(...)# | x.__gt__(y) <==> x>y# | # | __iadd__(...)# | x.__iadd__(y) <==> x+=y# | # | __imul__(...)# | x.__imul__(y) <==> x*=y# | # | __init__(...)# | x.__init__(...) initializes x; see help(type(x)) for signature# | # | __iter__(...)# | x.__iter__() <==> iter(x)# | # | __le__(...)# | x.__le__(y) <==> x<=y# | # | __len__(...)# | x.__len__() <==> len(x)# | # | __lt__(...)# | x.__lt__(y) <==> x<y# | # | __mul__(...)# | x.__mul__(n) <==> x*n# | # | __ne__(...)# | x.__ne__(y) <==> x!=y# | # | __repr__(...)# | x.__repr__() <==> repr(x)# | # | __reversed__(...)# | L.__reversed__() -- return a reverse iterator over the list# | # | __rmul__(...)# | x.__rmul__(n) <==> n*x# | # | __setitem__(...)# | x.__setitem__(i, y) <==> x[i]=y# | # | __setslice__(...)# | x.__setslice__(i, j, y) <==> x[i:j]=y# | # | Use of negative indices is not supported.# | # | __sizeof__(...)# | L.__sizeof__() -- size of L in memory, in bytes# | # | append(...)# | L.append(object) -- append object to end# | # | count(...)# | L.count(value) -> integer -- return number of occurrences of value# | # | extend(...)# | L.extend(iterable) -- extend list by appending elements from the iterable# | # | index(...)# | L.index(value, [start, [stop]]) -> integer -- return first index of value.# | Raises ValueError if the value is not present.# | # | insert(...)# | L.insert(index, object) -- insert object before index# | # | pop(...)# | L.pop([index]) -> item -- remove and return item at index (default last).# | Raises IndexError if list is empty or index is out of range.# | # | remove(...)# | L.remove(value) -- remove first occurrence of value.# | Raises ValueError if the value is not present.# | # | reverse(...)# | L.reverse() -- reverse *IN PLACE*# | # | sort(...)# | L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;# | cmp(x, y) -> -1, 0, 1# | # | ----------------------------------------------------------------------# | Data and other attributes defined here:# | # | __hash__ = None# | # | __new__ = <built-in method __new__ of type object># | T.__new__(S, ...) -> a new object with type S, a subtype of T
阅读全文
0 0
- Python常用数据类型方法调用
- Python数据类型转换常用方法
- python 基本数据类型常用方法总结
- python常用结构数据类型
- Python常用数据类型
- python笔记(一)常用数据类型和方法
- Javascript 数据类型常用方法
- [Python]常用的组合数据类型
- python常用数据类型及其使用
- Python基础I---常用数据类型
- python学习之常用数据类型
- Python数据类型及常用集合
- Python常用数据类型和常用运算
- javascript数据类型及其常用方法
- 调用webservise常用方法
- python和php的基本数据类型的定义赋值及常用方法函数
- python数据类型list方法整理
- python如何调用C, 如何注册成C的回调函数(python后台程序常用方法)
- html form表单验证和用户体验代码
- Letcode 205[easy]--- Isomorphic Strings. Isomorphic Strings
- 关于ROS找不到package的问题
- 关于mysql数据库单表分区的操作
- Python scrapy 爬取拉勾网招聘信息
- Python常用数据类型方法调用
- 解决Android studio 3.0 apt插件问题
- linux 安装库 编译提示-lcrypto 无法链接,缺少 crypto 库
- ./utserver: error while loading shared libraries: libcrypto.so.0.9.8: cannot open shared object file
- lambda与算法模板学习总结
- 查询优化技术概念
- 关于C语言feof()函数判断文件结尾的问题分析
- 杭电ACM OJ 1032 The 3n + 1 problem 数据很弱,但是也需要思考一个方向
- 错误: 编码GBK的不可映射字符