1. The differences between common parameters, specified parameters, default parameters and dynamic parameters are briefly described.
1 ########## defining Functions #########2 3 #name is called the formal parameter of function func, abbreviation: parameter4 deffunc (name):5 Printname6 7 ########## execution Function #########8 #' Wupeiqi ' is called the actual argument of function Func, referred to as: argument9Func'Wupeiqi')Ten One defFunc (name, age = 18): A - Print "%s:%s"%(name,age) - the #Specifying Parameters -Func'Wupeiqi', 19) - #using default Parameters -Func'Alex') + - Note: Default parameters need to be placed at the end of the parameter list + A at defFunc (*args): - - Printargs - - - #execution Mode one inFunc (11,33,4,4454,5) - to #mode of execution two +Li = [11,2,2,3,3,4,54] -Func (*Li) the * defFunc (* *Kwargs): $ Panax Notoginseng Printargs - the + #execution Mode one AFunc (name='Wupeiqi', age=18) the + #mode of execution two -Li = {'name':'Wupeiqi', Age:18,'Gender':'male'} $Func (* *Li) $ - defFunc (*args, * *Kwargs): - the Printargs - PrintKwargs
View Code
2, write function, calculate the number of "number", "Letter", "space" and "other" in the incoming string
1 def My_len (my_def): 2 my_def.split (3) return Len (my_def) 4 res = My_len ('hfweiie8832 fej Chinese ')5 Print(res)
View Code
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'])
View Code
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'])
View Code
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)
View Code
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.
123 |
dic = { "k1" : "v1v1" , "k2" : [ 11 , 22 , 33 , 44 ]} PS:字典中的value只能是字符串或列表 |
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__ function Exercises