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.
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
- booleanconfigures · A Boolean is a logical type or value representing truth or falsity, conventionally with two states: true and false.
- truthinessused by · Truthiness is a programming language’s rule for interpreting values as true or false in a Boolean context.
- Noneused by · None is Python’s singleton object representing the absence of a value or meaningful result.
- quantitative dataused by · Quantitative data is information represented numerically as quantities, measurements, counts, or other values suitable for numerical interpretation or analysis.
- file objectcontrasts with · A file object is a program-accessible representation of an open file or stream, including its access state and input/output operations.
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.

