Sideway
output.to from Sideway
Draft for Information Only

Content

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


©sideway

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

IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 337

Reference 67

Computer

Hardware 151

Software

Application 202

Digitization 25

Latex 10

Manim 159

Numeric 19

Programming

Web 285

Unicode 495

HTML 65

CSS 59

ASP.NET 194

OS 391

DeskTop 7

Python 46

Knowledge

Mathematics

Formulas 8

Algebra 29

Number Theory 206

Trigonometry 18

Geometry 18

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Electric 27

Biology 1

Geography 1


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