-1
>>> dict = {}
>>> a=2
>>> if a is not None and dict is None:
...     print("lol")
... else:
...     print(" Print result is a")
... 
   result is a

Why does the first if statement does not run? I am specifying that thedictis empty and that" a"` exists.

Reference: https://docs.python.org/2/library/stdtypes.html#truth-value-testing

Yazzz
  • 11
  • 6

2 Answers2

1

I am specifying that the dict is empty and that "a" exists.

No, you're not. You're testing whether a is not identical to None (true) and if {} is identical to None (false).

What you want is a and not d. You almost never want to compare with a boolean, and you certainly never want to do it with is.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

Because the dictionary isn't None.

There's a difference between a value being falsey, and it satisfying is None.

Just test the truthiness directly:

if a is not None and not dict:

As noted in the comments though, don't name variables the same as existing function names. That's just asking for 'dict' object is not callable errors in the future.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117