Simple usage of built-in functions in Python and usage of python Functions
Python provides an inline module, buildin, which defines some common functions in software development. These functions can be used to convert data types, calculate data, and process sequences.
Built-in functions of the buildin module:
1. apply ():You can call the function of variable parameter list to store a parameter in a tuples or sequence. The apply tuples parameter must be consistent with the sum () parameter.
#!/usr/bin/python# -*- coding:utf8 -*-def sum(x=1,y=2): return x+yprint apply(sum,(1,3))
2. filter ():You can filter a sequence. The filtered func () parameter cannot be blank.
Filter (func or None, sequence)-> list, tuple, or string
#!/usr/bin/python# -*- coding:utf8 -*-def func(x): if x>0: return xprint filter(func,range(-9,10))
3. reduce ():Continuous operations on elements in a sequence can be processed cyclically, with the function of continuous processing.
reduce(func,sequence[,initial]) –> value
Func is a user-defined function. In func (), sequential operations are performed on the parameter sequence. sequence is a sequence to be processed. If the Parameter
If the value of initial is not null, it is first passed into the function func () for calculation. If it is null, the value of initial is processed.
#!/usr/bin/python# -*- coding:utf8 -*-def sum(x,y): return x + yprint reduce(sum,range(0,10))print range(0,10)print reduce(sum,range(0,10),10)print reduce(sum,range(0,2),10)
4. map ():You can perform the same operation on each element of Multiple sequences and return the result in a list.
If multiple sequences are provided, the elements in each sequence correspond to each other for calculation. If the length of each sequence is different,
Then the short sequence is supplemented with "None" and then computed.
map(func,sequence[,sequence,…]) –> list#!/usr/bin/python# -*- coding:utf8 -*-def power(x): return x**xprint map(power,range(1,5))def power2(x,y): return x**yprint map(power2,range(1,5),range(5,1,-1))print range(1,5)print range(5,1,-1)
PS: Common built-in module functions:
Abs (x) returns the absolute value of x.
Apply (func [, args [, kwargs]) Place the function parameters in the sequence and input the Function
Bool ([x]) converts each value or expression to the bool type. If expression x is a value, True is returned; otherwise, False is returned.
Cmp (x, y) compares the sizes of x and y
Delattr (obj, name) is equivalent to del obj. name
Eval (s [, globals [, locals]) calculates the value of the expression.
Float (x) converts a number or string to float data.
Hash (object) returns the hash value of an object.
Help ([object]) returns the help description of inline functions
Id (x) returns the identifier of an object
Input ([prompt]) accepts input from the console and converts the input value to a number
Int (x) converts a number or string to an integer.
Number of elements contained in the len (obj) object
Range ([start,] end [, step]) produces a list and returns
Raw_input ([prompt]) accepts input from the console and returns the string type.
Reduce (func, sequence [, initial]) is used to accumulate the values of sequences.
Round (x, n = 0) Rounding Function
Set ([interable]) returns a set
Sorted (iterable [, cmp [, key [, reverse]) returns a sorted list.
Sum (iterable [, start = 0]) returns the sum
Type (obj) returns the type of an object.
The xrange (start [, end [, step]) function is similar to range (), but a value is returned at a time.
Zip (seq1 [, seq2,…]) Returns n sequences as list elements.