Python's commonly used built-in functions

Source: Internet
Author: User
Built-in functions

build-in function, start the Python interpreter, enter Dir (__builtins__), and see many of the properties and functions that are loaded by default after the Python interpreter starts, which are called builtin functions, because they are used more in programming, The CPython interpreter implements these functions in C language, which is loaded by default when the interpreter is started.

These functions are numerous, not suitable for memory, development is not used, and then help (function), to see how to use, or combined with Baidu query can be introduced here some commonly used built-in functions. Range

    Range (stop)-> list of integers
    range (start, stop[, step))-> list of integers
Start: The count begins with start. The default is starting from 0. For example, range (5) is equivalent to range (0, 5); Stop: Ends with stop, but does not include stop. For example: Range (0, 5) is [0, 1, 2, 3, 4] without 5 step: the spacing of each jump is the default of 1. For example: Range (0, 5) is equivalent to range (0, 5, 1)

The Python2 range returns the list, and the Python3 range returns an iteration value. If you want a list, you can use

A = range (5)
list (a)

Another way to create a list

in [[]: Testlist = [x+2 to x in range (5)]

: Testlist
out[22]: [2, 3, 4, 5, 6]
map Function

The map function maps the specified sequence based on the provided function

    Map (...)
        Map (function, sequence[, sequence, ...])-> list
Functions: is a function sequence: is one or more sequences, depending on the function requires several parameters

The return value is a list

Each element in the parameter sequence calls the function function and returns a listcontaining the return value of each function functions.

#函数需要一个参数
Map (Lambda x:x*x, [1, 2, 3])
#结果为: [1, 4, 9]

#函数需要两个参数
Map (lambda x, Y:x+y, [1, 2, 3], [4, 5, 6]  ]
#结果为: [5, 7, 9]


def f1 (x, y): Return  
    (x,y)

L1 = [0, 1, 2, 3, 4, 5, 6]  
L2 = [' Sun ', ' M ', ' T ', ' W ', ' T ', ' F ', ' S ']
L3 = map (F1, L1, L2) 
print (list (L3))
#结果为: [(0, ' Sun '), (1, ' M '), (2, ' T '), (3, ' W ') , (4, ' T '), (5, ' F '), (6, ' S ')]
Filter Function

The filter function performs a filtering operation on the specified sequence

Filter (...)
    Filter (function or None, sequence)-> list, tuple, or string return

    those the items of sequence for which function (item ) is true.
    the If function is None and return the items that are true.  If sequence is a tuple
    or string, return the same type, else return a list.
Function: Accepts a parameter that returns a Boolean value of TRUE or false sequence: sequence can be str,tuple,list

The filter function calls the function function on each element in the sequence parameter sequence, and the last returned result contains the element that invokes the result to true.

The type of the return value is the same type as the parameter sequence

Filter (lambda x:x%2, [1, 2, 3, 4])
[1, 3]

filter (None, "she")
' she '
Reduce function

Reduce function, the reduce function accumulates elements in a parameter sequence

Reduce (...)
    Reduce (function, sequence[, initial])-> value

    Apply a function of two arguments cumulatively to the items of a SEQ Uence, from left
    to right, and 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)
    .  If Initial is present, it is placed before the items
    to the sequence in the calculation, and serves as a default The
    sequence is empty.
Function: It has two parameters sequence: sequence can be str,tuple,list initial: fixed initial value

reduce takes an element in turn from the sequence and invokes the function again with the argument of the last Call function. When a function is invoked for the first time, if the initial argument is supplied, the function is called with the first element in sequence and initial as a parameter, otherwise the function is invoked as the first two elements in the sequence sequence. Note that a function function cannot be none.

Reduce (lambda x, Y:x+y, [1,2,3,4])

Reduce (lambda x, Y:x+y, [1,2,3,4], 5)

Reduce (lambda x, y:x +y, [' AA ', ' BB ', ' cc '], ' dd ')
' DDAABBCC '

In Python3, the reduce function has been removed from the global namespace, and it is now placed in the Fucntools module to be introduced first: from functools import reduce sorted function

Sorted (...)
    Sorted (iterable, Cmp=none, Key=none, Reverse=false)--> new sorted list
In [1]: Sorted ([1, 4, 2, 6, 5, 3])
out[1]: [1, 2, 3, 4, 5, 6] in

[2]: Sorted ([1, 4, 2, 6, 5, 3],reverse = 1)
O UT[2]: [6, 5, 4, 3, 2, 1] in

[4]: sorted ([' dd ', ' AA ', ' cc ', ' BB '])
out[4]: [' AA ', ' BB ', ' cc ', ' DD '] in

[5]: So rted ([' DD ', ' AA ', ' cc ', ' BB '], reverse = 1)
out[5]: [' DD ', ' cc ', ' BB ', ' AA ']

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.