2018-01-08 python 内置函数

内建函数 built-in functions 

http://docs.python.org

1.返回数字的绝对值

abs()

2.取列表最大最小值

max()

min()

3.其他函数

len()

divmod()

pow()

round()

s='123'
print(len(s))
print(len({'a':1,'b':2}))
print divmod(5,2)
print(pow(2,3))
print(pow(2,3,4))
print(pow(2,3,3))
print(round(2))
print(round(12.123,2))
print round(12.245,2)
print abs(-1)
print max(2,3)
print min(2,3)

结果:

3
2
(2, 1)
8
0
2
2.0
12.12
12.24
1
3
2

callable() 可调用的函数,判断一个函数是否可调用,返回一个布尔值

type() 查看变量类型

isnstance()判断一个对象是否是给定的类型,若是,返回真或假

cmp()比较两个对象,返回一个负数

range()返回的是一个列表

xrange()返回一个对象

a=1
print callable(a)

def b():
    print callable(b)
print type(a)
s='123'
print type(s)
print ({})

l=[1,2,3]
print type(l)
print isinstance(l,tuple)

class A(object):
    pass
a=A()
print isinstance(a,A)
print cmp(1,1)
print cmp(1,3)
print cmp('hello','hello1')
print cmp('za','aa22')
print range(10)
print xrange(10)

a=range(10)
print a

b=xrange(10)
print b
False
<type 'int'>
<type 'str'>
{}
<type 'list'>
False
True
0
-1
-1
1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
xrange(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
xrange(10)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章