Sideway from Sideway
Draft for Information Only


Python Booleans
 Unary Boolean Value
 Boolean Operations
 Logical Comparison Operations
  Logical Comparison Operators
 Source and Reference

Python Booleans

Python booleans are used to represent the Boolean result of operations and functions. Python booleans are implemented as a subtype of integers. A Boolean result always return 0 or False for Boolean false, and 1 or True for Boolean true unless otherwise stated. For example, or and and operations always return one of their operands accordingly.

Unary Boolean Value

Any Python object can be used as a Boolean value in an if or while condition or as an operand of the Boolean operations. By default, an object is considered as a True value unless the return value of an object is considered as 0 or False. For example, its class defines either a __bool__() method that returns a False value, or a __len__() method that returns zero. The typical built-objects that considered as False are:
  • Python constants that defined as False: None and Fasle
  • Zero of any numeric type, for example: 0, 0.0, 0𝑗, 0+0𝑗, Decimal(0), Fraction(0,1)
  • Empty sequences and collections, e.g.: '', (), [], {}, set(), range(0)

Boolean Operations

The Python Boolean operators for Boolean operations between two objects are not, and, and or. Precedence of logical operators in descending order of their priorities are not, and, and or. However, not operator has a lower priority than non-Boolean operators. OperationPriorityDescriptionRemarks not 𝑥Highif x is false, then True, else Falsenot has a lower priority than non-Boolean operators 𝑥 and 𝑦Mediumif 𝑥 is false, then 𝑥 else 𝑦A 'short-circuit' type operator. Only if the first operand is true, the second operand will then be evaluated. 𝑥 or 𝑦Lowif 𝑥 is false, then 𝑦, else 𝑥.A 'short-circuit' type operator. Only if the first operand is false, the second operand will then be evaluate.

Logical Comparison Operations

The Python logical comparison operator for boolean comparisons are <, <=, >, >=, ==,!=, is, and is not. Besides, the in and not in operators are used to compare object types that are iterable or implement the __contains__() method. Unlike Boolean operations, all logical comparison operations have the same priority. And all logical comparison operations have higher priority than all Boolean operations. Precedence of logical operators in descending order of their priorities are not, and, and or. However, not operator has a lower priority than non-Boolean operators. Comparisons can be chained arbitrarily. The concept used to evaluate a chained comparison is similar to that used in evaluating a mathematical expression. For example, 𝑥<𝑦<𝑧 is equivalent to 𝑥<𝑦 and 𝑦<𝑧, except that 𝑦 is evaluated only one time in the chained logical comparison operation.

Logical Comparison Operators

Types of Python comparison operators are OperatorDescription <strictly less than <=less than or equal >strictly greater than >=greater than or equal ==equal !-not equal isobject identity is notnegated object identity inin container not innot in container In general, objects of different types can never be compared except different numeric types. Operators <, <=, >, and >= are defined only when the operation makes sense. The == and !- operators are always defined. For some object types, the == operator is equivalent to is operator, e.g. class objects. However, non-identical instances of a class is normally considered as non-equal unless the class defines the __eq__() method. The behavior of the is and is not operators cannot be customized. Both operators can be applied to any two objects and never raise an exception.
Instances of a class cannot be ordered with respect to other instances of the same class, or other types of object, unless the class defines enough of the methods __lt__(), __le__(), __gt__(), and __ge__() (in general, __lt__() and __eq__() are sufficient, if you want the conventional meanings of the comparison operators).

Source and Reference


ID: 210100007 Last Updated: 1/7/2021 Revision: 0

Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna

Home 5






Hobbies 8


Chinese 1097

English 339

Reference 79


Hardware 224


Application 213

Digitization 32

Latex 52

Manim 204

KB 1

Numeric 19


Web 289

Unicode 504


CSS 65

SVG 46


OS 429

DeskTop 7

Python 72



Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 33

Coordinate Geometry 2

Calculus 67

Complex Analysis 21


Tables 8


Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5


Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1

Copyright © 2000-2022 Sideway . All rights reserved Disclaimers last modified on 06 September 2019