Course contentsShow
← Computer Science
Lesson 172 of 2,8724. Introduction to ProgrammingFree lesson

Logical Operators: AND, OR, NOT

Combining multiple boolean expressions with and, or, and not to create compound conditions.

Logical Operators: AND, OR, NOT

What You'll Learn

You'll learn how to combine multiple boolean expressions using and, or, and not to create more powerful compound conditions.

The Core Idea

Sometimes a single comparison isn't enough. What if you need to check two conditions at once—like whether a user is both logged in AND an admin? Or whether it's a weekend (Saturday OR Sunday)? That's where logical operators come in.

The Three Logical Operators

and — Both conditions must be true

  • age >= 18 and hasLicense == true — Only true if BOTH are true
  • Think: "You need a ticket AND an ID to enter"

or — At least one condition must be true

  • day == "Saturday" or day == "Sunday" — True if EITHER is true
  • Think: "Pay with cash OR card"

not — Reverses a boolean value

  • not isRaining — True if isRaining is false
  • Think: "NOT allowed" means forbidden

Combining Them

You can chain these together:

if age >= 13 and age <= 19 and not hasParentalConsent:
    # teenager without consent

This checks three things at once: age is at least 13, age is at most 19, and they don't have parental consent.

Truth in Action

  • true and true → true
  • true and false → false
  • true or false → true
  • false or false → false
  • not true → false

When to Use

Use logical operators whenever your decision depends on multiple facts being true or false together, rather than just one condition.

Key Takeaway: Logical operators (and, or, not) let you combine simple boolean expressions into compound conditions that make more complex decisions possible.