首页 > 代码库 > python学习笔记11-python内置函数
python学习笔记11-python内置函数
python学习笔记11-python内置函数
一、查看python的函数介绍:
https://docs.python.org/2/library/
二、python内置函数
1、abs获取绝对值:
通过python官网查看abs
abs
(x)Return the absolute value of a number. The argument may be a plain or long integer or a floating point number. If the argument is a complex number, its magnitude is returned.
通过help查看abs
In [20]: help(abs)
Help on built-in function abs in module __builtin__:
abs(...)
abs(number) -> number #返回一个number
Return the absolute value ofthe argument. #返回绝对值
(END)
abs实例:
In [14]: def fun(x): ...: if x < 0: ...: return -x ...: return x ...: In [15]: fun(10) Out[15]: 10 In [16]: fun(-10) Out[16]: 10 In [17]: abs(-10) Out[17]: 10 In [18]: abs(10) Out[18]: 10 In [19]: abs(-100) Out[19]: 100
2、列表的最大值和最小值
In [22]: max([1,2,3,4,5]) Out[22]: 5 In [23]: min([1,2,3,4,5]) Out[23]: 1 Help on built-in function max in module __builtin__: max(...) max(iterable[, key=func]) -> value #可迭代的对象 max(a, b, c, ...[, key=func]) -> value With a single iterable argument, return its largest item.#返回最大的参数 With two or more arguments, return the largest argument. In [26]: max(‘hsdhsjd‘,‘5687‘,‘12‘) #最长的一个 Out[26]: ‘hsdhsjd‘
3、获取长度len()
In [27]: s=‘1234‘ In [28]: len(s) #取字符创的长度 Out[28]: 4 help(len) Help on built-in function len in module __builtin__: len(...) len(object) -> integer #处理对象,返回整数 Return the number of items of a sequence or collection. In [31]: len([1,2])#取列表的元素个数,2个 Out[31]: 2 In [30]: len((‘a‘,)) #取元祖的元素个数,1个 Out[30]: 1 In [32]: len({‘a‘:3,‘d‘:4}) #len()统计字典有几对元素 Out[32]: 2
4、商和余数divmod()
In [35]: help(divmod) divmod(...) divmod(x, y) -> (quotient, remainder) Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x. (END) In [36]: divmod(5,2) Out[36]: (2, 1) #商和余数
5、pow()次方&取余
In [37]: help(pow) Help on built-in function pow in module __builtin__: 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). (END) In [38]: pow(2,3) #两个参数,结果是x的y次方 Out[38]: 8 In [40]: pow(2,3,3) #三个参数,结果是x的y次方之后,再取个余,取模 Out[40]: 2 In [39]: pow(2,3,4) Out[39]: 0
6、round()
第一步把数字变为浮点数,
第二步,没有第二参数,把一个数字四舍五入,默认保留小数点后是.0 ,有第二个参数,第二个参数是保留几位小数
In [41]: help(round) Help on built-in function round in module __builtin__: 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. (END) In [53]: print round(12.145) 12.0 In [54]: print round(12.145,3) 12.145 In [55]: print round(12.145,4) 12.145
7、callable()对象是否可调用的?
In [63]: help(callable) Help on built-in function callable in module __builtin__: callable(...) callable(object) -> bool #返回一个bool值 Return whether the object is callable (i.e., some kind of function). Note that classes are callable, as are instances with a __call__() method. In [64]: a = 123 In [65]: callable(a) #字符a不可调用 Out[65]: False In [66]: def b(): ...: pass ...: In [67]: callable(b)#函数b可调用 Out[67]: True In [69]: class A(object): #定义了一个对象也是可调用的,返回true ...: pass ...: In [70]: callable(A) Out[70]: True
8、type()确定类型
In [66]: def b(): ...: pass In [73]: print type(b) <type ‘function‘> In [64]: a = 123 In [74]: print type(a) <type ‘int‘> In [75]: print type([]) <type ‘list‘>
本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1846987
python学习笔记11-python内置函数