內建函數 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 bFalse
<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)