Sen
Sen

Reputation: 53

Boolean equal to -1

A rookie question probably..I am reading some code and wondering about line 28:

if len(files) == 0 or not files[0].endswith(".dcm") or root.find("sax") == -1:
    continue

So why does the boolean operation on the left hand side equal -1 (instead of 0)?

Upvotes: 0

Views: 53

Answers (2)

dianyi
dianyi

Reputation: 105

according to https://docs.python.org/2/library/stdtypes.html?highlight=str%20find#str.find

str.find(sub[, start[, end]]) Return the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

Upvotes: 1

Will Hardy
Will Hardy

Reputation: 14836

str.find() returns -1 if the provided text is not found, root.find("sax") == -1 is checking for that.

The line might be a little more readable if it were:

if not files or files[0].endswith('dcm') or 'sax' not in root:

Upvotes: 4

Related Questions