设为首页 收藏本站
查看: 1172|回复: 0

[经验分享] python基础-内置函数-作用域-闭包-递归-python3

[复制链接]
发表于 2018-8-10 08:37:44 | 显示全部楼层 |阅读模式
查看内置函数:  print(dir(__builtins__))
  常见函数:
  len 求长度
  min 最小值
  max 最大值
  sorted 排序,从小到大
  reversed 反向
  sum 求和
  进制转换:
  bin()  转换为二进制
  oct()   转换为八进制
  hex()   转换为十六进制
  ord()   将字符转换成对应的ASIIC码值
  chr()   将ASIIC码值转换成对应的字符
  补充:
  1.enumerate()   返回一个可以枚举的对象
  2.filter()      过滤器
  3.map()         加工。对于参数iterable中的每个元素都应用fuction函数,并返回一个map对象
  4.zip()         将对象逐一配对
  1.1 查看参数使用:
  >>> help(sum)
  Help on built-in function sum in module builtins:
  sum(iterable, start=0, /)
  Return the sum of a 'start' value (default: 0) plus an iterable of numbers
  When the iterable is empty, return the start value.
  This function is intended specifically for use with numeric values and may
  reject non-numeric types.
  >>> sum((1,23,4))
  28
  >>> sum([1,2,3])
  6
  >>> sum([1,2,3],10)
  16
  >>> sum([10,20,30],20)  #值=iterable值+start值
  80
  >>> sum([10,20,30],22)    #值=iterable值+start值
  82
  >>> sum({1:12,2:30})      #key相加
  3
  1.2 二进制:
  >>> bin(1)
  '0b1'
  >>> bin(2)
  '0b10'
  1.3 八进制:
  >>> oct(8)
  '0o10'
  >>> oct(12)
  '0o14'
  1.4 十六进制:
  >>> hex(10)
  '0xa'
  >>> hex(9)
  '0x9'
  >>> hex(15)
  '0xf'
  1.5 将ASIIC码转换成相应的字符
  >>> chr(65)
  'A'
  >>> chr(32)
  ' '
  1.6 将字符转换成ASIIC码
  >>> ord('a')
  97
  >>> ord(' ')
  32
  1.7 enumerate:
  >>> help(enumerate)

  Help on>  class enumerate(object)
  |  enumerate(iterable[, start]) -> iterator for index, value of iterable
  |
  |  Return an enumerate object.  iterable must be another object that supports
  |  iteration.  The enumerate object yields pairs containing a count (from
  |  start, which defaults to zero) and a value yielded by the iterable argument.
  |  enumerate is useful for obtaining an indexed list:
  |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
  |
  |  Methods defined here:
  |
  |  __getattribute__(self, name, /)
  |      Return getattr(self, name).
  |
  |  __iter__(self, /)
  |      Implement iter(self).
  |
  |  __new__(*args, **kwargs) from builtins.type
  |      Create and return a new object.  See help(type) for accurate signature.
  |
  |  __next__(self, /)
  |      Implement next(self).
  |
  |  __reduce__(...)
  |      Return state information for pickling.
  >>> enumerate([1,2,3,4])
  <enumerate object at 0x000000000343EF78>      #返回一个迭代器
  >>> list(enumerate([1,2,3,4]))                    #查看
  [(0, 1), (1, 2), (2, 3), (3, 4)]              #返回一个带索引的可枚举对象,index默认0,也可指定
  >>> list(enumerate(['a','b','c','d']))
  [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]      #返回一个带索引的可枚举对象,index默认0,也可指定
  >>> list(enumerate(['a','b','c','d'],3))
  [(3, 'a'), (4, 'b'), (5, 'c'), (6, 'd')]
  >>> list(enumerate((1,23,4,5,6),3))
  [(3, 1), (4, 23), (5, 4), (6, 5), (7, 6)]
  >>> list(enumerate({1,2,3,4,5},3))        #返回一个伪索引
  [(3, 1), (4, 2), (5, 3), (6, 4), (7, 5)]
  >>> list(enumerate({1:2,2:3,3:4},3))      #按可以返回
  [(3, 1), (4, 2), (5, 3)]
  1.8 filter 过滤器
  >>> help(filter)

  Help on>  class filter(object)
  |  filter(function or None, iterable) --> filter object
  |  Return an iterator yielding those items of iterable for which function(item)
  |  is true. If function is None, return the items that are true.
  |  Methods defined here:
  |  __getattribute__(self, name, /)
  |      Return getattr(self, name).
  |  __iter__(self, /)
  |      Implement iter(self).
  |  __new__(*args, **kwargs) from builtins.type
  |      Create and return a new object.  See help(type) for accurate signature.
  |  __next__(self, /)
  |      Implement next(self).
  |  __reduce__(...)
  |      Return state information for pickling.
  >>> filter(lambda x:x>2,[1,2,3,4,5])   #lambda x:x>2是个函数体
  <filter object at 0x0000000003420EB8>   #返回函数体
  >>> list(filter(lambda x:x>2,[1,2,3,4,5]))
  [3, 4, 5]
  >>> list(filter(None,[1,2,3,4,5]))
  [1, 2, 3, 4, 5]
  1.9 map加工
  >>> help(map)

  Help on>  class map(object)
  |  map(func, *iterables) --> map object
  |  Make an iterator that computes the function using arguments from
  |  each of the iterables.  Stops when the shortest iterable is exhausted.
  |  Methods defined here:
  |  __getattribute__(self, name, /)
  |      Return getattr(self, name).
  |  __iter__(self, /)
  |      Implement iter(self).
  |  __new__(*args, **kwargs) from builtins.type
  |      Create and return a new object.  See help(type) for accurate signature.
  |  __next__(self, /)
  |      Implement next(self).
  |  __reduce__(...)
  |      Return state information for pickling.
  >>> list(map(str,[1,2,3,4]))
  ['1', '2', '3', '4']
  1.10 zip 将对象逐一配对
  >>> list(zip([1,2,3],[4,5,6]))
  [(1, 4), (2, 5), (3, 6)]
  >>> list(zip((1,2,3),(4,5,6)))
  [(1, 4), (2, 5), (3, 6)]
  >>> list(zip([1,2,3],(4,5,6)))
  [(1, 4), (2, 5), (3, 6)]
  >>> list(zip([1,2,3,4],[5,6,7,8,9],['a','b']))
  [(1, 5, 'a'), (2, 6, 'b')]

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-549440-1-1.html 上篇帖子: Python学习笔记__9章 IO编程 下篇帖子: 利用virtualenv实现Python2和Python3共存,且ipython2和ipython3共存
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表