liveBook · Manning

Boolean

As explained in Python How-To

A boolean value is a truth value representing one of two logical states, true or false, in Python.

A boolean represents one of two logical states: True or False. Python produces booleans when conditions, string checks, and other predicates are evaluated. section 2.2.1 section 5.3.5

Use boolean results to make decisions, keep matching items, or report whether a condition was satisfied. Methods such as isalnum(), isalpha(), and isnumeric() return True or False. section 2.2.1 section 5.3.5

Checking for overlap

Convert lists to sets, find their intersection, and convert the result to a boolean when only the yes-or-no answer matters:\n\n```python\ngood_stocks_set & set(client0)\n# {'AMZN', 'GOOG'}\n\nbool(good_stocks_set & set(client0))\n# True\n```\n\nThe empty intersection would produce False because an empty collection evaluates as False. section 3.6.2

Keep these boolean behaviors distinct:

  • Use `filter` when True keeps an item and False excludes it. section 5.3.5
  • Remember that `or` and `and` use short-circuit evaluation and return one of their operands, rather than necessarily returning a boolean. section 3.6.5
  • Treat `isnumeric()` carefully: floats, negative integers, and empty strings produce False. section 2.2.1
Table 9.1 JSON data types (view table figure)

Data type

Data content

String

String literals enclosed in double quotes

Number

Number literals, including integers and decimals

Boolean

Boolean values, true or false (all lowercase)

Array

A list of supported data types wrapped in square brackets

Object

Key-value pairs surrounded by curly braces

Null

A special value (null) representing an empty value for any valid data type

Compare Python booleans with JSON booleans: JSON writes the values as lowercase `true` and `false`. section 9.3.1

How Python How-To covers it

How deep it goes
Defines True and False and demonstrates predicates, truthiness, filtering, and short-circuit evaluation without descending into implementation internals.
Examples
Demonstrates username validation, numeric-string checks, stock-list overlap, urgent-task filtering, and JSON Boolean values.
What it assumes
Assumes basic Python strings, lists, sets, functions, and iteration while teaching truth evaluation through practical examples.
What you can do after
Apply it via predicates, `bool`, set operations, and `filter` to validate data, detect matches, and select items.
How it connects
Connects booleans with predicates, truthiness, set intersection, filtering, short-circuit evaluation, and JSON data types.

Also explained in

Compare how 11 books explain boolean →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.