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

[经验分享] Python 内建函数

[复制链接]

尚未签到

发表于 2018-8-7 08:28:32 | 显示全部楼层 |阅读模式
  Python内置函数:
  官方帮助文档:
  https://docs.python.org/2.7/
  返回数字的绝对值.
  def fun(x):
  if x < 0:
  return -x
  return x
  print fun(10)
  常用函数:
  abs()
  >>> abs(-100)
  100
  取列表最大值和最小值
  max()
  >>> max('1235',123)
  '1235'
  min()
  >>> min('asdfq3w45')
  '3'
  len()
  >>> len('abcdf')
  5
  >>> len([1,3,4,5])
  4
  >>> len((1,3,4,5))
  4
  >>> len({1:3,2:5})
  2
  divmod()
  >>> help(divmod)
  Help on built-in function divmod in module __builtin__:
  divmod(...)
  divmod(x, y) -> (quotient, remainder)
  Return the tuple (x//y, x%y).  Invariant: div*y + mod == x.
  >>> divmod(5,2)
  (2, 1)
  pow()
  pow(...)
  pow(x, y[, z]) -> number
  With two arguments, equivalent to x**y.  With three arguments,
  equivalent to (x**y) % z, but may be more efficient (e.g. for longs).
  >>> pow(2,3)
  8
  >>> pow(2,3,3)
  2
  round()
  round(...)
  round(number[, ndigits]) -> floating point number
  Round a number to a given precision in decimal digits (default 0 digits).
  This always returns a floating point number.  Precision may be negative.
  >>> round(12.2)
  12.0
  >>> round(12.23)
  12.0
  >>> round(12.233,2)
  12.23
  callable()
  是否是可调用对象
  >>> a = 123
  >>> callable(a)
  False
  >>> def a():
  ...     pass
  ...
  >>> callable(a)
  True

  >>>>  ...     pass
  ...
  >>> callable(A)
  True
  type()
  判断类型
  >>> type(a)
  <type 'function'>
  isinstance()
  判断类型,
  >>> l =[1,2,3]
  >>> isinstance(l,list)
  True
  >>> isinstance(l,str)
  False
  >>> isinstance(l,(list,str))
  True
  判断是不是一个类
  >>> A
  <class 'A'>
  >>> a = A()
  >>> a
  <A object at 0x0379BE70>
  >>> isinstance(a,A)
  True
  cmp()
  >>> cmp(1,2)
  -1
  >>> cmp(1,0)
  1
  >>> cmp(1,1)
  0
  >>> cmp('a','ab')
  -1
  >>> cmp('a','a')
  0
  >>> cmp('helloa','hello')
  1
  range()
  >>> a = range(10)
  >>> a
  xrange()
  效率更高,不用时候不在内存中产生值
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  >>> b = xrange(10)
  >>> b
  xrange(10)
  >>> for i in b:print i
  ...
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
  int()
  >>> int(123.33)
  123
  long()
  >>> long(200)
  200L
  float()
  >>> float('123')
  123.0
  >>> float('123.0022')
  123.0022
  >>> float(123.0034)
  123.0034
  >>> float(123)
  123.0
  complex()
  转换成复数
  >>> complex(123)
  (123+0j)
  >>> complex(3.1415926)
  (3.1415926+0j)
  str()
  >>> str('123')
  '123'
  list()
  >>> list('123')
  ['1', '2', '3']
  tuple()
  >>> tuple('123')
  ('1', '2', '3')
  hex()
  变为16进制
  >>> hex(10)
  '0xa'
  >>> hex(10L)
  '0xaL'
  >>> int(0xaL)
  10
  eval()
  把字符串当成有效表达式求值。
  >>> eval('0xaL')
  10L
  >>> eval(&quot;[1,23,'a']&quot;)
  [1, 23, 'a']
  oct()
  10进制转成8进制
  >>> oct(10)
  '012'
  >>> oct(8)
  '010'
  chr()
  查ASSIC码对应值:
  >>> chr(97)
  'a'
  >>> chr(65)
  'A'
  ord()
  >>> ord('A')
  65
  字符串处理的函数:
  str.capitalize()
  首字母变大写:
  capitalize(...)
  S.capitalize() -> string
  Return a copy of the string S with only its first character
  capitalized.
  >>> s
  'hello'
  >>> s.capitalize()
  'Hello'
  str.replace()
  replace(...)
  S.replace(old, new[, count]) -> string
  Return a copy of string S with all occurrences of substring
  old replaced by new.  If the optional argument count is
  given, only the first count occurrences are replaced.
  >>> s = 'hello,h'
  >>> s.replace('h','H')
  'Hello,H'
  split()
  split(...)
  S.split([sep [,maxsplit]]) -> list of strings
  Return a list of the words in the string S, using sep as the
  delimiter string.  If maxsplit is given, at most maxsplit
  splits are done. If sep is not specified or is None, any
  whitespace string is a separator and empty strings are removed
  from the result.
  >>> s = 'hello a\tb\nc'
  >>> s
  'hello a\tb\nc'
  >>> s.split()
  ['hello', 'a', 'b', 'c']
  >> s
  'hello a\tb\nc'
  >>> s.split(' ')
  ['hello', 'a\tb\nc']
  >>> s.split('\t')
  ['hello a', 'b\nc']
  >>> ip = '192.168.1.1'
  >>> ip.split('.')
  ['192', '168', '1', '1']
  >>> ip.split('.',1)
  ['192', '168.1.1']
  >>> ip.split('.',2)
  ['192', '168', '1.1']
  join()
  >>> range(10)
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  >>> ''.join(str(i) for i in range(10))
  '0123456789'
  >>> int(''.join(str(i) for i in range(10)))
  123456789
  string:
  import string
  string.lower
  >>> string.lower('Kfdfa')
  'kfdfa'
  string.upper
  >>> string.upper('Kfdfa')
  'KFDFA'
  string.capitalize()
  >>> string.capitalize('adfafgh')
  'Adfafgh'
  string.replace()
  >>> string.replace('afkgha','a','A')
  'AfkghA'
  序列处理函数:
  len()
  max()
  min()
  filter()
  filter(...)
  filter(function or None, sequence) -> list, tuple, or string
  Return those items of sequence for which function(item) is true.  If
  function is None, return the items that are true.  If sequence is a tuple
  or string, return the same type, else return a list.
  >>> filter(None,range(10))
  [1, 2, 3, 4, 5, 6, 7, 8, 9]
  >>> def f(x):
  ...     if x % 2 == 0:
  ...         return True
  ...
  >>> filter(f,range(10))
  [0, 2, 4, 6, 8]
  >>> filter(lambda x: x%2==0,range(10))
  [0, 2, 4, 6, 8]
  zip()
  zip(...)
  zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
  Return a list of tuples, where each tuple contains the i-th element
  from each of the argument sequences.  The returned list is truncated
  in length to the length of the shortest argument sequence.
  >>> a1 = [1,3,4]
  >>> a2 = ['a','b','c']
  >>> zip(a1,a2)
  [(1, 'a'), (3, 'b'), (4, 'c')]
  >>> dict(zip(a1,a2))
  {1: 'a', 3: 'b', 4: 'c'}
  >>> dict(zip(a2,a1))
  {'a': 1, 'c': 4, 'b': 3}
  >>> a3 = ['x','y','z']
  >>> zip(a1,a2,a3)
  [(1, 'a', 'x'), (3, 'b', 'y'), (4, 'c', 'z')]
  >>> zip(a1,a3)
  [(1, 'x'), (3, 'y'), (4, 'z')]
  >>> a3 = ['x','y']
  >>> zip(a1,a3)
  [(1, 'x'), (3, 'y')]
  >>> zip(a1,a2,a3)
  [(1, 'a', 'x'), (3, 'b', 'y')]
  map()
  map(...)
  map(function, sequence[, sequence, ...]) -> list
  Return a list of the results of applying the function to the items of
  the argument sequence(s).  If more than one sequence is given, the
  function is called with an argument list consisting of the corresponding
  item of each sequence, substituting None for missing values when not all
  sequences have the same length.  If the function is None, return a list of
  the items of the sequence (or a list of tuples if more than one sequence).
  参数有几个,函数里的参数也应该对应有几个
  >>> map(None,a1,a2,a3)
  [(1, 'a', 'x'), (3, 'b', 'y'), (4, 'c', None)]
  >>> def f(x):
  ...     return x**2
  ...
  >>> map(f,a1)
  [1, 9, 16]
  >>> a1
  [1, 3, 4]
  >>> a1
  [1, 3, 4]
  >>> a2
  [2, 5, 6]
  >>> def f(x,y):
  ...     return x*y
  ...
  >>> map(f,a1,a2)
  [2, 15, 24]
  >>> map(lambda x,y: x*y ,range(1,10),range(1,10))
  [1, 4, 9, 16, 25, 36, 49, 64, 81]
  reduce()
  reduce(...)
  reduce(function, sequence[, initial]) -> value
  Apply a function of two arguments cumulatively to the items of a sequence,
  from left to right, so as to reduce the sequence to a single value.
  For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
  ((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
  of the sequence in the calculation, and serves as a default when the
  sequence is empty.
  >>> def f(x,y):
  ...     return x + y
  ...
  >>> reduce(f,range(1,101))
  5050
  列表表达式:
  [i*2 for i in range(10)]
  >>> [i*2 for i in range(10)]
  [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
  >>> [i*2+10 for i in range(10)]
  [10, 12, 14, 16, 18, 20, 22, 24, 26, 28]
  [i*2+10 for i in range(10) if i%3 == 0]
  >>> [i*2+10 for i in range(10) if i%3 == 0]
  [10, 16, 22, 28]

运维网声明 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-547922-1-1.html 上篇帖子: python---字符编码与转码 下篇帖子: python3 笔记
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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