A sequence of
The basics from Runoob watched most of it, checked the rest, and did some quizzes
www.runoob.com/quiz/python…
The rest can be followed by greedy NLP content.
Lambda expression
lambda_expr ::= "lambda" [parameter_list] ":" expression lambda_expr_nocond ::= "lambda" [parameter_list] ":" Expression_nocond lambda expressions (sometimes called lambda configurations) are used to create anonymous functions. The expression lambda Parameters: expression produces a function object. The unnamed object behaves like a function defined in the following way: def <lambda>(parameters): return expressionCopy the code
Let’s look at an example: the sum of two numbers
Note: if the logic is complex, it is better to go def, because lambdas are written on one line, which is too complex to read easily.
Three conditional expression
conditional_expression ::= or_test
[“if” or_test
“else” expression
]
Conditional expressions (sometimes called “ternary operators”) have the lowest precedence of all Python operations.
The expression x if C else y is evaluated on condition C first, not on x. If C is true, x is evaluated and returns its value; Otherwise y is evaluated and its value is returned.
Four map () function
Map () maps the specified sequence based on the provided function.
The first argument function calls the function function with each element in the argument sequence, returning a new list of values returned by each function function.
Map () function syntax:
map(function, iterable, ...)
Copy the code
Example:
Examples of multiple arguments:
Five filter () function
The filter() function is used to filter a sequence, filter out elements that do not meet the criteria, and return an iterator object that can be converted to a list using list().
This takes two arguments, the first a function and the second a sequence. Each element of the sequence is passed as an argument to the function for evaluation, and then returns True or False. Finally, the element that returns True is placed in the new list. ‘
Here is the syntax for the filter() method:
filter(function, iterable)
Copy the code
6 the reduce () function
The reduce() function accumulates the elements in the argument sequence.
Function performs the following operations on all the data in a data set (linked list, tuple, etc.) : operates on the first and second elements of the set with function (which has two parameters) passed to Reduce, and then calculates the result with the third data using function function, finally obtaining a result.
Python3.x reduce() has been moved to the functools module. If we want to use it, we need to introduce the functools module to call reduce() :
from functools import reduce
Copy the code
Reduce () function syntax:
reduce(function, iterable[, initializer])
Copy the code
- Initializer — Optional, initial parameter
Returns the result of the function evaluation.
The way you think about it is 1 plus 2 is 3, 3 plus 3 is 6,6 plus 4 is 10,10 plus 5 is 15. It’s just two plus two
So 10 is the initial value, 10 plus 15 is 25
Table derivation
List comprehensions are a Python shortcut to building lists.
Python’s range() function can be used to create a list of integers, typically used in a for loop.
Syntax :range(start, stop[, step])
Start: The count starts from start. The default value is 0 (closed range). For example, range(5) is equivalent to range(0,5).
Stop: Count to stop, excluding stop(open interval). For example, range(0,5) is [0, 1, 2, 3, 4], excluding 5.
Step: The difference between two adjacent values. The default value is 1. For example, range(0,5) corresponds to range(0,5, 1).
The for loop also works. In the first example above, the second is.
List derivation of complex points:
Set derivation
A set derivation replaces the list derivation with {}, and a dictionary derivation looks like deriving two values and constructing key-value pairs
Someone I like to hear:
set_1 = {value for value in 'Some are wanderers, some study hard, some flatter, some are numb.'}
print(set_1)
Copy the code
Output:
{' people 'and' hard ', 'cold', 'wood', 'mark', 'window', 'read', 'ma', 'lake', 'jiang', 'waves',' a ', 'Yu', 'early', 'in', 'on', ', 'and' has', 'o'}Copy the code
The collection is unordered and non-repetitive, so duplicate elements are automatically removed.
Dictionary derivation
Same as above with if conditional statements, nested loops, etc. I’m not going to expand it here.
So that’s the basic use of Python3.