0

I am reading a code and there is a list and an if statement, for example:

x, y = map(int, input().split())
ret = a[x][y]
    a[x][y] = 0
    for i in range(8):
        nx = x + [-2, -2, -1, -1, 1, 1, 2, 2][i]
        ny = y + [-1, 1, -2, 2, -2, 2, -1, 1][i]
    
        if a[nx][ny]:
            ret += dfs(nx, ny)

What does the last if statement do? This is my first time seeing an if without a condition like >, <, ==.

  • It *always* works like this, `if ` will evaluate `` to the resulting object, and that object is checked for "truthiness", specifically, whether `bool(resulting_object)` returns `True` or `False`. `True` and `False` are truthy and falsey, respectiely. But empty containers are Falsey, so `if my_list:` will check `bool(my_list)` which returns `True` if the list is nonempty, and False if it is empty. – juanpa.arrivillaga Jul 27 '21 at 21:08
  • You have not actually provided a [mcve], indeed, your code above *doesn't even compile*, but presumably, it is `a[nx][ny]` is an `int` or a `float`. When dealing with numeric types, `0` is falsey, everything else is truthy. – juanpa.arrivillaga Jul 27 '21 at 21:10

0 Answers0