🐍 Lesson 1: Boolean Logic in Python

Lesson: Introduction to Boolean Logic in Python

Boolean logic is the backbone of all programming decisions. It allows programs to make choices and perform actions based on conditions that evaluate to either True or False.


1. Introduction

Boolean logic in Python is based on truth valuesTrue and False.

A Boolean is its own data type in Python:

x = True
y = False

print(type(x))  # <class 'bool'>
print(type(y))  # <class 'bool'>

Booleans are often used with if statements, loops, and logical operations to control how a program behaves.


2. Relational Operators

Relational operators compare two values and return a Boolean (True or False). They are fundamental to decision-making in code.

Operator Meaning Example Output
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 10 > 2 True
< Less than 2 < 5 True
>= Greater than or equal to 5 >= 5 True
<= Less than or equal to 3 <= 4 True

3. Examples Using Relational Operators

a = 10
b = 20

print(a == b)   # False, because 10 is not equal to 20
print(a < b)    # True, because 10 is less than 20
print(a != b)   # True, because 10 and 20 are not equal
print(a >= 10)  # True, because 10 is equal to 10
print(b <= 15)  # False, because 20 is not less than or equal to 15

Relational operators are commonly used in conditions, like this:

temperature = 32

if temperature < 40:
    print("It’s a cold day.")
else:
    print("It’s a warm day.")

4. The NOT Function

The not operator reverses a Boolean value.

Expression Result
not True False
not False True

Examples:

is_raining = True
print(not is_raining)  # False

logged_in = False
print(not logged_in)   # True

Usage in conditions:

logged_in = False

if not logged_in:
    print("Please log in first.")
else:
    print("Welcome back!")

5. The AND Function

The and operator returns True only if both conditions are True.

A B A and B
True True True
True False False
False True False
False False False

Examples:

age = 18
has_license = True

if age >= 16 and has_license:
    print("You can drive!")
else:
    print("You cannot drive.")
print(True and True)   # True
print(True and False)  # False
print(False and True)  # False

6. The OR Function

The or operator returns True if at least one condition is True.

A B A or B
True True True
True False True
False True True
False False False

Examples:

is_weekend = False
is_holiday = True

if is_weekend or is_holiday:
    print("You can relax today!")
else:
    print("You have school.")
print(True or False)   # True
print(False or False)  # False

7. Conclusion

Booleans (True / False) represent truth values.

Relational operators (==, !=, <, >, <=, >=) compare values.

Logical operators (not, and, or) combine or modify Boolean results.

Key Idea:
Boolean logic allows your Python programs to make smart decisions — like whether to log in a user, open a file, or trigger an event. It’s the foundation for all control flow in programming.

Now Let’s Explore Boolean Logic in JavaScript


1. Introduction

Boolean logic forms the foundation of decision-making in programming.
In JavaScript, Boolean values represent whether something is true or false.

A Boolean can only be one of two values:

These values control how a program reacts to different situations — for example, determining if a user is logged in, if a score meets a certain threshold, or whether an action should occur.

Example:

let isOnline = true;
let hasPermission = false;

console.log(isOnline);         // true
console.log(typeof isOnline);  // "boolean"

Booleans are used in conditions to make programs dynamic and

2. Relational Operators

Relational (comparison) operators compare two values and return a Boolean (true or false).

Operator Meaning Example Result
== Equal to (loose) 5 == “5” true
=== Strictly equal to (type + value) 5 === “5” false
!= Not equal to (loose) 5 != “5” false
!== Strictly not equal to 5 !== “5” true
> Greater than 10 > 2 true
< Less than 2 < 5 true
>= Greater than or equal to 7 >= 7 true
<= Less than or equal to 3 <= 4 true

🧠 Key Tip:
Always prefer === and !== for accuracy, since == and != perform type conversion, which can lead to unexpected results.

Example:

console.log(0 == false);   // true (loose)
console.log(0 === false);  // false (strict)

3. Examples Using Relational Operators

Let’s see how these operators work in real-life examples.

Example 1: Checking login age requirement

let age = 18;

if (age >= 16) {
  console.log("You can drive!");
} else {
  console.log("You cannot drive yet.");
}

Example 2: Password check

let username = "admin";
let password = "1234";

let inputUser = "admin";
let inputPass = "1234";

if (inputUser === username && inputPass === password) {
  console.log("Login successful!");
} else {
  console.log("Login failed.");
}

Example 3: Comparing numbers

let score = 85;

if (score >= 90) {
  console.log("A grade");
} else if (score >= 80) {
  console.log("B grade");
} else {
  console.log("Needs improvement");
}

4. NOT Function (! Operator)

The NOT operator (!) reverses a Boolean value.

A !A
true false
false true

Example:

let isRaining = false;

console.log(!isRaining);  // true

You can also use ! to check for falsy values (values that behave like false).

Falsy values in JavaScript include:
0, "" (empty string), null, undefined, NaN, false

Example:

let name = "";

if (!name) {
  console.log("Name is required.");
}

🧠 Tip: The ! operator is powerful when combined with conditions that check for missing or invalid data.


5. AND (&&) and OR (||) Functions

AND (&&)

The AND operator returns true only if both conditions are true.

A B A && B
true true true
true false false
false true false
false false false

Example:

let age = 18;
let hasLicense = true;

if (age >= 16 && hasLicense) {
  console.log("You can drive!");
} else {
  console.log("You cannot drive yet.");
}

Short-Circuit Behavior (AND):
If the first condition is false, JavaScript doesn’t even check the second one.

Example:

function check() {
  console.log("Check ran!");
  return true;
}

console.log(false && check());  // "Check ran!" is not printed

OR (||)

The OR operator returns true if at least one condition is true.

A B A || B
true true true
true false true
false true true
false false false

Example:

let isWeekend = true;
let isHoliday = false;

if (isWeekend || isHoliday) {
  console.log("You can relax today!");
} else {
  console.log("Time for work.");
}

Short-Circuit Behavior (OR):
If the first condition is true, the second is not checked.

Example:

function greet() {
  console.log("Hello!");
  return true;
}

console.log(true || greet());   // "Hello!" is not printed

6. Conclusion

Boolean logic is the core of decision-making in JavaScript.

Summary:

Real-Life Example:

let loggedIn = true;
let isAdmin = false;

if (loggedIn && (isAdmin || true)) {
  console.log("Access granted!");
} else {
  console.log("Access denied.");
}

🔑 Key Takeaway:
Understanding Boolean logic allows you to control program flow, create smarter conditions, and write cleaner, more


Collegeboard Uses Slightly Different Symbols

These are the relational operators that are used on the AP CSP exam. Notice how they’re all simple math symbols that we’ve all seen before! Just make sure you know how they connect to Booleans and what role they play.