3 Python comparison operator

Posted by cafegirl on Tue, 22 Feb 2022 13:55:08 +0100

5.3 comparison operators

5.3.1 id function view variable flag

[experience code]

# Three integer variables are defined by unpacking assignment method
a,b,c = 10,10,20.5

# View the values of three variables
print(a)
print(b)
print(c)

# View the data types of three variables
print(type(a))
print(type(b))
print(type(c))

# View the IDs of the three variables
print(id(a))
print(id(b))
print(id(c))

[terminal output]

10
10
20.5
<class 'int'>
<class 'int'>
<class 'float'>
2097425508944
2097425508944
2097511330928

A variable consists of three parts: 1 is the value, 2 is the data type, and 3 is the flag (also known as the memory address).
The value and data type of variables have been mentioned before and will not be repeated.
The id here is the memory address of the variable.
When we create a new variable, the computer will allocate a memory space to the variable. Even if the variable is empty, it will also allocate memory space, that is, its id.
To view the variable id, the syntax is id (variable name).
Observing the output results, we find that the values, data types and IDs of variables a and b are the same.

# Create 3 new lists
list_1 = [1,2,3,4]
list_2 = [1,2,3,4]
list_3 = [1,4]

# View the IDs of 2 lists
print(id(list_1))
print(id(list_2))
print(id(list_3))

[terminal output]

2097506767552
2097507620608
2097510504320

Looking at the output, we found that although the list_1 and list_ The elements of 2 are the same, but the IDs of the two lists are different.
Note: the id of the variable is not fixed.
The id of the same variable is different after each run. Even if the variable value and type you define are the same as me, the output id may be different.

5.3.2 concept of comparison operator

The comparison operator is used to compare the size of two variables or expressions.
The result is Boolean data, that is, true or false.
true or false is called boolean type, which will be explained in detail in the next section.

5.3.3 > < >= <=

[experience code]

# Define 2 variables with unpacking assignment method
a, b = 10,20

#Compare the size of ab
print('a greater than b Do you',a>b)
print('a less than b Do you',a<b)
print('a Greater than or equal to b Do you',a>=b)
print('a Less than or equal to b Do you',a<=b)

[terminal output]

a greater than b Do you False
a less than b Do you True
a Greater than or equal to b Do you False
a Less than or equal to b Do you True

Observe the output results:
We define two variables, a = 10 and B = 20;
Code a > b means to judge whether a is greater than B. if it is greater than B, the value is true, and if it is not greater than B, the value is false;
10 is not greater than 20, a > b is not true, so the value of print (a > b) is false.
If 10 is less than 20, a < B holds, so the value of print (a < b) is true.

5.3.4 == , != Compare values for equality

5.3.4 are the is and is not comparison marks equal

[experience code]

# Define 2 variables with unpacking assignment method
a, b = 10,10

# Compare whether the values of two variables are equal
print(a == b)

# View the flags (IDS) of the two variables
print(id(a))
print(id(b))

# Compare whether the flags of two variables are equal
print(a is b)

[terminal output]

True
2097425508944
2097425508944
True

The value of both variables is 10, so the result of print(a == b) is true.
The IDs of both variables are 2097425508944, so the result of print(a is b) is true.
Both results are true, indicating that the values and flags of variable a and variable b are equal.

# Create 2 new lists
list_1 = [1,2,3,4]
list_2 = [1,2,3,4]

# View the IDs of 2 lists
print(id(list_1))
print(id(list_2))

# Compares whether the elements of two lists are equal
print(list_1 == list_2)
# Compare whether the flags of two lists are equal
print(list_1 is list_2)

[terminal output]

2097510559360
2097506768896
True
False

The elements of the two lists are the same, so the result of print(list_1 == list_2) is true.
The IDs of the two lists are different, so the result of print(list_1 is list_2) is false.

# Create 2 new lists
list_1 = [1,2,3,4]
list_2 = [1,2,3,4]

# View the IDs of 2 lists
print(id(list_1))
print(id(list_2))

# Compares whether the elements of two lists are equal
print(list_1 != list_2)

# Compare whether the flags of two lists are equal
print(list_1 is not list_2)

[terminal output]

2097506647040
2097506751360
False
True

The elements of the two lists are the same, so the result of print (list_1! = list_2) is false.
The IDs of the two lists are different, so the result of print(list_1 is not list_2) is true.

be careful
A = is called the assignment operator, which is used to assign values to variables.
Two = = are called comparison operators, which are used to judge whether the values of variables are equal.

5.3.6 summary


Topics: Python Back-end