Python Booleans

Python · Variables & Data Types

Theory

A boolean is one of two values, True or False, of type bool. Comparisons produce them (5 > 3 is True), and if, while and filtering logic consume them. bool is actually a subclass of int, so True behaves like 1 and False like 0: True + True is 2, and sum() of a list of booleans counts the True values, a handy trick in data work.

Every Python object also has a truth value, which is what if checks. Falsy values are False, None, zero of any numeric type (0, 0.0), and empty containers ('', [], (), {}, set()). Everything else is truthy, including negative numbers and non-empty strings. A classic gotcha: bool('False') is True, because it is a non-empty string; converting the text 'False' needs an explicit comparison like value == 'True'.

Why we use it

Booleans are how programs make decisions. Every if, while, filter and validation rule depends on a True/False answer, and knowing truthiness lets you write short checks like if names: instead of if len(names) > 0:.

When to use it

Use booleans for flags (is_active) and results of comparisons. Rely on truthiness for 'is it empty?' checks, but compare explicitly (if x is None, if count == 0) when 0 and None mean different things in your data.

How it works

  1. Python needs a truth value, e.g. if value:
  2. Is value None or False? Yes: Treat as False. No: Check further.
  3. Is it a zero number or an empty container? Yes: Treat as False. No: Treat as True.
  4. Run the matching branch
  5. Comparisons like age >= 18 already return True or False

Syntax

x = True
y = 5 > 3

Example

is_adult = age >= 18

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic