Python function basics

Source: Internet
Author: User

function is to encapsulate a piece of common code in the program, a name, the other parts of the program can be easily called

Definition of a function

def function name (parameter 1, parameter 2 ...)

Do something ...

return value/object, function, etc.

# CODING:GBK def Python_class (n):     Print (" Saturday has%s bit to learn python" % n    ) return Truepython_class (20)

Parameter type

1: Normal
2: Default parameter
3: Variable length parameter
# CODING:GBK def Test_args (a,b=1):    print(    b)print(A +b)    test _args (3) Test_args (3,2)

Variable length parameter : when calling a function, you can pass in any number of arguments in any form.

* On behalf of Ganso, all parameters with no key specified will be placed in a tuple.

* * means that the dictionary will put all the parameters of the specified key into the dictionary.

*args:tuple

**kwargs: That's when you pass in Key=value is a stored dictionary

# CODING:GBK def Test_args (*args,**Kwargs):    print(Args,kwargs)    Test_args (3,5,a=1 , b=2)

return value of the function

# CODING:GBK def Test (m,n):     return M,n Print (Test (1,2A))

Python function basics

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.