Working with Boolean Operators in Python

The Python Boolean data type of is one of the built-in data types that takes two values: True or False. Unlike most other programming languages, in Python, boolean values are capitalized (for example, "True"). If the value is written in lower case, e.g., "true", the interpreter will throw an error: NameError: name 'true' is not defined. A boolean value is returned by comparison operations, logical operations, and their combinations. You can use Python's bool() function to evaluate any value and check if it is True or False. Boolean expressions are essentially conditional statements that may or may not be True. Click Execute to run Python Boolean Example online and see the result.
Working with Boolean Operators in Python Execute
print(bool("Hello"))
print(bool(False))
print(bool(None))
print(bool(5==10))
Updated:

What is Python?

Python is a widespread general-purpose programming language that is commonly used for server-side programming, machine learning, application testing, and as "glue code" for connecting various components. Python is an interpreted and object-oriented language with dynamic typing, dynamic binding, and built-in garbage collection. Python works on most operating systems, including Windows, Linux, and macOS. Python is easy to maintain, and Python code is easy to learn. Python has built-in modules for handling JSON, strings, HTTP requests, and XML right out of the box.

What is a boolean in Python?

The Boolean concept is present in every programming language for building algorithms and controlling the behavior of programs. Boolean data type (bool or boolean type) is a primitive data type that takes two values - True or False. True and False are instances of the class bool, which is, in turn, a subclass of int, so True and False behave like the numbers 1 and 0 in Python. The only difference is how they are displayed.

Python Boolean Example
print(bool("Python"))

# output: True

What are the Boolean operators in Python?

Python has three Boolean operators: "and", "or", and "not". They are written as words rather than symbols, unlike the "&&", "||" and "!" used in JavaScript and many other languages. The "and" and "or" operators combine two expressions, while "not" inverts a single one.

Python Boolean Operators Syntax
a and b
a or b
not a

Where:
  • and: True only when both operands are true
  • or: True when at least one operand is true
  • not: inverts the truth value of the operand

The following is an example of using the Boolean operators in Python:

Python and, or, not Example
print(True and False)
print(True or False)
print(not True)

# output:
# False
# True
# False

How to combine comparisons with Boolean operators in Python?

Comparison operators such as "==", "!=", ">" and "<" return a Boolean value, so you can join them with "and" and "or" to build a single condition. This is the most common use of Boolean operators in Python.

Python Combining Comparisons Example
age = 20

print(age > 18)
print(age == 20 and age < 65)

# output:
# True
# True

What do "and" and "or" return in Python?

Unlike in most languages, the Python "and" and "or" operators do not return True or False - they return one of the operands. Python evaluates the operands from left to right and stops as soon as the result is known, which is called short-circuit evaluation. The "or" operator returns the first operand that is true, and "and" returns the first one that is false. This makes "or" a convenient way to supply a fallback value.

Python Short-Circuit Evaluation Example
print("Python" and "Requests")
print("" or "default")
print(0 or [])

# output:
# Requests
# default
# []

Which values are true and which are false in Python?

Boolean operators accept any object, not only True and False. Empty values - an empty string, zero, an empty list or dictionary, and None - are false; everything else is true. You can check any value with the bool() function.

Python Truth Value Example
print(bool(""), bool("Python"))
print(bool(0), bool(42))
print(bool([]), bool([1]))
print(bool({}), bool(None))

# output:
# False True
# False True
# False True
# False False

See also