Python: True/False (boolean)

By Xah Lee. Date: . Last updated: .

True and False are literal values of boolean type.

print( type(True))              # <class 'bool'>
print( type(False))             # <class 'bool'>

What Value Evaluates to True?

In a if statement,

Following evaluates to False:

bool function

You can use the function bool to test.

# all the following are True

print( bool(3))
print( bool("abc"))

# all the following are False

print( bool(None))
print( bool([]))
print( bool(0))
print( bool(""))

Comparison Operators

Boolean Operators

x and y
and
x or y
or
not x
negation

Equality Test

x == y
equal
x != y
not equal

Number Comparison

x < y
less than
x <= y
less equal
x > y
greater
x >= y
greater equal

Object Identy Test

x is y
same object
[see Python 3: Object, ID, Type]
x is not y
not same object

Python

Overview

Help

String

Arithmetic

Boolean

Conditional

Loop, Iteration

Data Structure

Function and Class

Module

Misc

Regex

Text Processing

Web

Misc