0

Recently I read several lines of Python codes and got a bit confused.

p = 'a*b.*'
lp = len(p)
m = [0]*(lp+1)
for i in range(1, lp + 1):
    m[i] = m[i-1] and p[i-1] == '*' or (i > 1 and m[i-2] and p[i-1] == '*')
print(m)

The result printed out as [0, False, 0, False, 0, False]

p is a string, and lp is the length of string p. I created a list m [0, 0, 0, 0, 0, 0]. Then I am getting confused: I loop all the elements in this array(list). What does "and" and "or" mean here in the loop. Double equal signs are also used here. I thought "and" and "or" can only be used under if-else clauses. What does this mean here? Could anyone please translate for me? Thanks!

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Troy Bear
  • 55
  • 5
  • 2
    The meaning of `and` and `or` does not change if it's used in a loop. – mkrieger1 Jul 03 '21 at 21:07
  • `m` should *probably* be initialized with `[False] * (lp+1)` to make the final result clearer. – chepner Jul 03 '21 at 21:08
  • sorry, what does this mean? I am still confused why the result printed out like [0, False, 0, False, 0, False] – Troy Bear Jul 03 '21 at 21:09
  • 1
    You should probably forget this code for a moment and learn from a simpler example. This code is very unclear. – mkrieger1 Jul 03 '21 at 21:11
  • 1
    It means that the person who wrote that line of code thought they were really, really clever. – Silvio Mayolo Jul 03 '21 at 21:13
  • " I thought "and" and "or" can only be used under if-else clauses" that is definitely not true, `and` and `or` are boolean operators, they can be used anywhere an expression can be used – juanpa.arrivillaga Jul 03 '21 at 21:32

1 Answers1

1

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.

enzo
  • 9,861
  • 3
  • 15
  • 38