7. Control Flow: If, flags, ... - JulTob/Python GitHub Wiki

Control Flow

If

if <TestExpression>:
    <Block>
if x < 10 : 
  y = x
if x < 10 : y = x
if 0: y = False
if 1: y = True
if 21: y = True

If-else

if <TestExpression>:
    <BlockIf>
else:
    <BlockElse>

Or

 <Expression> or <Expression>

If the first statement is true the second one is not evaluated. Evaluates left to right.

Conditional statements

“If” executes the content code when the control expression evaluates to a True boolean value.

dead_parrot = True
if dead_parrot :
  print("This parrot is dead")

if dead_parrot :
  print("This parrot is dead")
else:
  print("No, he's sleeping")

Expected = False 
Surprise = True
if Expected:
  print("Can you start again?")
elif Surprise:
  print("Nobody expected the Spanish Inquisition!!!")
else:
  print("Bring the Comfy Cushions")

Control Optimization

def is_friend(String):
  result = False
  if String[0]=='D':
    result=True
    return result
  if String[0]=='N':
    result=True
    return result
  return result

## Can be combined as
def is_friend(String):
  result = False
  if String[0]=='D' or String[0]=='N':
    result=True
    return result
  return result

Flags

# Different ways to test multiple
# flags at once in Python
x, y, z = 0, 1, 0

if x == 1 or y == 1 or z == 1:
    print('passed')

if 1 in (x, y, z):
    print('passed')

# These only test for truthiness:
if x or y or z:
    print('passed')

if any((x, y, z)):
    print('passed')
# Different ways to test multiple
# flags at once in Python
x, y, z = 0, 1, 0

if x == 1 or y == 1 or z == 1:
    print('passed')

if 1 in (x, y, z):
    print('passed')

# These only test for truthiness:
if x or y or z:
    print('passed')

if any((x, y, z)):
    print('passed')
# Python's list comprehensions are awesome.

vals = [expression 
        for value in collection 
        if condition]

# This is equivalent to:

vals = []
for value in collection:
    if condition:
        vals.append(expression)

# Example:

>>> even_squares = [x * x for x in range(10) if not x % 2]
>>> even_squares
[0, 4, 16, 36, 64]
# Because Python has first-class functions they can
# be used to emulate switch/case statements

def dispatch_if(operator, x, y):
    if operator == 'add':
        return x + y
    elif operator == 'sub':
        return x - y
    elif operator == 'mul':
        return x * y
    elif operator == 'div':
        return x / y
    else:
        return None
def dispatch_dict(operator, x, y):
    return {
        'add': lambda: x + y,
        'sub': lambda: x - y,
        'mul': lambda: x * y,
        'div': lambda: x / y,
    }.get(operator, lambda: None)()


>>> dispatch_if('mul', 2, 8)
16

>>> dispatch_dict('mul', 2, 8)
16

>>> dispatch_if('unknown', 2, 8)
None

>>> dispatch_dict('unknown', 2, 8)
None
# Functions are first-class citizens in Python.

# They can be passed as arguments to other functions,
# returned as values from other functions, and
# assigned to variables and stored in data structures.

>>> def myfunc(a, b):
...    return a + b
...
>>> funcs = [myfunc]
>>> funcs[0]
<function myfunc at 0x107012230>
>>> funcs[0](2, 3)
5
⚠️ **GitHub.com Fallback** ⚠️