liveBook · Manning

Boolean context

A boolean context is a program context in which a value is interpreted as true or false according to the language’s truth-value rules.

How 2 Manning books explain it

Python Workout: 50 ten-minute exercises

A Python evaluation context in which an object's truth value determines conditional or logical behavior.

How it is used
Uses truth testing to recognize empty user input and control a rainfall-collection loop.
How deep it goes
Defines empty strings as false and demonstrates `not` in a loop, without expanding beyond string truth testing.
Examples
Demonstrates an empty city name terminating a rainfall loop and a nonempty name updating a dictionary.
What you can do after
Apply Boolean context to detect empty input and choose whether a loop continues or exits.

Read how Python Workout: 50 ten-minute exercises explains boolean context →

Python Workout, Second Edition

A Boolean context is a Python evaluation context in which an object's truth value determines control flow or a conditional result.

How it is used
Uses object truth values to guard indexing, validate input, and control loops.
How deep it goes
Explains empty tuples and strings through executable examples, without developing a broader theory of truth-value testing.
Examples
Demonstrates `if not items` for variadic arguments and `if not city_name` in a rainfall-entry loop.
What you can do after
Apply Python truth testing to write concise guards for empty collections and input strings.

Read how Python Workout, Second Edition explains boolean context →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.