【python】第五周-复习Python内置函数

Python内置函数

Python内置了很多函数,他们都是一个个的.py文件,在python的安装目录可以找到。
列出一些常用的

列出一些常用的

  • abs, max, min, len, divmod, pow, round, callable,
  • isinstance, cmp, range, xrange, type, id, int()
  • list(), tuple(), hex(), oct(), chr(), ord(), long()
  • numList = [1, 2]
  • if type(numList) == type([]):
  • print "It is a list"
  • if isinstance(numList, list): # the same as above, return true
  • print "It is a list"
  • for i in range(1, 10001) # will create a 10000 list, and cost memory
  • for i in xrange(1, 10001)# do not create such a list, no memory is cost
  • str = 'hello world'
  • str.capitalize() # 'Hello World', first letter transfer to big
  • str.replace("hello", "good") # 'good world'
  • ip = "192.168.1.123"
  • ip.split('.') # return ['192', '168', '1', '123']
  • help(str.split)
  • import string
  • str = 'hello world'
  • string.replace(str, "hello", "good") # 'good world'
  • len, max, min
  • filter(function or none, sequence)

  • def fun(x):
  • if x > 5:
  • return True
  • numList = [1, 2, 6, 7]
  • filter(fun, numList) # get [6, 7], if fun return True, retain the element, otherwise delete it
  • filter(lambda x : x % 2 == 0, numList)
  • zip()

  • name = ["me", "you"]
  • age = [25, 26]
  • tel = ["123", "234"]
  • zip(name, age, tel) # return a list: [('me', 25, '123'), ('you', 26, '234')]
  • map()

  • map(None, name, age, tel) # also return a list: [('me', 25, '123'), ('you', 26, '234')]
  • test = ["hello1", "hello2", "hello3"]
  • zip(name, age, tel, test) # return [('me', 25, '123', 'hello1'), ('you', 26, '234', 'hello2')]
  • map(None, name, age, tel, test) # return [('me', 25, '123', 'hello1'), ('you', 26, '234', 'hello2'), (None, None, None, 'hello3')]
  • a = [1, 3, 5]
  • b = [2, 4, 6]
  • def mul(x, y):
  • return x*y
  • map(mul, a, b) # return [2, 12, 30]
  • reduce()

  • reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) # return ((((1+2)+3)+4)+5)

你可能感兴趣的:(【python】第五周-复习Python内置函数)