liveBook · Manning

Boolean operation

As explained in Get Programming: Learn to code with Python

A logical operator is an operator that performs logical combination or negation of Boolean or truthy and falsy operands.

Boolean operators combine true/false expressions to help a program make decisions. In Python, `and` is true when both expressions are true; `or` is true when at least one is true; and `not` reverses a truth value. section 14.1 section 13.1

Deciding what to do

For pasta dinner, combine “I have pasta” and “I have sauce”: both must be true. For a cold day, having a jacket or a sweater gives two alternatives. In Python, combined conditions can go in one `if` statement. section 14.1

Table 14.1. Truth values for combinations of two statements with “and” and “or”

Statement 1 (example: “It is raining”)

Word to combine the statements (<and>, <or>, <not>)

Statement 2 (example: “I am hungry”)

Result (example: “It is raining <_> I am hungry”)

True <and> True True
True <and> False False
False <and> True False
False <and> False False
True <or> True True
True <or> False True
False <or> True True
False <or> False False
N/A <not> True False
N/A <not> False True

Compare the truth-table results for `and`, `or`, and `not`. section 14.1

How Get Programming: Learn to code with Python covers it

How deep it goes
Explains operator truth values with a table and shows combined conditions in an `if` statement.
Examples
Uses pasta-and-sauce decisions, jacket-or-sweater choices, and negative-number conditions.
What it assumes
Assumes familiarity with true/false expressions and introduces their connection to yes/no questions.
What you can do after
Apply Boolean operators to combine conditions in Python `if` statements.
How it connects
Links Boolean operators to Boolean expressions, conditionals, and `if` statements.

Also explained in

Compare how 4 books explain boolean operation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.