DEV Community

Cover image for Applying Boolean Algebra in Python
Ganesh Kumar
Ganesh Kumar

Posted on

Applying Boolean Algebra in Python

Hello, I'm Ganesh Kumar, and I'm building LiveReview — a blast-radius aware AI code review built for your business-critical systems. Star us to help devs discover the project, give it a try, and share your feedback to help improve the product.
Python booleans are literals (fixed values) like integers, strings etc.

What makes the boolean data type interesting is that booleans can only be one of two values: True or False.

Let's first understand about whatt boolean is.

What Boolean Represents?

In computer scinece Boolean is Data type that is True or False.

We can say it as as 0 and 1 as well.

In python we use this for many cases where we need some conditions.

A boolean value, False, is assigned to is_valid
is_valid = False

See here if we assign "False" to is is_valid
Then it will be string.

Also the word false is treated as a variable
is_valid = false

Takeaway: True and False should be written without quotation marks and with the first letter capitalized to be a boolean.

Boolean Expressions

Any Data type plays very important role in practical use case same way Boolean data type plays good role when it is been used with boolean expressions.

is_blast_radius <= 10

Read the above code as: Is the blast radius is less than or equal to 10.

Questions like "Is the blast radius of code change is less than 10" or "How much current ?" have only two possible answers: yes or no. In programming, yes is represented as True and no as False, and such questions are expressed using boolean expressions.

In the above code, <= is a comparison operator used to compare two values. Let's look at all the comparison operators next.

Comparison Operators

Python has all these comparison operators that can be used to compare values.

Operator Expression Read as
< x < y Is x less than y?
<= x <= y Is x less than or equal to y?
> x > y Is x greater than y?
>= x >= y Is x greater than or equal to y?
== x == y Is x equal to y?
!= x != y Is x not equal to y?

Let's try out these operators.

Example: < and <=

>>> x = int(input("Enter x: "))
Enter x: 10
>>> y = int(input("Enter y: "))
Enter y: 20
>>> result = (x < y)
>>> print(f"x < y ---> {result}")
x < y ---> True
>>> result = (x <= y)
>>> print(f"x <= y ---> {result}")
x <= y ---> True
>>> result = (x < 10)
>>> print(f"x < 10 ---> {result}")
x < 10 ---> False
>>> result = (x <= 10)
>>> print(f"x <= 10 ---> {result}")
x <= 10 ---> True
Enter fullscreen mode Exit fullscreen mode

similarly we can use other oprators as well.
Example: == and !=

x = int(input("Enter x: "))
y = int(input("Enter y: "))

result = (x == y)
print(f"x == y ---> {result}")

result = (x != y)
print(f"x != y ---> {result}")

result = (x == 10)
print(f"x == 10 ---> {result}")

result = (x != 10)
print(f"x != 10 ---> {result}")
Enter fullscreen mode Exit fullscreen mode

Output

Enter x: 10
Enter y: 20
x == y ---> False
x != y ---> True
x == 10 ---> True
x != 10 ---> False


name1 = "Alice"
print(name1 == "Alice")   # True
print(name1 == "alice")   # False
print(name1 != "Alice")   # False
print(name1 != "alice")   # True
Enter fullscreen mode Exit fullscreen mode

Logical Operator

There are three logical operators: and, or & not.

And: Checks both values has True to return True or else it will be false.

Or: Any on value should be True to return True or else it will be false.

Not:Will make true to false and false to true.

How Boolean Works

0 is False
print(bool(0))

Non-zero value is True
print(bool(12))

Non-empty string is True

print(bool("Python"))
print(bool("False")) # True

Empty string is False
print(bool(""))

The "False" string is actually True in a boolean context because it's non-empty.

Conclution

Boolean gives true only based on expression we use or when we use to check wheather there is a value stored in any other datatype.

Next we will use in some exprestion mainly in controlling program flow with conditional statements.

Instead of presenting every diff with equal emphasis, LiveReview scores each change by blast radius — how far its impact reaches through your call graph — so you can focus attention where it actually matters.

Spend code review effort where business risk is highest — not spread evenly across every diff.

⭐ Star it on GitHub:

GitHub logo HexmosTech / LiveReview

Blast-Radius Aware AI Code Review for Business-Critical Systems

LiveReview

gitleaks.yml osv-scanner.yml govulncheck.yml semgrep.yml dependabot-enabled mcp-testcases.yml

LiveReview: Blast-Radius Aware AI Code Review for Business-Critical Systems

LiveReview is an AI code reviewer that scores every hunk of a diff by blast radius: how far a change reaches through your call graph, how much persistent state it touches, and how well-tested it is. A 3-line change to a shared auth check can outrank a 300-line UI tweak. Your team's attention goes to the highest-risk code first, not spread evenly across every diff.

blast-radius-demo.mp4

LiveReview's Blast Radius & Review Priority scoring, live in the diff viewer.
















The exact math, not a black box Visualize blast radius at a glance Every factor that feeds the score

How does Blast Radius scoring work? (a more technical explanation)

Here's the goal:

  • A 3-line fix in a function used by 40 other files, that also writes to a database, should score high.
  • A 300-line UI change in one file, fully covered by…




Click below to try LiveReview with your codebase:

LiveReview Banner

Top comments (0)