1

Apologies for overexplaining here.

I was trying to see if a string has any digits or not. So I wrote simple code for this, which is:

any(char.isdigit() for char in s)

Which results in True if any character in the string is a digit and False if None of them are.

Now let's say I wanted False instead of True as the output. That means I want negation of whatever the result is. So I finally found out that all I need is to put not in front of the statement and that solves the problem I am having.

But before I found this, I ended up with some other unexpected results. Such as:

With ~ operator:

  • ~(any(char.isdigit() for char in s)) results to -1, bool expected
  • So I tried ~True to see if that normally works or not, results to -2, False(bool) expected
  • Then I tried ~False, results to -1, True(bool) expected

Not just that, I remembered that ! in front of bool value works as well, but here's what I got:

!(any(char.isdigit() for char in s)) results to error - "for was unexpected at this time."

This type of error I am seeing for the first time ever in Python.

Does anyone have any idea what these -1 and -2 values actually mean? It might be my ignorance of Python's basic concepts. If it's already answered in StackOverflow, can someone point it out to me? I'll delete the question in such a case.

Amit Amola
  • 2,301
  • 2
  • 22
  • 37

0 Answers0