Well, today we are talking about this. Today's main content is about functions.
Function: encapsulate the function or action Syntax: def function name (form parameter): Return Value of the function body function name (real parameter), terminate the function operation 1. no return is written in the function. By default, none2. is returned at the end of the function. The function only writes return to return none3. return value has a return value. 4. return Value 1, value 2, value 3 .... multiple return values are returned to the caller.
Function parameters (Part)
Parameter: variable at the position of function declaration 1. location Parameter 2. the default value is 3. hybrid location. Default Value: 4. dynamically passing parameters, * ARGs, ** kwargs real parameters: the specific value passed to the function during function call 1. location Parameter 2. keyword parameter 3. mixed Parameter order: location, keyword parameter passing: the process of assigning real parameters to the form parameter
Job ### 1. Sort out function-related knowledge points and write a blog. 2. Write a function to 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.
Def measure (LST ):
Lst2 = []
For N in lst:
If n % 2! = 0 and n! = 0:
Lst2.append (N)
Return lst2
LST = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Print (measure (LST ))
3. Write a function to determine whether the length of the object (string, list, And tuples) imported by the user is greater than 5.
Def measure (s ):
If Len (s)> 5:
Return ('ture ')
Else:
Return ('falus ')
Print (measure ('20140901 '))
4. Write the function and check the length of the input list. If the value is greater than 2, the first two items of the list are returned to the caller.
Def compare (LST ):
If Len (LST)> 2:
Return lst [: 2]
LST = [1, 2, 3]
Print (compare (LST ))
5. Write a function and calculate the numbers, letters, and spaces in the input function string.
And
The number of other contents, and return results.
Def count (s ):
Digit = 0
Alpha = 0
Space = 0
Other = 0
For N in S:
If n = '':
Space + = 1
Elif N. isalpha ():
Alpha + = 1
Elif N. isdigit ():
Digit + = 1
Else:
Other + = 1
Return digit, Alpha, space, other
S1 = 'asdasfesadasdff85 2456 # $ % ^ &*('
Digit, Alpha, space, other = count (S1)
Print (digit, Alpha, space, other)
6. Write a function to receive two numeric parameters and return a relatively large number.
Def compare (A, B ):
If A> B:
Return
If B>:
Return B
Else:
Return '='
K = compare (1, 1)
Print (k)
7. Write the 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, 44]}
PS: the value in the dictionary can only be a string or a list.
Def count (DIC ):
Dit = {}
For N, J in DIC. Items ():
If Len (j)> 2:
Dit [N] = J [: 2]
Return dit
Print (count (DIC ))
8. Write a function. This function only receives one parameter and must be of the List data type. This function is used to return a dictionary to the caller, the key-value pairs of this dictionary are the indexes and corresponding elements of this list. For example, the input list is: [11, 22, 33]
The returned dictionary is
{0: 11, 1: 22, 2: 33 }. NC ('A', 'B ')
Def change (LST ):
Dit = {}
I = 0
For N in lst:
Dit ['% d' % (I)] = N
I + = 1
Return dit
LST = [11, 22, 33]
Print (Change (LST ))
9. Write a function. The function receives four parameters: name, gender, age, and education. The user enters the four content and passes the four content into the function. The function receives the four content and appends the content to a student_msg file.
Def save (name, sex, age, Edu ):
Dit = {}
Dit ['name'] = Name
Dit ['sex'] = sex
Dit ['age'] = Age
Dit ['edu'] = edu
With open (r 'syudent _ MSG ', 'A', encoding = 'utf-8') as F:
F. Write (STR (DIT) + '\ n ')
Save ('J', 'M', '50', 'bk ')
10. Upgrade to question 9th: users can continue to enter Q or Q, and the default gender is male. If a female student is encountered, the gender is entered into female.
Def IP (name, sex = 'male '):
K = Print (name, sex)
While 1:
Name = input ('enter your name and enter Q to exit ')
If name. Upper () = 'q ':
Break
Sex = input ('enter your gender ')
If sex = 'female ':
IP (name, sex)
Else:
IP (name)
11. Write a function, pass in the modified file name, and the content to be modified. Execute the function to modify the file (upgrade question ).
Def Rename (name, content ):
With open ('% s' % (name), 'R', encoding = 'utf-8') as F:
N = f. Read ()
Return N. Replace (content, 'asdasdasda ')
12. Write a function to complete the three-time login function, and then write another function to complete the registration function (upgrade question)
Def sign ():
N = 3
Name1 = 1
Password1 = 2
While N:
Name = input ('name ')
Password = input ('Password ')
If name = name1 and Password = password1:
Print ('logon successful ')
Break
N-= 1
If n = 0:
Print ('logon failed ')
Sign ()
Def register ():
Print ('You enter the registration function ')
Name = input ('enter your Username :')
Password = input ('enter your password ')
Print ('registered successfully ')
Register ()
Well, today is the thing.