Condition test:
An If condition test expression
Different types of comparisons: numbers: Comparing sizes
String: Per-character comparison by ASCII code table
Lists and tuples: Comparing the contents of each part from left to right
Dictionaries: Comparing sorted (key, value) lists
True and False in Python:
1, non-0 numbers and non-empty objects are true
2, number 0, empty object and none is False
3. Comparisons and equivalence tests are applied recursively to data structures
4, the return value is TRUE or False
Combination condition test:
X and y, x or y, not X
If syntax structure
If Boolean_expression1:
Suite1//Indent 4 characters
A = X If Y else Z:
If Y:
A = X
Else
A = Z
expression1 If boolean_expression else expression2
While and for loops
While loops are used to write common iteration structures
A For loop is a generic sequence iterator
Python Stealth Iteration Tool:
In member relationship testing; list parsing; map, reduce, filter functions
While Boolean_expression:
While_suite
Else
Else_suite
Else optional, boolean_expression end loop When the result is false, execute else.
Print x, which can be displayed on a single line in a non-wrapped line
Break: Jump out of the inner loop
Continue: advance to the next cycle and move to the beginning of the nearest layer loop.
ELSE code block: The loop terminates normally and else will not execute if the loop termination is caused by a break.
Range generates a sequence directly in memory before the for loop starts. Xrange is a data element that is generated at a time, one out, and can save memory resources.
The For loop executes faster than while.
Zip: Returns a list of tuples of parallel elements, often traversing several sequences in a for loop.
L1 = [1,2,3,4,5,6]
L2 = [' A ', ' B ', ' C ', ' d ', ' e ', ' F ']
Zip (L1,L2)
[(1, ' a '), (2, ' B '), (3, ' C '), (4, ' d '), (5, ' E '), (6, ' F ')]
Zip is used to construct the dictionary dynamically:
D = {}
for (k,v) in Zip (L1,L2):d [k] = V
D
{1: ' A ', 2: ' B ', 3: ' C ', 4: ' d ', 5: ' E ', 6: ' F '}
Python Process Control