&& (logical AND)

k = 0
for y in range(5, 95, 5):
    k += 1    
    if y > 45 and k % 2 == 0:
        # Set color to red for every other line
        # for all y > 45.
        stroke(color(255, 0, 0))
        stroke(0)    # Set color to black
    line(30, y, 80, y)

Compares two expressions and evaluates to True only if both evaluate to True. Returns False if one or both evaluate to False. The following list shows all possible combinations:

True and False   # Evaluates False because the second is False
False and True   # Evaluates False because the first is False
True and True    # Evaluates True because both are True
False and False  # Evaluates False because both are False

The "and" operator is short-circuiting; it will not bother to evaluate its second expression of the first is False. That means, in the following example, that DoSomething is never called:

def DoSomething():
    print "I did something!"
    return True

x = False
if x and DoSomething():
    print "This cannot happen."
expression1 and expression2
expression1any valid expression
expression2any valid expression
Related or (logical OR)
not (logical NOT)
Updated on Tue Jul 11 06:52:24 2017.
Creative Commons License