Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Tutorial
  3. Python Booleans and Comparison Operators

Core Python · Tutorial 8

Python Booleans and Comparison Operators

Use True and False, comparison operators, and logical operators to build conditions.

Open the compilerPractice exercises
beginner6 min readUpdated November 19, 20263 learning goals
On this page
  1. Comparison operators
  2. Logical operators
  3. Truthiness
  4. Operators on strings and lists
  5. Practice

What you will learn

  • Compare values with ==, !=, <, <=, >, and >=
  • Combine conditions with and, or, and not
  • Use truthiness correctly
Before this lesson: Python Numbers and Arithmetic Operators

A boolean has exactly two possible values: True and False. Booleans usually come from comparisons and logical expressions.

Comparison operators#

Python
print(10 == 10)   # True
print(10 != 10)   # False
print(7 < 12)     # True
print(7 >= 7)     # True
print("a" < "b")  # True

Important distinction:

  • = assigns a value.
  • == compares two values.

Strings compare lexicographically by Unicode code point, which is case-sensitive: "Z" < "a" is True.

Logical operators#

Python
age = 20
has_ticket = True

print(age >= 18 and has_ticket)   # True
print(age < 13 or has_ticket)     # True
print(not has_ticket)             # False

and is true only when both sides are true. or is true when at least one side is true. not reverses a boolean.

These operators short-circuit. In a and b, Python evaluates b only when a is truthy. In a or b, it evaluates b only when a is falsy.

Python
name = ""
display = name or "Anonymous"
print(display)  # Anonymous

This works because an empty string is falsy.

Truthiness#

Many values behave like False in a condition:

  • False, None
  • Numeric zero: 0, 0.0
  • Empty collections: "", [], (), {}, set()

Everything else is truthy by default.

Python
items = [1, 2, 3]

if items:
    print("The list is not empty")

This is more idiomatic than if len(items) > 0, though both are correct.

Chained comparisons

Python supports 0 <= score <= 100, which means both comparisons must hold. In many other languages, and is required between comparisons.

Operators on strings and lists#

Some operators have non-mathematical meanings:

Python
print("ab" * 3)        # ababab
print("a" in "cat")    # True
print([1, 2] + [3])    # [1, 2, 3]

in tests membership. It is often clearer than a loop for simple checks.

Practice#

Predict the result of each expression, then verify in the compiler:

Python
score = 84
passed = score >= 60
distinction = score >= 85

print(passed)
print(passed and distinction)
print(passed or distinction)
print(not passed)

Next, use these conditions to control which code runs with if, elif, and else.

Previous tutorialReading User Input with Python input()Next tutorialPython if, elif, and else Statements

Related tutorials

beginner

Python if, elif, and else Statements

Control program flow with conditions, nested branches, and clear boolean expressions.

8 min read
beginner

Python while Loops

Repeat code until a condition changes, avoid infinite loops, and use counters and sentinels safely.

8 min read

On this page

  1. Comparison operators
  2. Logical operators
  3. Truthiness
  4. Operators on strings and lists
  5. Practice

Run the examples

Every Python block has a Run in compiler button that opens the code in a new tab.