python-Basics-Functions

Source: Internet
Author: User

1 definition and invocation of function 1.1 function definition

1.2 Invocation of functions

2 function parameters

>>> def fun (A, B, *args, * *Kwargs): ..."""variable Parameter Demonstration example""".. print"A =", a ... print"B =", b ... print"args =", args ... print"Kwargs:"...      forKey, Valueinchkwargs.items (): .... print key,"=", Value ...>>> Fun (1,2,3,4,5, m=6, n=7, p=8) # Note that the passed parameters correspond to a=1b=2args= (3,4,5) Kwargs:p=8m=6N=7>>>>>>>>>>>> C = (3,4,5)>>> d = {"m":6,"N":7,"P":8}>>> Fun (1,2, *c, * *d) # Note the way a tuple and dictionary are passed=1b=2args= (3,4,5) Kwargs:p=8m=6N=7>>>>>>>>>>>> Fun (1,2, C, D) # Note the difference between the asterisk and the above=1b=2args= ((3,4,5), {'P':8,'m':6,'N':7}) Kwargs:>>>>>>

3 function return value

4 local variables and global variables

Local variables

Global variables

Summarize:

    • Variables defined outside the function are called全局变量
    • Global variables can be accessed in all functions
    • If you modify a global variable in a function, you need to global declare it, or else an error
    • If the name of the global variable is the same as the name of the local variable, then the local variable is used, and the tip强龙不压地头蛇

Summary 2:

    • The nature of global variables cannot be modified when global variables are declared without using global in a function, which means that global variables cannot be modified to point to new data. lists and dictionaries can be declared without Globa.
    • For global variables of an immutable type, global variables cannot be modified without using global because the data they point to cannot be modified.
    • For variable-type global variables, global variables can also be modified without using global because the data they point to can be modified.
5 Anonymous functions

python-Basics-Functions

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.