Built-in functions
Example: If you return the absolute value of a number, the Write function is very inconvenient [[email protected] tools]# python fa.py 10[[email protected] tools]# cat fa.py #!/usr/bin/pythond EF A (x): if x < 0:return-x return x n = a ( -10) print n
#帮助查看 #
>>> Help (Len)
Usage: Help (name of function)
ABS () return absolute value
>>> ABS (-10) 10
Max () returns the maximum value, min () returns the minimum value
>>> l=[1,4,7,12,555,66,]>>> min (l) 1>>> Max (l) 555
Len () gets the string length
>>> s = ' Konglingchao ' >>> len (s) 12
Divmod () seeking for remainder usage: divmod (divisor, divisor)
>>> Divmod (10,2) (5, 0)
pow () Operation usage: POW (number 1, number 2,/(number 3)) number 3 optional
>>> Pow (3,3) 27>>> pow (3,3,2) 1
Round () return floating-point number
>>> round (1112) 1112.0
Callable () tests whether a function is defined
>>> callable (min) True #函数定义返回True >>> callable (f) Traceback (most recent call Last): File "<stdin>", line 1, in <module>nameerror:name ' F ' are not defined #函数未被定义 >>> f=100 #f是一个变量 >>> callable (f) False #返回True
Isinstance ()
>>> print L[1, 4, 7, 555, 66]>>> print f<function f at 0x1a1e320>>>> type (f) <type ' function ' >>>> type (l) <type ' list ' > >>> if Type (l) ==type ([]):. print ' OK ' ... ok #判断 Whether to use Isinstance for the list to determine an object type >>> isinstance (l,int) false>>> isinstance (L,STR) false>>> Isinstance (l,list) True
CMP () string comparison
>>> CMP ("7", "7") 0>>> CMP ("7", "M") -1>>> CMP ("7", "6") 1
Range () Fast sequence generation
>>> Range (10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Xrange () can be understood as an object with a higher efficiency than range
>>> x = xrange (Ten) >>> print Xxrange (10)
Functions for data Conversion types:
int () integer
Long () length shaping
Float () Float type
Complex () plural type
STR ()
List ()
Tuple ()
>>> l=[1,2,3,4]>>> Tuple (L) (1, 4, 7, 12, 555, 66)
Note: The conversion of data should be selective
>>> s= ' Hello ' >>> int (s) Traceback (most recent call last): File "<stdin>", line 1, in <MODULE&G T Valueerror:invalid literal for int. () with base: ' Hello ' >>> s= ' 123 ' >>> type (s) <type ' str ' >> >> Int (s) 123
String function: function is limited to manipulation of strings
#查看帮助要 Add a function to the category:>>> Help (Str.replace)
Str.capitalize (): Capitalize first letter
>>> s= ' Hello ' >>> str.capitalize (s) ' Hello ' >>> s ' Hello word ' >>> s.capitalize () # S is the object in the string, s calls the Capitalize function ' Hello word '
str.replace (): Replace,(you can specify the number of replacements)
>>> s.replace (' Hello ', ' good ') ' good word ' >>> s ' Hello word ' #这里的good word is just a return value and does not change the data directly >> > ss= ' 12121212121212 ' >>> ss.replace (' 1 ', ' xxxxx ') ' xxxxx2xxxxx2xxxxx2xxxxx2xxxxx2xxxxx2xxxxx2 ' >> > Ss.replace (' 1 ', ' xxxxx ', 1) # Specify the number of substitutions ' xxxxx2121212121212 '
Str.split (): Cutting str. Split ("Parameter 1: Cut Suit", "parameter 2 = number of cuts")
>>> ip= "192.168.1.2"
>>> ip.split ('. ')
[' 192 ', ' 168 ', ' 1 ', ' 2 ']
>>> ip.split ('. ', 2)
[' 192 ', ' 168 ', ' 1.2 ']
#######################
Calling methods by importing modules
>>> Import String >>> Help (String.Replace) >>> s ' Hello word ' >>> string.replace (S, ' Hello ', ' good ') ' good word '
#####################################
Sequence processing functions
Len ()
Max ()
Min ()
#使用序列函数求数字的长度是无法使用的!!!!!
>>> Len (123) Traceback (most recent): File "<stdin>", line 1, in <module>typeerror:object of type ' int ' has no Len () >>>
Filter (Parameter 1: function, parameter 2: sequence)
Help on built-in function filter in module __builtin__:
Filter (...)
Filter (function or None, sequence), list, tuple, or string
#函数 or #空 sequence
Return those items of sequence for which function (item) is true. If
function is None, and return the items is true. If sequence is a tuple
or string, return the same type, else return a list.
Example:
>>> def f (x): ... if x>5: ... return True ... >>> f (Ten) true>>> F (3) >>> l=r Ange >>> l[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> Filter (f,l) [6, 7, 8, 9]# f function on List L
zip, map () List traversal
>>> name=[' Milo ', ' Zou ', ' Tom ']>>> age=[20,30,40]>>> tel=[' 133 ', ' 144 ', ' 155 ']>>> zip (Name,age,tel) [(' Milo ', 20, ' 133 '), (' Zou ', 30, ' 144 '), (' Tom ', 40, ' 155 ')] #并行遍历 >>> map (None,name,age,tel) [(' Milo ', 20, ' 133 ') , (' Zou ', 30, ' 144 '), (' Tom ', 40, ' 155 ')]>>> test=[' A ', ' B ']# If the value of one of the lists is less than the other list, it traverses the shortest list, >>> zip (name,age,tel,test) [(' Milo ', 20, ' 133 ', ' a ') , (' Zou ', 30, ' 144 ', ' B ')] if swapped for a map traversal, it fills the missing value of a list with none. >>> map (None,name,age,tel,test) [(' Milo ', 20, ' 133 ', ' a '), (' Zou ', 30, ' 144 ', ' B '), (' Tom ', 40, ' 155 ', none)] #此处的None也可以变为函数, the value of the subsequent operation. >>> a=[1,4,6,]>>> b=[7,8,9]>>> map (None,a,b) [(1, 7), (4, 8), (6, 9)]>>> def mf (x, y): &NBSP;&NBsp; print x*y... >>> map (mf,a,b) 73254[none, none, none]
reduce() recursive operations
>>> def f (x, y): ... return x+y >>> l= (range (1,101)) >>> reduce (f,l) 5050 or use anonymous function >>&G T Reduce (lambda x,y:x+y,l) 5050
This article from "thinking More than technology" blog, declined reprint!
Python Basic learning built-in functions