Python basic functions

Source: Internet
Author: User
Tags function definition

1, function definition: def fun ():

***

Return

Note: The function name is actually a reference to a function object, it is possible to assign the function name to a variable, which is equivalent to giving the function an "alias"

2. Function parameters: Default parameters:def Power(x, n=2): callable: Power (x) define default parameters keep in mind that the default parameter must point to the immutable object

Variable parameters: Variable parameters allow you to pass in 0 or any of the parametersdef Calc(*numbers): Calc (1, 3, 5, 7) or nums=[1, 3, 5, 7] Calc (*nums)
Keyword parameter: The keyword parameter allows you to pass in 0 or any parameter with parameter names that automatically group a dict within the function. def person(name, age, **kw): Person (' Adam ', gender=,' M ', job=' Engineer ')
or extra = {' City ': ' Beijing ', ' job ': ' Engineer ' ('Jack ', ' **extra ' )
Named keyword parameters:def person (name, age, *, City, Job): City and job if you have a non-default parameter, you must pass in the parameter name

Note: The order of the parameter definitions must be: required, default, variable, named keyword, and keyword parameters.

3. High-order function #函数参数可以传入函数

4. Common functions

Help function

Use Help () to view the helper for the function. such as: Help (ABS)

Int () can convert other data types to integers. Similarly, float () str () bool () Hex ()

Isinstance (objet, ClassInfo) such as Isinstance (1,int) isinstance (' abc ', interable) #判断是否可迭代 (from Collectins Import interable)

Enumerate #将lis变成索引-element to for I, value in enumerate ([' A ', ' B ', ' C '])

Print (I, value)

Map (): The function receives two parameters, one is the function, the other is the function of the Iterable map passed in order to each element of the sequence, and the result as a new Iterator return

List (map (str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
Reduce (): A function is acting on a sequence, and the result continues and the next element of the sequence is accumulated (fromfunctools import reduce)

Reduce (f, [X1, x2, X3, x4]) = f (f (f (x1, x2), x3), x4)

Lambda anonymous function: Allows the user to quickly define a single-line function Log2=lambda x:log (x)/log (2)

Filter (): The function is used to filter the sequence, receive a function and a sequence, and then decide whether to True False retain or discard the element based on the return value.

List (filter (is_odd, [1, 2, 4, 5, 6, 9, Ten, 15]))
Sorted (): Sorted ([5, -+, 9,-+], key=abs)

Python basic functions

Related Article

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.