Function exercises for Python and exercises for python

Source: Internet
Author: User

Function exercises for Python and exercises for python
1. Differences between common parameters, specified parameters, default parameters, and dynamic parameters

Common parameter: Put a form parameter. when put into the real parameter, the form parameter value needs to be given in order.

Specify parameters: the parameter is specified when it is placed into the real parameter. You do not need to give the parameter in order to get the corresponding parameters.

Default parameter: specify a parameter in the parameter, which must be placed at the end of the parameter. If no value is provided for a real parameter, the parameter value is used by default.

Dynamic parameters: format: the first one of * args and ** kwargs is saved as a tuple, and the last one is saved as a dictionary.

2. Write a function to calculate the number of numbers, letters, spaces, and other numbers in the input string.
#! /Bin/bash/env python #-*-coding: UTF-8-*-# function: Write function, calculate the number of numbers, letters, spaces, and other in the input string def func1 (p ): digit_number = 0 space_number = 0 alpha_number = 0 else_number = 0 for I in p: if I. isdigit (): # Check whether the string is composed of only digits: digit_number + = 1 elif I. isspace (): # Check whether the string is composed of spaces only. space_number + = 1 elif I. isalpha (): # Check whether the string is composed of letters only alpha_number + = 1 else: else_number + = 1 return (digit_number, space_number, alpha_number, else_number) r = func1 ("qwer 123") print (r) Result: (3, 2, 4, 0)
3. Write a function to determine whether the length of the object (string, list, And tuples) imported by the user is greater than 5
#! /Bin/bash/env python #-*-coding: UTF-8-*-# function: Write function, calculate the number of numbers, letters, spaces, and other numbers in the input string def func1 (p): i1 = len (p) print (i1) if i1> 5: print ('yes, the length higher than 5') else: print ('no') r = func1 (, 33 ))

Result:

3
NO

4. Write a function and check whether each element of the object (string, list, And tuples) imported by the user contains null content.
#! /Bin/bash/env python #-*-coding: UTF-8-*-# function: Write a function and check the input objects (strings, lists, and tuples) whether each element of def func1 (p, q, I): if p = "": print ('string') if q = []: print ('list exist') if I = (): print ("tuples have") r = func1 ("123", [], () Result: tuples have
5. Write a function and check the length of the input list. If the length is greater than 2, only the content of the first two lengths is retained and the new content is returned to the caller.
#! /Bin/bash/env python #-*-coding: UTF-8-*-# function: Write a function and check the length of the input list. If it is greater than 2, then, only the content of the first two lengths is retained and the new content is returned 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) Result: [11, 22]
6. Write a function. Check and obtain all elements corresponding to the odd-digit index of the input list or tuples, and return the elements to the caller as a new list.
#! /Bin/bash/env python #-*-coding: UTF-8-*-# function: Write a function and check the elements corresponding to all odd-digit indexes of the input list or tuples, return the 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: []

7. Write a function and check the length of each value in the input dictionary. If the value is greater than 2, only the content of the first two lengths is retained and the new content is returned to the caller.

Dic = {"k1": "v1v1", "k2": [11, 22, 33 }}

Ps: the value in the dictionary can only be a string or a list.

#! /Bin/bash/env python #-*-coding: UTF-8-*-# function: Check the length of each value in the input dictionary. If it is greater than 2, then, only the content of the first two lengths is retained and the new content is returned 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]}

  

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.