Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Logic operators – and, or, not, if-else


Python offers us four logical operators: and, or, not, and if-else. These work with Boolean values to create Boolean results. They're entirely distinct from the bit-wise operators of &, |, ^, and ~, that we looked at in Chapter 2, Simple Data Types.

The and, or, and not operators are common in all programming languages. They fit the widely-used definitions from Boolean algebra.

The if-else Boolean expression has three operands. In the middle, it uses a Boolean condition, but the other two operands can be objects of any types. Here's an example:

selection = "yankee" if wind < 15 else "stays'l"

The if-else operator has a Boolean condition in the middle. In this example, it's the comparison, wind < 15. If the condition is True, then the left-most expression is the result, the string "yankee". If the condition is False, then the right-most expression is the result; here, it's "stays'l".

The logical operators implicitly apply the bool() function to...