Comparison and Logical Operators in Python

Comparison and Logical operators in Python are used to compare the value between variables, and also between expressions. When a condition is evaluated, it always results in a value of data type boolean, in other words, true or false.

Comparing Values in Python

When programming, you often want to compare two variables. For example, there may be comparison operations that determine whether a number is larger or smaller than another number. When you compare two variables you always get the value True, or False, that is, you get back if the operation is true or false.

 

Let’s take an easy example how we can compare to variables in Python

 

a = 10
b = 5
print(a>b)

In this case the print becomes

 

True

since it is true that the variable a is larger than the variable b.

 

Furthermore, in the same way when write

 

a = 10
b = 5
print(b>a)

We get the print

 

False

since it is a false statement that b is greater than a.

 

Usually, you perform comparison operations in the if statement  (more on that later) where you want something to be performed depending on whether an expression is true or false. In this way, we can create conditional programming so that certain operations are performed only if certain requirements are met (True).

 

When you compare two variables you always get the value True, or False 

Comparison and logical operators in Python

 

Namn Syntax Beskrivning
AND  a && b True if a and b are true
OR a || b True if  a or b are true
NOT !a True if a is false
Equal to a == b True if a and b are equal
Not equal to a != b True if a are false and b are false
Greater than a > b True if a are greater than b
Less than a < b True if a are less than b
Less than or equal to a <= b True if a is smaller or as large as b
Greater than or equal to a >= b True if a is larger or as large as b

EXAMPLES OF LOGICAL COMPARISONS IN PYTHON

The AND-operator

 

The example below will be True if a and b have the same value.

 

a = 3
b = 4
print(a == b)

The result is

 

False

since a and b do not have the same value.

 

Another example,

 

a = True
b = 1
print(a == b)

Result in

 

True

Because 1 is the same as True in Python.

 

And in the same way,

 

a = False
b = 0
print(a == b)

Also results in

 

True

since 0 is the same as False in Python.

Note that: False == False is True because we are comparing two variables with the same value.

Comparing strings in Python

Pay attention when comparing text strings. For example,

 

a = "True"
b = True
print(a == b)

will result in

 

False

because the text string “True”, is not the same as the value True.

Note that a is of the data type string and b is of the boolean type.

The OR-operator in Python

We get the result True if a or b are true

 

a = True
b = False
print(a | b)

will result in

 

True

since one of the values a or b has the value True.

The NOT-operator in Python

We get the result True if a is false

 

a = False
print(a)

will result in

 

True

since the value a is not True