1, conditional statement
2. Circular statements
3. Iterator
4. List resolution
5, Generator expression
1, conditional statement
The simplest conditional statement:
If expression:
Expr_true_suite
As above, if is the keyword, expression is a conditional expression that supports multiple conditional judgments, can be concatenated with Boolean operators and, OR and not, Expr_true_suite is a code block, and expression is true when the code block is only one line , the entire conditional statement above can be written to a single line, but with poor readability.
Conditional statement with Elif and else:
If expression1:
Expr1_true_suite
Elif expression2:
Expr2_true_suite
Elif Expressionn:
Exprn_true_suite
Else
None_of_the_above_suite
As above, syntax is similar to conditional statements in other languages, and elif and else are optional.
The conditional expression implements the ternary operator:
In C + +, the ternary operator is as follows (E is set to execute X, otherwise execute y)--
E? X:y
The ternary operator of Python simulation--
(E and [X] or [Y]) [0]
The implementation of the Python ternary operator--
X if E else Y
2. Circular statements
While loop:
While expression:
Suite_to_repeat
For loop:
For Iter_var in iterable:
Suite_to_repeat
The For loop accesses all elements of the Iterable object, and Iter_var is set to the current element of the object that can be iterated. A For loop can be used for a sequence type, including a string, list, or tuple, or for an iterator type, an iterator object has a next () method that returns the next entry after a call, and after all entries are iterated, the iterator throws a stopiteration exception telling the program that the loop is finished, The For statement internally calls next () and catches the exception.
Break, continue, pass:
The break statement ends the current loop and jumps to the next statement. The continue statement terminates the current loop and attempts the next iteration. The pass statement indicates that nothing is done.
else in the loop:
There can also be else in the loop, which differs from C + + in that the ELSE clause executes only after the loop completes normally, meaning that the break statement skips the else block.
3. Iterator
An iterator provides an interface to a class sequence object, and, more subtly, an object that is not a sequence but shows a sequence behavior, such as a dictionary key, a file's row, and so on. Invoking iter () on an object can get its iterator, and a class that implements the __iter__ () and Next () methods can also be used as iterators. Itertools is a module associated with iterators.
4. List resolution
Let's take a look at the syntax for list parsing:
[Expr for Iter_var in iterable if COND_EXPR]
The kernel of this statement