Logical And Comparison Operators In Python

Logical And Comparison Operators In Python

Logical operators in Python are used for testing conditional statements that are either true or false. It is how we can make decisions about what to do next in the code. Logical operators in Python are and, or and not. The first two look at two operands and make a conclusion. For example, with the and operator, you have a True value if both the operands are true. With the or operator, you have a True value if either of the operands is True. Lastly, there is the not operator, which pretty much just reverses the current value. It returns True if the operand is False. Comparison operators compare the values on either side of the operand and determine how they relate to each other. The comparison operators include things like ==(is equal), !=(is not equal) , >(is greater than, <=(is less than or equal to), etc.

What is a Boolean?

A Boolean is a binary variable that can be either True or False. They seem really simple but they are quite important because you’re going to be using Booleans a lot in programming. In this example, we are going to be using the weather, so we’re going to see if it’s snowing or not, if it’s freezing or not, and based on those values, we’re going to take actions. Based on the code below it is not snowing, but it is freezing. Boolean values give us the ability to use logical operators on them.

isSnowing = False
isFreezing = True

Logical Operators are Special Operators for Booleans

A logical operator takes one or more Boolean values and operates on them. These are And, Or, and Not, and they’re good to know because they can help you write terse but understandable code. The logical operators are shown in this table. We can use them to make decisions about how to take action.

And Operator

Combination Meaning
true and true true
false and true false
true and false false
false and false false

In this code below, we check to see if it is snowing and freezing. If both of those conditions are True, then we want to move south. In our case, only one is True so we stay put.

if isSnowing and isFreezing:
    print('Time to move south')

or Operator

The logical or operator can be used with two operands and produces the following values.

Combination Meaning
true or true true
false or true true
true or false true
false or false false

The code snippet here makes use of the or operator within an if check. We check to see if it is snowing, or if it is freezing. Only one of these has to be true in order to be True. Since it is freezing, the sentence ‘It must be winter time’ is printed out.

if isSnowing or isFreezing:
    print('It must be winter time')
It must be winter time

not Operator

The last logical operator to be aware of is not, and this will return the reverse of what you give it. Again it works on a Boolean value.

Not Operator Meaning
true false
false true

The code here puts the not operator to use in an if statement. We know that isSnowing is a Boolean false. By apply the not operator to it, it reverses to True. Therefore, the sentence ‘The snow is falling’ is printed.

if not isSnowing:
    print('The snow is falling')
The snow is falling

This example here combines some of these concepts together. We start by creating a variable called scores, and inside of scores we some numbers that represent various scores. We also use the statistics module to find the average of all the scores. The for loop iterates through all of these scores, and we want to see if that score is greater than average or less than average. Then using the NOT operator we either print a message that the score is above average or that the score is below average.

scores = [112, 189, 329, 817, 772, 259]
averagescore = statistics.mean(scores)
for score in scores:
    isAverage = score < averagescore
    if not isAverage:
        print('The score of ' + str(score) + ' is above average')
        print('The score of ' + str(score) + ' is below average')
The score of 112 is below average
The score of 189 is below average
The score of 329 is below average
The score of 817 is above average
The score of 772 is above average
The score of 259 is below average

Python Comparison Operators

We’ll now take a look at some comparison operators. A comparison operator, also called a relational operator, determines the equality or difference between values. The whole expression ultimately returns a Boolean value. The comparison operators are things like greater than, equal to, or less than.

Operator Meaning
== is equal to
<= less than or equal to
>= greather than or equal to
< less than
> greater than

The following code snippets should be pretty self-explanatory.

< means is less than

print(7 < 73)
print(73 < 7)

if 7 < 73:
    print('seven is less than seventy three')
seven is less than seventy three

== means is equal to

puppy = 7
labrador = 35

if puppy < labrador:
    print('The puppy weighs less than the labrador')
The puppy weighs less than the labrador

< means is less than

mouse = 1
if mouse < puppy and mouse < labrador:
    print('The mouse weighs the least')
The mouse weighs the least

False means 0
True means 1

print(False > True)
# A - Z means 1 - 26
print('Words' > 'Word')
# A - Z means 1 - 26
print('a' <= 'b')

Logical And Comparison Operators In Python Summary

A logical operator is used with one or more Boolean values to determine a final True or False value. These are the AND, OR, and NOT operators. These operators need to be in lowercase in your Python code. Comparison operators are very helpful if you need to know if one value is less than, equal to, or greater than another value in a piece of code. These include the ==, !=, <>, >, <, >=, and <= operators.