Boolean logic is a foundational concept in programming and computing, dealing with truth values, typically represented as True or False . In Python, boolean logic is used in conditions and loops, and it's vital for decision-making processes.

Boolean Values in Python

In Python, the two Boolean values are True and False. Note that they are capitalized, which is important as Python is case-sensitive.

Basic Boolean Operators

Python uses three basic Boolean operators:

  1. AND (and) : Returns True if both operands are true.

  2. OR (or) : Returns True if at least one of the operands is true.

  3. NOT (not) : Returns True if the operand is false (negation).

Truth Tables

Truth tables show the output of Boolean operations for all possible input combinations. Let's see the truth tables for AND , OR , and NOT .

AND (and) Truth Table

A

B

A AND B

True

True

True

True

False

False

False

True

False

False

False

False

OR (or) Truth Table

A

B

A OR B

True

True

True

True

False

True

False

True

True

False

False

False

NOT (not) Truth Table

A

NOT A

True

False

False

True

Comparison Operators

Comparison operators in Python are used to compare two values. They are fundamental in making decisions in your code by allowing you to evaluate conditions and execute certain blocks of code based on those evaluations. Here are the primary comparison operators:

  1. Greater Than (>) : Evaluates if the left-hand operand is greater than the right-hand operand.

  2. Less Than (<) : Evaluates if the left-hand operand is less than the right-hand operand.

  3. Equal To (==) : Checks if the values of two operands are equal.

  4. Not Equal To (!=) : Checks if the values of two operands are not equal.

  5. Greater Than or Equal To (>=) : Evaluates if the left-hand operand is greater than or equal to the right-hand operand.

  6. Less Than or Equal To (<=) : Evaluates if the left-hand operand is less than or equal to the right-hand operand.

These operators are widely used in controlling the flow of programs, including in if statements, loops, and other control structures. By comparing values, they help in making decisions and performing different actions based on the outcome of these comparisons.