Parameters of the Python function

Source: Internet
Author: User

#CODING=GBK#parameters of the Python function: including required parameters, default parameters, variable parameters, keyword parameters#1 Required parameters, that is, each time you must select the parameters to be entereddefABS1 (num):#Calculate Absolute Value    ifisinstance (Num,[int,float]):returnABS (num)Else :        RaiseTypeError ("num is isn't the right type") A= ABS (1)Print(a)
#2 default parameter, that is, the system default value is used every time if you do not enterdefSqrt_n (x,n=2):#computes the n-th-squareS=1 whileN>0:s=s*x N=n-1returnsPrint(Sqrt_n (5))Print(Sqrt_n (5,3))#When the default parameter is high, you can call it again to specify the value of a parameter.defStudent_enroll (name,age,city="Beijing", gender='male'): returnName,age,city,genderstudent1= Student_enroll ("Jim", 18) Student2= Student_enroll ("Obama", 18,gender="female")Print("St1 is:", Student1,'\nst2 is:', Student2)
#3 variable parameters, the number of arguments passed in is variable, they are called automatically assembled as a tuple example descriptiondefSum2 (*numbers):#calculate several numbers andSum2=0 forIinchnumbers:sum2=sum2+Ireturnsum2a= (1,5,2,6,2)#the use of list here is equally valid, the same wayPrint(Sum2 (*a))#A tuple is entered here, so the front of a plus * indicates a variable parameter, which can actually be writtenPrint(Sum2 (1,5,2,6,2))#orPrint(Sum2 (a[0],a[1],a[2],a[3],a[4]))
#4 keyword parameter, allow to pass in 0 or any parameter with parameter name, these keyword parameters are automatically assembled into a dictdefMatriculate (NAME,AGE,**KW):#the KW here is an abbreviation for keyword, and can also be substituted with other characters return(name,age,kw) S1= Matriculate ("Tom", 18) S2= Matriculate ("Jack", 18,city ='Beijing', gender ='male')Print('S1 is:', S1,'\ n','S2 is:', S2)#You can also define the keyword parameters in advance (use SIM instead), and then directly use thesim={' City':'Beijing','Job':'Teacher','Gender':'male'}S3= Matriculate ('Hu', 18,**SIM)Print(S3)
#examples on the web#The most amazing thing is that through a tuple and dict, you can also call the function:defFunc (A, B, c=0, *args, * *kw):Print('A ='A'B ='B'C ='C'args =', args,'kw =', kw) args= (1, 2, 3, 4) kw= {'x': 99}func (*args, * *kw)#so, for any function, it can be called in the form of Func (*args, **kw)

#Note" "The default parameters must be used with immutable objects, such as L[]=none, otherwise the run will have a logical error *args is a mutable parameter, args receives a tuple () **kw is the keyword parameter, receives a dict{} using *args and * * KW is a Python idiom, but it can also be used with other parameter names, but it's best to use idioms" "#part of the reference from the network, hereby declares

Parameters of the Python function

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.