https://docs.python.org/3/tutorial/controlflow.html
The 'if' statement is used for conditional execution. It is a compound statement.
# Compound statement structure.
header_statement: # 'if' or 'while' or 'for'
statements # indentation 4 spaces (recommended)
# Nested suites in compound statements.
suite1 # no indentation
header_statement1:
suite2 # indentation 4 spaces
header_statement2:
suite3 # total indentation 8 spaces
suite2 (continued) # indentation 4 spaces
suite1 (continued) # no indentation
# Syntax. Zero or one part of the 'if' statement will be executed.
if condition1:
statements
elif condition2: # optional, more 'elif' parts is possible
statements
else: # optional, only one 'else' part is possible
statements
# Short form in a single line (better to avoid). if condition: simple_statement
assert isinstance(n, int)
if n < 0:
print("negative number")
elif n % 2 == 0:
print("{} {}".format(n, "is even"))
else:
print("{} {}".format(n, "is odd"))
if condition:
A = X
else:
A = Y
# Conditional expression (sometimes called a “ternary operator”) [PEP 308].
A = X if condition else Y
A = ((X) if (condition) else (Y)) # for more complex expressions
# In C/C++:
# A = ((condition) ? (X) : (Y))
a, b = 25, 30 pos_a = ((-a) if a < 0 else a) # abs(a) is better max_ab = (b if a < b else a) # max(a, b) is better min_ab = (a if a < b else b) # min(a, b) is better