Python function exercises

Source: Internet
Author: User

1. Differences between common parameters, specified parameters, default parameters, dynamic parameters

The common parameter is the common form of the pass argument.

def F (a):     = a+1    return= f (3)

Specify parameters, insert parameters sequentially, or specify parameters by "=".

def f (a,b,c)     = a+b+c# defaults to specifying the parameter F (All-in-one)# or specifying the parameter F (c=1,b=2,a =3)

The default parameter, which does not need to be specified, is given by default.

Dynamic Parameters ,

*args  means that when we need to pass in multiple parameters, we can use *args to represent multiple parameters, instead of specifying multiple parameters in parentheses.
**kwargs, we can use **kwargs when we need to pass in the parameter of the key-value pair type.
def F (a,*args,**Kwargs)##万能模式, you can insert a single parameter, multiple parameters, key-value pair parameters
2. Write the function to calculate the number of "number", "Letter", "space" and "other" in the incoming string
defF1 (a): Num_num= 0#Number of digitsNUM_ABC = 0#Number of lettersNum_space = 0#Number of spacesNum_else = 0#Other number     forIinchA:ifi.isdigit (): Num_num+=1elifI.isalpha (): Num_abc+=1elifi.isspace (): Num_space+=1Else: Num_else+=1returnNum_num,num_abc,num_space,num_elseli= F1 ("Zifuchuan")Print(LI)
3, write function, to determine whether the user incoming objects (string, list, tuple) length is greater than 5.
def F3 (ARG):     if len (ARG) >5:        return  True    Else:          return= F2 ({'a':'b'})  Print(ret)
4. Write function to check whether each element of the user's incoming object (string, list, tuple) contains empty content.
def f4 (*args):      for inch args:         if "" :             return True     return  = F2 ([11,22,"",])print(ret)
5, write the function, check the length of the incoming list, if greater than 2, then only the first two length of the content, and return the new content to the caller.
defF5 (*args): Li=list () Count=0ifLen (ARG) >2:         forIinchArgs:count+=1li.append (i)ifCount ==2:                 Break        returnLiElse:       returnArgret= F2 (11,22,33)Print(ret)
6. Write the function, check the element that gets all the odd bit indexes of the incoming list or tuple object, and return it to the caller as a new list.
def f6 (*args):    = 0    = []    for in  args:        +=1        if count%2 ==1:            li.append (i)    return= f6 ( 11,22,33,44)print(ret)
7, write the function, check the length of each value passed into the dictionary, if greater than 2, then only the first two length of the content, and the new content is returned to the caller.
defF7 (ARG): RET= {}     forKey,valueinchArg.items ():ifLen (value) > 2: Ret[key]= Value[0:2]        Else: Ret[key]=valuereturnRetdic= {"K1":"v1v1","K2": [11, 22, 33, 44],"K3":" A"}r=F7 (DIC)Print(r)
8, write the function, use recursion to get the 10th number in the Fibonacci sequence, and return the value to the caller.
def F8 (a1,a2,count):     +=1    = A1 + A2    if count ==10:        Print(A1)         return     F8 (a2,a3,count) F8 (0,1,0)

Python function exercises

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.