“This is the fifth day of my participation in the Gwen Challenge.
In selection and loop constructs, the Python interpreter determines the next step in the execution process based on the value of the conditional expression. A selection structure determines whether to execute a particular code based on different conditions, and a loop structure determines whether to execute a particular code repeatedly based on different conditions.
In Python, all legal expressions can be used as conditional expressions. If the value of a conditional expression is True, the condition is True; if the value is False, the condition is not True. A conditional expression whose value is not False, 0 (or 0.0, 0j, etc.), None, an empty list, an empty tuple, an empty set, an empty dictionary, an empty string, an empty range object, or another empty iterator is considered equivalent to True by the Python interpreter (note that equivalence and equality are different).
For example, numbers can be used as conditional expressions, but only 0, 0.0, 0j are equivalent to False, and any other number is equivalent to True. Lists, tuples, dictionaries, collections, strings, and other container objects such as range, Map, ZIP, Filter, Enumerate, and reversed can also be used as conditional expressions. Container objects that do not contain any elements are equivalent to False. Container class objects containing any element are equivalent to True. In the case of strings, only empty strings containing no characters are equivalent to False; A string containing any character, even a single space, is equivalent to True.
Single branch selection structure
The syntax for a single branch selection structure is as follows.
If conditional expression statement block
The colon “:” after the expression is indispensable, indicating the beginning of a statement block, and the statement block must be indented accordingly, usually by 4 Spaces.
When the condition expression value is True or some other value equivalent to True, the condition is met and the block is executed, otherwise the block is not executed and subsequent code execution (if any) continues.
Generates a name that contains two or three Chinese characters
From random import choice,random name =choice() if random()>0.5: Name += choice(' choice ') print(name)Copy the code
Loop structure
Python has two forms of loop structure: for and while.
The for loop is a great way to iterate over elements in container-like objects (lists, tuples, dictionaries, collections, strings, maps, zip, and the like) in the following syntax.
For loop iterates through in container class object: loop body [else: else clause block]
The else clause in square brackets can be absent or absent, depending on the problem to be solved. If the for loop has an else clause, we execute as follows: when the loop ends naturally because it has traversed all the elements in the container, we continue to execute the else block; if the loop ends prematurely because a break statement was executed, we do not execute the else block.
The while loop
While conditional expression loop body [else: else clause block]