Less than - Returns whether x is less than y. All comparison operators return 1 for true and 0 for false. This is equivalent to the special variables True and False, respectively. Note that the capital names of these variables are capitalized.

(a < b) returns True.

>=

Greater than or equal to - Returns whether x is greater than or equal to y.

(a >= b) returns False.

<=

Less than or equal to - Returns whether x is less than or equal to y.

(a <= b) returns True.

The following example demonstrates the operation of all Python comparison operators:

Instance(Python 3.0+)

#!/usr/bin/python3a = 21b = 10c = 0if(a == b):
print("1 - a equal b")else:
print("1 - a not equal to b")if(a != b):
print("2 - a not equal to b")else:
print("2 - a equal b")if(a < b):
print("3 - a Less than b")else:
print("3 - a greater or equal to b")if(a > b):
print("4 - a more than the b")else:
print("4 - a Less than or equal to b")# Modify the values of variables a and ba = 5;
b = 20;
if(a <= b):
print("5 - a Less than or equal to b")else:
print("5 - a more than the b")if(b >= a):
print("6 - b greater or equal to a")else:
print("6 - b Less than a")

The output of the above example:

1- a not equal to b
2- a not equal to b
3- a greater or equal to b
4- a more than the b
5- a Less than or equal to b
6- b greater or equal to a

a = 21b = 10c = 0

Python Assignment Operator

The following hypothesis variable a is 10 and variable b is 20:

Operator

Description

Instance

=

Simple assignment operator

c = a + b Assign the result of a + b to c

+=

Additional Assignment Operator

c += a is equivalent to c = c + a

-=

Subtraction assignment operator

c -= a is equivalent to c = c - a

*=

Multiplication assignment operator

c *= a is equivalent to c = c * a

/=

Division assignment operator

c /= a is equivalent to c = c / a

%=

Modification Assignment Operator

c %= a Equivalent to c = c % a

**=

Power assignment operator

c **= a is equivalent to c = c ** a

//=

Rounding the assignment operator

c //= a is equivalent to c = c // a

The following example demonstrates the operation of all Python assignment operators:

Bitwise AND Operator: Two values participating in the operation. If both corresponding bits are 1, the result of this bit is 1, otherwise 0

< Td> (a & b) output 12, binary interpretation: 0000 1100

|

Bitwise OR Operator: As long as one of the corresponding two binary digits is 1, the result bit is 1.

(a | b) Output 61, binary interpretation: 0011 1101

^

bitwise XOR operator: When the two corresponding binary digits are different, the result is 1

(a ^ b) Output 49, binary interpretation: 0011 0001

~

Bitwise negation operator: Inverts each binary bit of data, that is, changes 1 to 0 and 0 to 1. ~x is similar to -x-1

(~a ) output -61 , binary interpretation : 1100 0011, in the complement form of a signed binary number.

<<

Left movement operator: Each binary of the operand is shifted left by several digits, and the number of digits moved by the number on the right side of "<<" is discarded. The low position is 0.

a << 2 output 240, binary interpretation: 1111 0000

>>

Right movement operator: shift all the binary digits of the operand to the left of ">>" to the right by several digits, and the number on the right side of ">>" Number of bits moved

a >> 2 Output result 15, binary interpretation: 0000 1111

Python Logic Operators

The Python language supports logical operators. The following assumes that the variable a is 10 and b is 20:

Operator

Logical Expression

Description

Instance

and

x and y

Boolean "and" - If x is False, x and y return False, otherwise it returns the calculated value of y.

(a and b) returns 20.

or

x or y

Boolean "or" - If x is True, it returns the value of x, otherwise it returns the computed value of y.

(a or b) returns 10.

not

not x

Boolean "Non" - Returns 0 if x is True. If x is False, it returns True.

not(a and b) returns False

The output of the above example:

Instance (Python 3.0+)

#!/usr/bin/python3a = 10b = 20if(aandb):
print("1 - The variables a and b are both true")else:
print("1 - Variables a and b have one not true")if(aorb):
print("2 -The variables a and b are both true, or one of the variables is true")else:
The variables a and b are both true, or print("2 - variables a and b are not true")
One variable is
# Modify the value of the variable aa = 0if(aandb):
print("3 - The variables a and b are both true")else:
print("3 - Variables a and b have one not true")if(aorb):
print("4 - The variables a and b are both true, or one of the variables is true")else:
print("4 - The variables a and b are not true")ifnot(aandb):
print("5 - The variables a and b are both false，Or one of the variables is false")else:
print("5 - The variables a and b are both true")

The output of the above example:

1 - Variables a and b are both true
2 - Variables a and b are both true, or one of the variables is true
3 - Variables a and b have one not true
4 - Variables a and b are both true, or one of the variables is true
5 - Variables a and b are both false, or one of the variables is false

Python member operator

In addition to some of the above operators, Python also supports member operators, which contain a series of members, including strings, lists, or tuples.

Operator

Description

Instance

in

Returns True if the value is found in the specified sequence, otherwise returns False.

x In the y sequence, if x returns True in the y sequence.

not in

Returns True if no value is found in the specified sequence, otherwise returns False.

x is not in the y sequence, if x does not return True in the y sequence.

The following example demonstrates the operation of all Python member operators:

Instance (Python 3.0+)

#!/usr/bin/python3a = 10b = 20list = [1, 2, 3, 4, 5];
if(ainlist):
print("1 - Variable a in the given list list in")else:
print("1 - The variable a is not in the list in the given list")if(bnotinlist):
print("2 - The variable b is not in the list in the given list")else:
print("2 - The variable b is in the list in the given list")# Modify the value of the variable aa = 2if(ainlist):
print("3 - The variable a is in the list in the given list")else:
print("3 - The variable a is not in the list in the given list")

The output of the above example:

1 - variable a is not in the list in the given list
2 - variable b is not in the list in the given list
3 - variable a in the list in the given list

Python Identity Operator

The identity operator is used to compare the storage units of two objects

Operator

Description

Instance

is

Is is to determine whether the two identifiers are referenced from an object

x is y, similar to id(x) == id(y) , Returns True if the same object is referenced, otherwise returns False

is not

is not is to determine if two identifiers are referenced from different objects

x is not y , similar to id (a) != id(b). Returns True if the reference is not the same object, otherwise returns False.

The following example demonstrates the operation of all Python identity operators:

Instance (Python 3.0+)

#!/usr/bin/python3a = 20b = 20if(aisb):
print("1 - a Same as b")else:
print("1 - a And b do not have the same identifier")if(id(a) == id(b)):
print("2 - a Same as b")else:
print("2 - a And b do not have the same identifier")# Modify the value of the variable bb = 30if(aisb):
print("3 - a Same as b")else:
print("3 - a And b do not have the same identifier")if(aisnotb):
print("4 - a And b do not have the same identifier")else:
print("4 - a Same as b")

The output of the above example:

1 - a and b have the same logo
2 - a and b have the same logo
3 - a and b do not have the same logo
4 - a and b do not have the same logo

is and == difference:

is is used to determine whether two variable reference objects are the same, and == is used to determine whether the values of the reference variables are equal.