Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Truthy and Falsy? How is it different from True and False?

People also ask

What is truthy and Falsy?

Truthy values are values that evaluate to True in a boolean context. Falsy values are values that evaluate to False in a boolean context. Falsy values include empty sequences (lists, tuples, strings, dictionaries, sets), zero in every numeric type, None , and False .

What is the difference between truthy and true?

in a boolean context (if statement, &&, ||, etc.). So someone designing a language has to decide what values count as "true" and what count as "false." A non-boolean value that counts as true is called "truthy," and a non-boolean value that counts as false is called "falsey."

What is truthy and Falsy in JS?

In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false , 0 , -0 , 0n , "" , null , undefined , and NaN . JavaScript uses type coercion in Boolean contexts.

What are truthy and Falsy values in Python?

Python boolean data type has two values: True and False . Use the bool() function to test if a value is True or False . The falsy values evaluate to False while the truthy values evaluate to True . Falsy values are the number zero, an empty string, False, None, an empty list, an empty tuple, and an empty dictionary.


All values are considered "truthy" except for the following, which are "falsy":

  • None
  • False
  • 0
  • 0.0
  • 0j
  • decimal.Decimal(0)
  • fraction.Fraction(0, 1)
  • [] - an empty list
  • {} - an empty dict
  • () - an empty tuple
  • '' - an empty str
  • b'' - an empty bytes
  • set() - an empty set
  • an empty range, like range(0)
  • objects for which
    • obj.__bool__() returns False
    • obj.__len__() returns 0

A "truthy" value will satisfy the check performed by if or while statements. We use "truthy" and "falsy" to differentiate from the bool values True and False.

Truth Value Testing


As the comments described, it just refers to values which are evaluated to True or False.

For instance, to see if a list is not empty, instead of checking like this:

if len(my_list) != 0:
   print("Not empty!")

You can simply do this:

if my_list:
   print("Not empty!")

This is because some values, such as empty lists, are considered False when evaluated for a boolean value. Non-empty lists are True.

Similarly for the integer 0, the empty string "", and so on, for False, and non-zero integers, non-empty strings, and so on, for True.

The idea of terms like "truthy" and "falsy" simply refer to those values which are considered True in cases like those described above, and those which are considered False.

For example, an empty list ([]) is considered "falsy", and a non-empty list (for example, [1]) is considered "truthy".

See also this section of the documentation.


Python determines the truthiness by applying bool() to the type, which returns True or False which is used in an expression like if or while.

Here is an example for a custom class Vector2dand it's instance returning False when the magnitude (lenght of a vector) is 0, otherwise True.

import math
class Vector2d(object):
    def __init__(self, x, y):
        self.x = float(x)
        self.y = float(y)

    def __abs__(self):
        return math.hypot(self.x, self.y)

    def __bool__(self):
        return bool(abs(self))

a = Vector2d(0,0)
print(bool(a))        #False
b = Vector2d(10,0)    
print(bool(b))        #True

Note: If we wouldn't have defined __bool__ it would always return True, as instances of a user-defined class are considered truthy by default.

Example from the book: "Fluent in Python, clear, concise and effective programming"


Truthy values refer to the objects used in a boolean context and not so much the boolean value that returns true or false.Take these as an example:

>>> bool([])
False
>>> bool([1])
True
>>> bool('')
False
>>> bool('hello')
True