I. Built-in Functions
Following is a brief introduction of several:
1. Absolute value of ABS ()
2.all() Returns True if all elements of iterable are true (or if iterable is empty).
3.any() returns True if any element of iterable is true. If iterable is empty, return False
4.callable() If the object parameter is tunable, return True or False
5.divmod() takes two (non-complex) digits as parameters and returns a pair of digits consisting of quotients and residues when using integer division. For mixed operand types, the rules for binary arithmetic operators apply. For integers, the result is the same as (a//b,a%b). For floating-point numbers, the result is (q,a%b), where q is usually math.floor(a/b), but can be less than 1.
6. The enumerate () parameter must be an iterative object, and the result of the function operation yields an iterator, output elements and corresponding index values.
7.eval() extracts the string to execute
8.frozenset() immutable set, frozenset() defined set non-additive and deletable elements
9.globals() returns a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module that defines it, not the module from which it is invoked)
Round () rounds the parameters
11.sorted() sort without changing the original list
l=[1,2,4,9,-1] print(sorted(l)) #From small to large print(sorted(l,reverse=True)) #From big to small
12.zip() zipper function
Create an iterator that aggregates elements from each iterator.
Returns an iterator for a tuple, where the i-th tuple contains the i-th element from each parameter sequence or iteration. When the shortest input iteration is exhausted, the iterator stops. Using a single iteratable parameter, it returns an iterator of a tuple. Without parameters, it returns an empty iterator
13.max()
Returns the largest iteratable term or one of two or more parameters.
If a location parameter is provided, it should be an iterable. Returns the maximum item in the iteration. If two or more position parameters are provided, the maximum position parameter is returned.
max() can specify key (that is, specify the part to be compared)
14.map() mapping
Returns an iterator that applies each item from function to iterable to produce results
l=[1,2,3,4] m=map(lambda x:x**2,l) print(list(m)) ----->[1, 4, 9, 16]
15.reduce() merge
from functools import reduce res=0 for i in range(100): res+=i print(res)
16.filter() filters elements with Boolean values of True
names=['alex_sb','yuanhao_sb','wupeiqi_sb','egon'] print(list(filter(lambda name:name.endswith('_sb'),names)))--->['alex_sb', 'yuanhao_sb', 'wupeiqi_sb']
Detailed introduction of built-in functions can be referred to as follows: https://www.rddoc.com/doc/Python-3.6.0/library/functions/
2. Anonymous function (lambda expression)
def func(x): return x**2 print(func(2)) lambda x:x**2 #The upper function can be written directly in this form.
lambda function with return value
Anonymous functions can only replace some very simple functions, mainly in conjunction with other functions.
Another case is that some functions are defined only once and are not used. If they are not deleted, they will occupy memory space, and deletion will be cumbersome. Then anonymous functions can be used.
3. Recursion
In the process of calling a function, the function itself is used directly or indirectly.
Recursion efficiency is very low, so we need to keep the current state when we enter the next recursion. Python, unlike other languages, has no tail recursion, but Python has limited conditions and does not allow users to recurse indefinitely.
The characteristics of recursion:
1. There must be a clear termination condition.
2. The scale of the problem should be reduced compared with the previous recursion each time we enter a deeper level of recursion.
3. Recursive efficiency is not high, too many recursive layers will lead to stack overflow.
Examples:
# 1 The document reads as follows,The title is:Full name,Gender,Age,salary # # egon male 18 3000 # alex male 38 30000 # wupeiqi female 28 20000 # yuanhao female 28 10000 # # Requirement: # Remove each record from the file and put it in the list, # Every element in the list is{'name':'egon','sex':'male','age':18,'salary':3000}Form # # 2 List from 1,Get information about the highest paid people # 3 According to the list from 1 to 1,Take out the information of the youngest person # 4 List from 1,Mapping the names in each person's information into capital letters # 5 List from 1,Filter out names with a The Information of the Beginner # 6 Using recursive printing of Fibonacci sequence(The sum of the first two numbers yields the third number) # 0 1 1 2 3 4 7... with open('b.txt',encoding='utf-8')as f: l=[{'name': line.split()[0], 'sex': line.split()[1], 'age': line.split()[2], 'salary': line.split()[3]} \ for line in f] #2. print(max(l,key=lambda i:i['salary'])) #3. print(min(l,key=lambda i:i['age'])) #4. m=map(lambda x:x['name'].capitalize(),l) print(list(m)) #5. print(list(filter(lambda x:not(x['name'].startswith('a')),l))) #6. def f(n): if n==0: return 0 elif n==1: return 1 else: if n==1000: return f(1000) else: return f(n-2)+f(n-1) for i in range(150): print(f(i))