|

# 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.

### 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.

### `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.

`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.

`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.

```413
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

```True
False
seven is less than seventy three```

### `==` means is equal to

`The puppy weighs less than the labrador`

### `<` means is less than

`The mouse weighs the least`

`False` means `0`
`True` means `1`

`False`

`True`

`True`

### 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.

|