【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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,790评论 0 33
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,139评论 19 139
  • Python 是一种相当高级的语言,通过 Python 解释器把符合语法的程序代码转换成 CPU 能够执行的机器码...
    Python程序媛阅读 1,967评论 0 3
  • Python内置函数详解——总结篇 ** 引 言** ** 数学运算** abs:求数值的绝对值>>> abs...
    yutiansut阅读 801评论 0 1
  • 好看呢!不过奶奶曾经说过“今天太阳反照,明天晒得鬼叫”不晓得明天又有好热火哦!
    数字达人阅读 309评论 0 0