Simple usage of built-in functions in Python

Source: Internet
Author: User
Tags iterable set set
Python provides an inline module, Buildin, which defines some of the functions that are often used in software development, using these functions to transform data types, calculate data, and process sequences.

Built-in functions for the Buildin module:
1, apply (): can invoke a variable parameter list of the function, the parameter exists in a tuple or sequence, the apply tuple parameter must be consistent with sum () parameters

#!/usr/bin/python#-*-coding:utf8-*-def sum (x=1,y=2):  return x+yprint apply (sum, (1,3))

2. Filter (): a sequence can be filtered, where the func () parameter of the filter cannot be null.
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 (): the sequence of elements of continuous operation can be processed through the loop, with continuous processing function.

Reduce (func,sequence[,initial]) –> value

Func is a custom function that implements a continuous operation on the parameter sequence in func (), sequence the sequence to be processed, if the parameter
The value of initial is not NULL, the function func () is passed first for evaluation, and if empty, 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), ten) print reduce (Sum,range (0,2), 10)

4. Map (): You can perform the same operation on each element of multiple sequences and make a list return.
If more than one sequence is provided, the element one by one in each sequence corresponds to the calculation, and if the length of each sequence is different,
Then the short sequence is supplemented with "None" and then calculated

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]]) put the parameters of the function in the sequence to pass in the function
BOOL ([x]) converts each value or expression to a bool type and returns True if the expression x is a value, otherwise false
CMP (x, y) compares the size of X, y
Delattr (Obj,name) is equivalent to Del Obj.name
Eval (S[,globals[,locals]]) evaluates the value of an expression
Float (x) converts a number or string into a float type data
Hash (object) returns the hash value of an object
Help for helper ([object]) returns an inline function
ID (x) returns the label of an object
Input ([prompt]) takes input from the console and converts the input value into a number
int (x) converts a number or string to an integral type
The number of elements that the Len (obj) object contains
Range ([Start,]end[,step]) produces a list and returns
Raw_input ([prompt]) takes input from the console, returns the string type
Reduce (func,sequence[,initial]) calculates the value of a sequence
Round (x,n=0) rounding function
Set ([interable]) returns a set set
Sorted (Iterable[,cmp[,key[,reverse]]) returns a sorted list
SUM (iterable[,start=0]) returns a sequence of and
Type (obj) returns the types of an object
Xrange (Start[,end[,step]) function is similar to range () but returns one value at a time
Zip (seq1[,seq2,...]) returns n sequences as elements of a list


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.