NameError: name 'false' is not defined in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

NameError: name 'false' is not defined in Python #

The Python "NameError: name 'false' is not defined" occurs when we misspell the keyword False. To solve the error, make sure to capitalize the first letter of the keyword - False.

nameerror name false is not defined

Here is an example of how the error occurs.

main.py
# ⛔️ NameError: name 'false' is not defined. Did you mean: 'False'? f = false print(f)

To solve the error, we have to use a capital F when spelling the False keyword.

main.py
f = False print(f) # 👉️ False

Names of variables, functions, classes and keywords are case-sensitive in Python.

The only other available boolean value is True (capital T).

main.py
f = False print(f) # 👉️ False t = True print(t) # 👉️ True
There are only 2 boolean values - True and False.

The type of the two boolean values is <class 'bool'>.

main.py
print(type(False)) # 👉️ <class 'bool'> print(type(True)) # 👉️ <class 'bool'>

Boolean objects in Python are implemented as a subclass of integers.

main.py
print(isinstance(False, int)) # 👉️ True print(isinstance(True, int)) # 👉️ True

Converting a False boolean value to an integer returns 0, whereas converting True returns 1.

main.py
print(int(False)) # 👉️ 0 print(int(True)) # 👉️ 1

You can use the not (negation) operator to invert a boolean value.

main.py
print(not False) # 👉️ True print(not True) # 👉️ False

You can use the built-in bool() function to convert any value to a boolean.

main.py
print(bool('')) # 👉️ False print(bool(0)) # 👉️ False print(bool(1)) # 👉️ True print(bool('Hello')) # 👉️ True

The bool() function takes a value and converts it to a boolean (True or False). If the provided value is falsy or omitted, then bool returns False, otherwise it returns True.

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 0 (zero) of any numeric type
  • empty sequences and collections: "" (empty string), () (empty tuple), [] (empty list), {} (empty dictionary), set() (empty set), range(0) (empty range).

The bool() function returns True if passed any non-falsy value.

Conclusion #

The Python "NameError: name 'false' is not defined" occurs when we misspell the keyword False. To solve the error, make sure to capitalize the first letter of the keyword - False.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.