Python Built-in Functionsint('x',base=10) : x >> integer
long('x',base=10)  xL
bin():十进制转二进制 bin(2) = 0b10
oct():十进制转八进制
hex():十进制转十六进制 hex(2) = 0x2   hex(18) = 0x12
float.hex(1.5) = 0x1.80000000p+0
chr(113) = 'q'
unichr(113) = u'q'
ord('q') = 113
round(number,ndigits=0):Return the floating point value number rounded to ndigits digits after the decimal point.(四舍五入)
math.trunc(x) : 截取整数部分保留
math.floor(x) : 不大于x的最大整数
math.ceil(x) : 不小于x的最小整数

id(object):return the identity of an object. integer (unique, constant). It is the address of the object in memory.
a = 1;id(a) = 15500
b = a;id(b) = 15500
c = 1;id(c) = 15500

l = [1,2];id(l) = 44489704
s = l;id(s) = 44489704
t = l[:],id(t) = 44489664

def enumerate(sequence, start=0):
    n=start
    for item in sequence:
        yield n,item
        n += 1

seasons = ['spring','summer','fall','winder']
list(enumerate(seasons))
>>>[(0, 'spring'), (1, 'summer'), (2, 'fall'), (3, 'winder')]
list(enumerate(seasons,start=1))
>>>[(1, 'spring'), (2, 'summer'), (3, 'fall'), (4, 'winder')]

isinstance('s',basestring) = True
isinstance(1,basestring) = False
isinstance(object,(str,unicode)
isinstance(object,classinfo)
issubclass(class,classinfo)

cmp(x,y) if x<y,-1;if x>y,1;if x=y,0
divmod(a,b) = (a // b,a % b)

dir():show the names in the current module namespace
dir(os):show the names in the struct module
delattr(object, name) is equal to del object.name
setattr(object, name)
getattr(object, name[,default])
hasattr(object, name)

a = input('enter') : int
b = raw_input('enter') : str
c = int[eval](raw_input('enter')) : int
from __future__ import print_function

list('abc): ['a','b','c']
list((1,2,3)):[1,2,3]
a = []:a = list()
range(stop) = range(0:stop:1)
range(start,stop,step=1)
tuple()

file
open(name[,mode[,buffering]])

reduce(function,iterable,initializer)
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 连加
zip() zip(*)
x = [1,2,3];y = [4,5,6]
zipped = zip(x,y)
>>>[(1,4),(2,5),(3,6)]
x2,y2 = zip(*zipped)
x == list(x2) and y == list(y2)
>>>True

































相关文章: