Python develops "xxx article" function exercises

Source: Internet
Author: User

1. The differences between common parameters, specified parameters, default parameters and dynamic parameters are briefly described.

########## defining Functions ##########name is called the formal parameter of function func, abbreviation: parameterdeffunc (name):Printname########## execution Function ##########' Wupeiqi ' is called the actual argument of function Func, referred to as: argumentFunc'Wupeiqi')defFunc (name, age = 18):        Print "%s:%s"%(name,age)#Specifying ParametersFunc'Wupeiqi', 19)#using default ParametersFunc'Alex'Note: Default parameters need to be placed at the end of the parameter listdefFunc (*args):Printargs#execution Mode oneFunc (11,33,4,4454,5)#mode of execution twoLi = [11,2,2,3,3,4,54]func (*Li)defFunc (* *Kwargs):Printargs#execution Mode oneFunc (name='Wupeiqi', age=18)#mode of execution twoLi = {'name':'Wupeiqi', Age:18,'Gender':'male'}func (**Li)defFunc (*args, * *Kwargs):PrintargsPrintKwargs

2, write function, calculate the number of "number", "Letter", "space" and "other" in the incoming string

def My_len (my_def):2      my_def.split ()3      return  len (my_def)4 res = My_len (  'hfweiie8832  fej Chinese ')print(res)

3, write function, to determine whether the user incoming objects (string, list, tuple) length is greater than 5.

1 defMy_fun (func):2My_count =03      forIinchfunc:4My_count = My_count + 15     ifMy_count > 5:6         Print('The length of the object you passed in is greater than 5')7     Else:8         Print('The length of the object you passed in is not greater than 5')9 TenMy_fun ([1, 2,'JDI','English'])

4. Write function to check whether each element of the user's incoming object (string, list, tuple) contains empty content.

1 defMy_fun (func):2      forIinchfunc:3         ifi.isspace ():4              Print('This element contains empty content')5         Else:6             Continue7 8My_fun ([' ',' A','3','4','DIW'])

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.

1 defMy_fun (func):2My_count =03      forIinchfunc:4My_count = My_count + 15     ifMy_count > 2:6Func = Func[0:2]7         returnfunc8res = My_fun ([A.'Edj'])9 Print(RES)

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.

1 defMy_fun (func):2My_count =03Func1 = []4      forIinchfunc:5My_count = My_count + 16         ifMy_count% 2 = =0:7 func1.append (i)8     Print(FUNC1)9My_fun ([A.'Edj'])
View Code

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.

1 defmy_dict (DIC):2      forKeyinchDIC:3         ifLen (Dic[key]) > 2:4Dic[key] = Dic[key][0:2]5         Else:6             Continue7     returnDiC8DIC = {"K1":"v1v1","K2": [11,22,33,44]}9res =my_dict (DIC)Ten Print(RES)
View Code

8, write the function, use recursion to get the 10th number in the Fibonacci sequence, and return the value to the caller.

1 defMy_feb (n):2My_count =03Arg1 = 14ARG2 = 15      forKinchRange (1, (n+1)):6         ifMy_count < n andMy_count >= 2:7My_count = My_count + 18Arg3 = Arg1 +arg29Arg1 =arg2TenArg2 =Arg3 One         Else: AMy_count = My_count + 1 -Arg3 = 1 -     returnArg3 theres = My_feb (10) - Print(RES)
View Code

Python develops "xxx article" function exercises

Related Article

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.