The simplest conditional statement:
If expression: expr_true_suite
As above, if is a keyword and expression is a conditional expression, conditional expressions support multiple conditional judgments, can be concatenated with Boolean operators and, or, and not, Expr_true_suite is a code block, expression is true, code block if only one row , the entire conditional statement above can be written to a single line, but with poor readability.
Conditional statements with Elif and else:
If expression1: expr1_true_suiteelif expression2: expr2_true_suiteelif expressionn: exprn_true_ Suiteelse: none_of_the_above_suite
As above, syntax is similar to conditional statements in other languages, and elif and else are optional.
Conditional expressions Implement ternary operators:
The three-dimensional operator is the following in C + + (e executes X at the time of creation, otherwise executes y)--
E? X:y
Ternary operators for Python simulations--
(E and [X] or [Y]) [0]
Implementation of the Python ternary operator--
X if E else Y
Take a look at some examples of judgments:
>>> if 1 < x < 2: print (' True ')
and represents and
or means >>> x 2 >>> if x = = 2 or x = = 3: print (x)
Returns a If B is true, otherwise returns C
A If B else c>>> ' True ' if 1 < x <2 els