Learned the comparison operator

2021年6月5日

Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)

Recently, I have been learning Python.

The following is a brief description of the environment in which Python runs.

  • Windows 10 64bit
  • python 3.9.4
  • PyCharm Community Edition 2021.1 x64

More details can be found in the following article.

In this article on programming in Python, we tried the basic usage of the comparison operator. This article is a reminder of what we learned.

Sponsored Links

Boolean value

The result of the comparison will be the boolean value True or False. These two are keywords. They are used to represent truth values.

True and False are values of type bool. In the following sample program, the type of the variable that stores True and False is output by the type() function.

Source
a = True
b = False

print(type(a))
print(type(b))
Execution results
<class 'bool’>
<class 'bool’>

As shown above, the output shows that it is of type bool.

Compare the size of values.

First, we tried the operator for comparing the size of values.

Suppose that the variables a and b contain integers. To compare the size of the values of variables a and b, we can use the following expression.

Expression Results
a > b True if a is greater than b. False otherwise.
a >= b True if a is greater than or equal to b. False otherwise.
a < b True if a is less than b. False otherwise.
a <= b True if a is less than or equal to b. False otherwise.

The following is a sample program that I created and ran.

Source
a = 1
b = 2

print(f'{a=}, {b=}’)
print(a > b)
print(a >= b)
print(a < b)
print(a <= b)

a = 3
b = 3

print(f'{a=}, {b=}’)
print(a > b)
print(a >= b)
print(a < b)
print(a <= b)
Execution results
a=1, b=2
False
False
True
True
a=3, b=3
False
True
False
True

If either of the variables a or b is a string, an error will occur. For example, if you execute a > b, you will get the following error.

  • TypeError: '>’ not supported between instances of 'str’ and 'int’

However, if both variables a and b are strings, no error will occur. They are compared in lexicographic order.

The following is a sample program for string-to-string comparison.

Source
a = 'ABC’
b = 'ABD’

print(f'{a=}, {b=}’)
print(a > b)

a = 'ABC’
b = 'AB’

print(f'{a=}, {b=}’)
print(a > b)
Execution results
a=’ABC’, b=’ABD’
False
a=’ABC’, b=’AB’
True

The above is a comparison of alphabetic strings. The results are compared in alphabetical order.

Compare if the values are equal

Suppose that variables a and b contain integers. To compare whether the values of variables a and b are equal or not, write the following

Expression Results
a == b True if the values of a and b are equal; otherwise, false.
a != b True if the values of a and b are not equal, false otherwise.

The following is a sample program that I created and ran.

Source
a = 1
b = 2

print(f'{a=}, {b=}’)
print(a == b)
print(a != b)

a = 3
b = 3

print(f'{a=}, {b=}’)
print(a == b)
print(a != b)
Execution results
a=1, b=2
False
True
a=3, b=3
True
False

Note that no error was generated even if a string was stored in either a or b in the above. For example, if a=3, b=’3′, the result of a == b was false.

Belongs or is a substring

There is an operator to check whether a list or set type contains an element or not. To check whether the value of variable a is contained in the list stored in the list type variable b, write the following

Expression Results
a in b True if a is an element, false otherwise.
a not in b True if a is not an element, false otherwise.

It can also be used on strings. We tried using this operator on a string earlier.

The following checks whether the string stored in variable a is a part of the string stored in variable b.

Source
a = 'is’
b = 'This is a pen.’

print(f'{a=}’)
print(f'{b=}’)
print(a in b)
print(a not in b)
Execution results
a=’is’
b=’This is a pen.’
True
False

It can be used to determine whether a string is included or not, as shown above.

Next, I tried the list type.

Source
a = 'AB’
b = ['A’, 'AB’, 'ABC’]

print(f'{a=}’)
print(f'{b=}’)
print(a in b)
print(a not in b)
Execution results
a=’AB’
b=['A’, 'AB’, 'ABC’]
True
False

We store the list in the variable b above. It can be defined by using square brackets and separating the items with commas.

The string stored in the variable a is contained in the list b, so a in b becomes True.

Whether it is the same object or not

You can determine if variable a and variable b are the same object as follows.

Expression Results
a is b True if a and b are the same object, false otherwise.
a is not b True if a and b are not the same object, false otherwise.

The following is a test using the list type.

Source
a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(f'{a=}, {b=}, {c=}’)
print(a is b)
print(a is c)
Execution results
a=[1, 2, 3], b=[1, 2, 3], c=[1, 2, 3]
True
False

Since we are assigning the value of variable a to variable b, variables a and b contain the same object. Therefore, a is b will be True.

Variable c is a different object because it is a separately defined list, although the contents are the same. Therefore, a is c will be false.

That’s all. I hope this is helpful to you.

Sponsored Links