What does 'i<i or 1>' mean in python?

Question:

enter image description here

I was taking a look at the code and found <> behind the variable. At first I thought it’s the same as [] for indexing, but still cannot figure out the exact purpose of using it.

Anyone would let me know what it is or the keyword for googling it?

Asked By: Junyeong Ahn

||

Answers:

i<0 is an expression that test if the variable i is strictly less than 0 which in this case means i is negative. Can’t read the 2nd thing but i >= means greater or equal to something.

Answered By: Allan Wind

The line of code in question is obscured by the blue pen, but I assume it is supposed to be this:

if i<0 or i>=len(mat) or j<0 or j>= len(mat[0]):
    continue

From your description, it seems as though you think i<0 or i> is one expression, but this is incorrect. In actual fact, the expressions are i<0, i>=len(mat), j<0 and j>= len(mat[0]). To make the precedence of the operators clearer, I have added parentheses and spaces below:

if (i < 0) or (i >= len(mat)) or (j < 0) or (j >= len(mat[0])):
    continue

The mat variable is a two-dimensional matrix, indexed by i and j. The code above checks both i and j to see if they are within the bounds of the matrix. In other words, it makes sure that they are not less then zero, and not greater than the highest vertical or horizontal index.

Answered By: Jack Taylor
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.