常用函数
来源:互联网 发布:js触发不显示事件 编辑:程序博客网 时间:2024/05/16 11:03
abs(x)
abs()返回一个数字的绝对值。如果给出复数,返回值就是该复数的模。
>>>print abs(-100)100>>>print abs(1+2j)2.2360679775
callable(object)
callable()函数用于测试对象是否可调用,如果可以则返回1(真);否则返回0(假)。可调用对象包括函数、方法、代码对象、类和已经定义了“调用”方法的类实例。
>>> a="123">>> print callable(a)0>>> print callable(chr)1
cmp(x,y)
cmp()函数比较x和y两个对象,并根据比较结果返回一个整数,如果x<y,则返回-1;如果x>y,则返回1,如果x==y则返回0。
>>>a=1>>>b=2>>>c=2>>> print cmp(a,b)-1>>> print cmp(b,a)1>>> print cmp(b,c)0
divmod(x,y)
divmod(x,y)函数完成除法运算,返回商和余数。
>>> divmod(10,3)(3, 1)>>> divmod(9,3)(3, 0)
isinstance(object,class-or-type-or-tuple) -> bool
测试对象类型
>>> a='isinstance test'>>> b=1234>>> isinstance(a,str)True>>> isinstance(a,int)False>>> isinstance(b,str)False>>> isinstance(b,int)True
len(object) -> integer
len()函数返回字符串和序列的长度。
>>> len("aa")2>>> len([1,2])2
pow(x,y[,z])
pow()函数返回以x为底,y为指数的幂。如果给出z值,该函数就计算x的y次幂值被z取模的值。
>>> print pow(2,4)16>>> print pow(2,4,2)0>>> print pow(2.4,3)13.824
range([lower,]stop[,step])
range()函数可按参数生成连续的有序整数列表。
>>> range(10)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> range(1,10)[1, 2, 3, 4, 5, 6, 7, 8, 9]>>> range(1,10,2)[1, 3, 5, 7, 9]
round(x[,n])
round()函数返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。
>>> round(3.333)3.0>>> round(3)3.0>>> round(5.9)6.0
type(obj)
type()函数可返回对象的数据类型。
>>> type(a)<type 'list'>>>> type(copy)<type 'module'>>>> type(1)<type 'int'>
xrange([lower,]stop[,step])
xrange()函数与range()类似,但xrnage()并不创建列表,而是返回一个xrange对象,它的行为与列表相似,但是只在需要时才计算列表值,当列表很大时,这个特性能为我们节省内存。
>>> a=xrange(10)>>> print a[0]0>>> print a[1]1>>> print a[2]2
0 0
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- 常用函数
- C++ I/O
- 总计门路说嘛说主中早退
- STL学习之list
- folder错误frm-41097,frm-41099
- MFC系统剪切板
- 常用函数
- 提升开发效率的十个工具
- 23种设计模式之python实现--观察者模式
- drawer navigation, tabhostFragment 默认导向
- gcc的参数 -I和-L 的用法
- JAVA虚拟机内存分配与回收机制
- 1005. 继续(3n+1)猜想 (25)
- 【PyQt4实例12】Graphics Item 的各种变形
- EBS R12中如何使用CGI登录Form