What is Truthy and Falsy in python ? How is it different from True and False?
<p>I just came to know there are <strong>Truthy</strong> and <strong>Falsy</strong> values in python which are different from the normal <code>True</code> and <code>False</code>?</p>

<p>Can someone please explain in depth what <em>truthy</em> and <em>falsy</em> values are? </p>

<p>Where should I use them?</p>

<p>What is the difference between <em>truthy</em> and <code>True</code> values and <em>falsy</em> and <code>False</code> values ?</p>

<p>In Python 2.7.x there is a special method on certain objects called <code>__nonzero__</code> which returns a boolean value. When defined, it allows you to call things like you would a boolean value:</p>

<pre><code>class Foo(object):
  def __init__(self, x):
    self.x = bool(x)
  def __nonzero__(self):
    return self.x
</code></pre>

<p>If you were to type this in the command line:</p>

<pre><code>a = Foo(True)
b = Foo(False)

def test_it(foo):
  if foo:
    print('It is truthy')
</code></pre>

<p>you'd see that <code>a</code> is "truthy" while <code>b</code> is not.</p>

<p>In Python 3.x you'd use the special method <code>__bool__</code>.</p>

python