Boolean Operators: Comparing Values in Python

Boolean Operators in python

Python is widely used for both scripting and automation on Linux, as well as building web and standalone applications.

Python is designed to be easy to learn, easy to write, and easy to read. It’s a great multi-purpose programming language.

Boolean Values

This article covers how boolean operators work in Python. In programming, a boolean value is either TRUE or FALSE.

When comparing values in Python, a boolean value is returned allowing you to store the result of the comparison, or take a certain action depending on the result.

Comparison Operators for Comparing Values

When comparing values, the following comparison operators can be used:

Operator Comparison
== Equal to
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to

Logic Operators for Building Conditions

You can use logic operators to check whether multiple comparison conditions are met, or whether only some are, or whether none are:

Operator Meaning
and True if both comparisons are true
or True if either comparison is true
not True if comparison is false

Examples Using if

Here are some examples of how to use the above comparison and logical operators with a simple if statement. If the comparison passed to an if statement returns TRUE, the associated code will execute, otherwise, it will not:

value1 = 10
value2 = 5
value3 = 15

if value1 == value2:
    print("value1 is equal to value2") # This message will not be shown as the comparison will return false (10 is not equal to 5)

if value1 > value2:
    print("value1 is greater than value2") # This message will be shown as the comparison is true (10 is greater than 5)

if value1 > value2 and value2 > value3:
    print("value1 is greater than value2 and value2 is greater than value3") # Will not be shown as both comparisons are not TRUE (10 is greater than 5, but 5 is not greater than 15)

if value1 > value2 or value2 > value3:
    print("value1 is greater than value2 or value2 is greater than value3") # Will be shown as one of the comparisons is TRUE, even though the other is FALSE

if not value1 == value2:
    print("value1 is not greater than value2") # Shown because 10 does not equal 5

Note that:

  • Variables are assigned with the = statement
  • White space is important in Python! Make sure your code is properly indented or it won’t work
  • To compare two values using comparison operators, place the value you wish to compare before the operator, and the value you want compared after it
  • Use the andor, and not operators to chain together comparisons for more complex logic
  • Comments are used: Text following the # character will be ignored by Python and can be used to leave yourself notes

Conclusion

For more real-world examples of how boolean logic can be used to build useful software functions, check out our other Python and programming articles.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *