1. Write a function to calculate the sum of incoming numeric arguments (dynamic passing)
def func_sum(x, y): return x + y # or lambda x,y:x+y
2. Write function user passes in the name of the modified file
The user passes in the name of the file to be modified and the content to be modified, and executes the function to complete the batch modification operation of the entire file.
import os def modify_file(file_name,content,newstr): new_file_name = '%sfile_name' %'new.' f_new = open(new_file_name, 'w') if (file_name): with open(file_name,'r+') as f: for line in f: if content in line: line = (content, newstr) f_new.write(line) f_new.close() (new_file_name, file_name) else: exit('file is not exist !!!')
3. Write functions to check the objects passed in by the user
User object.(Whether each element of the (string, list, tuple) contains null content.
def isNull(p_obj): for item in p_obj: if () == '': return True else: return False a = [' ','1','2'] b = ['5','1','2'] c = 'ab c' print(isNull(a)) print(isNull(c)) print(isNull(b))
4. Write a function to check the length of each value passed into the dictionary.
Example:If greater than 2, then only the first two lengths are retained and the new content is returned to the caller.
def two_len(**kwargs): for k, v in (): if len(v) > 2: kwargs[k] = v[:2] return kwargs print(two_len(x='12', y='345', c='byw'))
5. Closures
Internal functions that contain references to variables in external scopes rather than global scopes are called closures
6. Write a function to return a list of playing cards
The returned list of playing cards contains 52 items, each of which is a tuple
Example:[('Hearts', 2), ('Straw Flower', 2), ...('Ace of Spades')]
def cards(): type_li = ['Red Heart', 'Grass Flowers', 'Spades','Plum Blossom'] num = list(range(2, 11)) ('JQKA') return [(x, y) for x in type_li for y in num ] print(len(cards()), cards())
7. Write a function that passes in n numbers and returns a dictionary
Dictionary {'max': maximum value, 'min': minimum value}
def max_min_dic(*args): min_v = min(args) max_v = max(args) return {'max':max_v,'min':min_v} print(max_min_dic(2,3,6,7,9))
8. Write a function that passes a parameter n and returns the factorial of n
from functools import reduce def factorial(n): if n == 0: return 0 elif n == 1: return 1 else: return reduce(lambda x, y: x*y ,list(range(1, n))) print(factorial(5))
9. Writing decorators
Adding authentication to multiple functions (where the user's account password is derived from a file) requires one successful login, and subsequent functions do not need to re-enter the username and password.
user_dic={ 'user':None, 'is_authenticate':False } def read_file(): with open('','r') as f: s = ().strip(',') user_info = eval(s) return user_info def auth(user_info): username = input("account:").strip() password = input("password:").strip() print(user_info) if username in user_info['name'] and password in user_info['password']: print("success") user_dic['user'] = username user_dic['is_authenticate'] = True return user_dic else: print("Failure") return '' def login_required(func): def inner(*args, **kwargs): if args[0].get('is_authenticate'): ret = func(*args, **kwargs) else: exit('need authenticate') return ret return inner @login_required def print_info(acc_data): print('Enter') user_info = read_file() user_data = auth(user_info) print(user_data) print_info(user_data)
To this point this article on the use of Python functions related exercises to share the article is introduced to this, more related Python exercises content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!