The function exercises of Python

Source: Internet
Author: User

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

Normal parameter: is to put a formal parameter, when put into the actual parameter, you need to give the parameter value in order.

Specify the parameter: When the argument is placed, it is specified, and the parameter can be obtained without having to give the formal parameter in order.

Default parameter: Specifies a parameter within the formal parameter that needs to be placed on the last side of the parameter. When an argument is not given a value, it defaults to the value of the formal parameter.

Dynamic Parameters: Format: *args and **kwargs the previous one is saved as a tuple, and the following one is saved as a dictionary.

Second, write the function, calculate the number of "number", "Letter", "space" and "other" in the incoming string
#!/bin/bash/env python#-*-coding:utf-8-*-#function: Write a function to calculate the number of "digits", "letters", "spaces", and "other" in the incoming string def func1 (p):    Digit_ Number = 0    space_number = 0    alpha_number = 0    else_number = 0 for    i in P:        if I.isdigit ():     #检查字符串是否只 Composed of numbers            Digit_number + = 1        elif i.isspace ():     #检查字符串是否只由空格组成            Space_number + = 1        elif i.isalpha ():   #检查字符串是否只由字母组成            Alpha_number + = 1        else:            else_number + = 1    return (digit_number,space_number , alpha_number,else_number) R = func1 ("Qwer  123") print (R) Results: (3, 2, 4, 0)
Third, write the function, determine whether the user incoming object (string, list, tuple) length is greater than 5
#!/bin/bash/env python#-*-coding:utf-8-*-#function: Write a function that calculates the number of digits, letters, spaces, and other in the incoming string def func1 (p):    i1 = Len ( p)    print (i1)    if I1 > 5:        print (' yes,the length higher than 5 ')    else:        print (' NO ') R = Func1 (( 11,22,33))

Results:

3
NO

Write functions to check whether each element of a user's incoming object (string, list, tuple) contains empty content
#!/bin/bash/env python#-*-coding:utf-8-*-#function: Write a function that checks whether each element of the user's incoming object (string, list, tuple) contains empty content def func1 (p,q,i):    if p = = "":        print (' string has ')    if q = = []:        print (' list has ')    if i = = ():        print ("tuple has") R = func1 ("123", [11,22], ()) Result: the tuple has
Write the function, check the length of the incoming list, if it is greater than 2, then just keep the first two length of the content, and return the new content to the caller
#!/bin/bash/env python#-*-coding:utf-8-*-#function: Write the function, check the length of the incoming list, if it is greater than 2, then just keep the first two length of the content, and return the new content to the caller def func1 (p):    i1 = Len (p)    if I1 > 2:        i2 = p[0:2]    return I2R = Func1 ([11,22,33,44,55]) print (R) Results: [11, 22]
Six, write function, check get incoming list or tuple object of all the odd digit index corresponding element, and return it as a new list to the caller
#!/bin/bash/env python#-*-coding:utf-8-*-#function: Write a function that checks for an element corresponding to all the odd bit indexes of an incoming list or tuple object and returns it as a new list to the caller Def func1 (P,Q):    result = [] for    i1 in range (Len (P)):        if i1% 2 = = 1:            result.append (P[i1]) for    i2 in range (len (q)): 
   if i2% 2 = = 1:            result.append (P[i2])    print (result) R = func1 ([11,22,33], (11,22,33)) Result: [22,22]

Write functions, check the length of each value passed in the dictionary, and if it is greater than 2, retain only the first two length of content and return the new content to the caller

DIC = {"K1": "V1v1", "K2": [11,22,33}}

PS: value in a dictionary can only be a string or list

#!/bin/bash/env python#-*-coding:utf-8-*-#function: Checks the length of each value passed into the dictionary, if greater than 2, preserves only the first two length of content, and returns the new content to the caller def func1 ( **P): For    Key,value in P.items ():        If Len (value) > 2:            p[key] = Value[0:2]    return PR = func1 (k1= "v1v1" , k2=[11,22,33,44,55]) print (R) Result: {' K1 ': ' v1 ', ' K2 ': [11, 22]}

  

The function exercises of 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.