MindMap Gallery Python commonly used functions
This is a mind map about commonly used functions in Python, summarizing built-in functions, custom functions, etc. Hope it helps everyone!
Edited at 2024-03-15 17:28:32This is a mind map about bacteria, and its main contents include: overview, morphology, types, structure, reproduction, distribution, application, and expansion. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about plant asexual reproduction, and its main contents include: concept, spore reproduction, vegetative reproduction, tissue culture, and buds. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about the reproductive development of animals, and its main contents include: insects, frogs, birds, sexual reproduction, and asexual reproduction. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about bacteria, and its main contents include: overview, morphology, types, structure, reproduction, distribution, application, and expansion. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about plant asexual reproduction, and its main contents include: concept, spore reproduction, vegetative reproduction, tissue culture, and buds. The summary is comprehensive and meticulous, suitable as review materials.
This is a mind map about the reproductive development of animals, and its main contents include: insects, frogs, birds, sexual reproduction, and asexual reproduction. The summary is comprehensive and meticulous, suitable as review materials.
Python commonly used functions
built-in functions
print()
View content
Output content
parameter
Multiple contents are separated by ","
Set a print content separator sep, the default is space
sep="----"
Set the separator end between two prints, the default is "/n" (line feed)
type()
View type
dir()
View the functions and properties available to the content
len()
View length
input
Enter information
Format
input('prompt information')
Information is all characters
operator.itemgetter()
Obtain
in iterable object
an element (or elements)
object attribute value
parameter
itemgetter(n): Gets the nth element in the iterable object. ·itemgetter(n1,n2,...): Get the n1, n2, ....th element in the iterable object and return a tuple. ·itemgetter(attr): Gets the attr attribute of the element in the optional object, ·itemgetter(item): Gets the element whose element is item in the iterable object. (item can be any comparable object, including numeric values, strings, tuples, etc.)
itemgetter(n)
nth element in iterable object
itemgetter(n1,n2,...)
The n1, n2,….th element in the iterable object
Returns a tuple
itemgetter(attr)
The attr attribute of the element in the optional proxy object
itemgetter(item)
The element in the iterable object whose element is item
item
comparable objects
numerical value
string
tuple
Custom function
define function
def function name (parameter): Processing return value
constitute
Function name
number
Cannot be the beginning
letter
multiple words
"_"connect
()end
Fill in parameters
parameter
formal parameter
Write at the end ()
variable
Arguments
variable value
type
Positional parameters
Bind based on location
correct order
Same quantity
key parameter
When calling a function, specify the parameter name
Can be used with positional parameters
Key parameters must follow positional parameters
Default parameters
When function is defined
Give the parameters a default value (positional parameters and key parameters can be given default values)
Positional parameters should be placed before the default parameters
variable parameter
Variable names with an asterisk (")
*args
Store all unnamed variable parameters
No limit on the number
Output tuple
Sequence packaging
When defining a function
Add * in front of function parameters
Pack elements into tuples
Unpacking of sequences
When the function is executed
Add * in front of the actual parameters
Unpack the sequence
Reorganize into tuples
Processing
The program code corresponding to the function name
return value
rerun
Do not write
The return value is None
multiple values
tuple form
Terminate operation
The following code will not be executed
Commonly used functions
anonymous function
lambda
Functions or subroutines that do not need to define identifiers (function names)
Format
lambda parameter: return expression
Before the colon are parameters
There can be multiple
separated by commas
The one to the right of the colon is the expression
There can only be one
Main points
Multiple parameters possible
Only one expression
use
When special, non-reusable code needs to be encapsulated
Avoid cluttering your code with lots of one-line functions
usage
Assign to a variable
addition
Take the maximum value
Assign to other functions
Passed as arguments to other functions
map() function
sorted() function
filter() function
recursive function
Basically similar to the loop structure
Example
1-100 cumulative
Loop structure
for
while
recursive function
higher order function
Can accept a function and a sequence
map
Apply func to each element in the iteration object to get the return value and form a new generation object.
grammar
map(func,iterable)
Convert to list display
Example
Returns the square of each element in the list
Loop structure
map function
combined with lambda
filter
filter function
func is called once for each element
True
reserve
False
give up
grammar
filter(func.iterable)
func
Function name
judge
OutputTrue/False
iterable
iterable object
Example
Remove numbers greater than 3 from the list
Loop structure
filter function
combined with lambda
reduce
Receive two parameters and apply a function to a sequence. Reduce will continue to calculate the result with the next element of the sequence.
Merges a sequence into a single value by applying a two-argument function cumulatively to the items of a sequence from left to right.
You need to call the module before using it
from functools import reduce
grammar
reduce(func,iterable[,initiaizer])
initiaizer
initial parameters
The first parameter involved in the operation
Optional
Example
synthesize a number
reduce function
combined with lambda
sorted
Sorting function
Sort all iterable objects
Difference from .sort()
Operation object
.sort
applied to list
sorted
for all iterable objects
return value
.sort
applied to list
No return value
sorted
What is returned is a new list
grammar
sorted(iterable[,cmp=None][, key=None][, reverse=False])
iterable
iterable object
cmp
comparison function
two parameters
The values of parameters are all taken from iterable objects.
rule
more than the
1
less than
-1
equal
0
key
elements to compare
feature
only one parameter
Taken from an iterable object
Specifies an element in the iterable object
When the list elements are multi-dimensional data, a key is needed to select which bit of data to sort by.
Commonly used functions
lambda function
operator.itemgetter()
reverse
Sorting rules
reverse=True
descending order
reverse=False
Ascending order
(default)
Example
General usage
Elements are one-dimensional numbers or letters
Return a new list
string
Arrange according to the first numerical-alphabetical order
String and numeric types cannot be compared
Advanced usage
key function
Scenes
Data multi-dimensional
Sort by special form
lambda function
operator.itemgetter()
machine learning