and
and or
are actually boolean operators and they can be used anywhere to compare two values.
According to the docs,
The expression x and y
first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
The expression x or y
first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
Note also that
In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true.
So, in your case, the values being compared are
m[i-1] type int, evaluates to False if it's 0 else True
p[i-1] == '*' type bool, evaluates to False if p[i-1] is not equal to '*' else True
i > 1 type bool, evalueates to False if i is less or equal to 1 else True
m[i-2] type int, evaluates to False if it's 0 else True
So why the snippet outputs [0, False, 0, False, 0, False]
? If you debug this program, you'll notice that in some iterations, the value at the left of the first and
is returned. In these iterations, the result will be a integer (0) since m[i-1]
is a integer. In other iterations, the value at the right of the first and
is returned. In these, the result will be a boolean.