An Introduction to Programming

Python for All

Chapter Eight — Comparison & Boolean Operators

Thanasis Troboukis  ·  All Chapters

Chapter Eight

Comparison & Boolean Operators

Simple rules for asking questions in Python and combining True/False conditions.

Comparison Operators

Comparison operators compare two values and return True or False.

== equal, != not equal, > greater than, < less than, >= greater or equal, <= less or equal.

Python · Try it

      
Important: = means assignment, while == means comparison.

Boolean Operators

Boolean operators combine conditions.

and: both conditions must be True. or: at least one condition must be True. not: flips True to False (and False to True).

Python · Try it

      

Combining Conditions in Real Examples

In real programs, we usually combine comparison operators with boolean operators inside if statements.

Python · Try it

      
Python · Try it

      
Simple way to read conditions: Evaluate each small comparison first, then combine them with and, or, or not.

Identity and Membership Operators

is and is not check identity (whether two names refer to the same object). in and not in check membership (whether a value exists inside a sequence or set).

Python · Try it

      

Chapter Navigation

Move between chapters.

Loading Python environment — this may take a moment…