function usage in Python

Source: Internet
Author: User

1. Creation of functions

def # definition    Print ('hellow')    # Execution Code    of the function Retrun    1        # return value fun ()                  # Execute function

2. Parameters of the function

Common parameters: To enter parameters in order

def Fun (a,b,c):     Print (a)     Print (b)     Print (c)     return   afun (11,22,33)# output:One# output:#  Output:33

  

Specify parameters: Input parameters can not be entered in sequence

def Fun (a,b,c):     Print (a)     Print (b)     Print (c)     return   Afun (b=1,a=3,c=44)# output: 3   1   

Default parameter: parameter must be placed at the end of the parameter when there are default parameters

 def  Fun (A,b= "  old driver   "  =a+b   return   Temret  =fun ( " i am  "  )  print   #  Span style= "COLOR: #008000" > output: I am old driver 
#如果不想默认值
def fun (a,b=  old driver ):
Tem=a+b
return tem

Ret=fun ("I Am","SB")
Print (ret)

#输出: I am SB

Dynamic parameters

*args: Representing tuples

**kwargs: Represents a dictionary

def Fun (a,*args,**Kwargs):    print(A,args,kwargs) Fun (23,1,2,3,4,k1=33,k2= 5)# output (1, 2, 3, 4) {' K2 ': 5, ' K1 ':

 

function usage in Python

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.