YouTip LogoYouTip

Python Operator

Python3.x Python3 operator Module

In Python 2.x versions, the cmp() function was used to compare the size relationship of two lists, numbers, or strings.

In Python 3.X versions, the cmp() function no longer exists. If you need to implement comparison functionality, you need to import the operator module, which is suitable for any object and includes the following methods:

operator.lt(a, b)

operator.le(a, b)

operator.eq(a, b)

operator.ne(a, b)

operator.ge(a, b)

operator.gt(a, b)

operator. __lt__ (a, b)

operator. __le__ (a, b)

operator. __eq__ (a, b)

operator. __ne__ (a, b)

operator. __ge__ (a, b)

operator. __gt__ (a, b)

operator.lt(a, b) is the same as a < b, operator.le(a, b) is the same as a <= b, operator.eq(a, b) is the same as a == b, operator.ne(a, b) is the same as a != b, operator.gt(a, b) is the same as a > b, operator.ge(a, b) is the same as a >= b.

Example

# Import operator module

import operator

# Numbers

x = 10

y = 20

print("x:", x, ", y:", y)

print("operator.lt(x,y): ", operator.lt(x,y))

print("operator.gt(y,x): ", operator.gt(y,x))

print("operator.eq(x,x): ", operator.eq(x,x))

print("operator.ne(y,y): ", operator.ne(y,y))

print("operator.le(x,y): ", operator.le(x,y))

print("operator.ge(y,x): ", operator.ge(y,x))

print()

# Strings

x = "Google"

y = ""

print("x:", x, ", y:", y)

print("operator.lt(x,y): ", operator.lt(x,y))

print("operator.gt(y,x): ", operator.gt(y,x))

print("operator.eq(x,x): ", operator.eq(x,x))

print("operator.ne(y,y): ", operator.ne(y,y))

print("operator.le(x,y): ", operator.le(x,y))

print("operator.ge(y,x): ", operator.ge(y,x))

print()

# Check return value

print("type((operator.lt(x,y)): ", type(operator.lt(x,y)))

The output of the above code is:

x: 10 , y: 20
operator.lt(x,y): True
operator.gt(y,x): True
operator.eq(x,x): True
operator.ne(y,y): False
operator.le(x,y): True
operator.ge(y,x): True

x: Google , y: 
operator.lt(x,y): True
operator.gt(y,x): True
operator.eq(x,x): True
operator.ne(y,y): False
operator.le(x,y): True
operator.ge(y,x): True

Comparing two lists:

Example

# Import operator module

import operator

a = [1, 2]

b = [2, 3]

c = [2, 3]

print("operator.eq(a,b): ", operator.eq(a,b))

print("operator.eq(c,b): ", operator.eq(c,b))

The output of the above code is:

operator.eq(a,b): False
operator.eq(c,b): True

Operator Functions

The operator module provides a set of efficient functions corresponding to Python's built-in operators. For example, operator.add(x, y) is the same as the expression x+y.

The types of functions include: object comparison operations, logical operations, mathematical operations, and sequence operations.

Object comparison functions apply to all objects, and function names are named according to their corresponding comparison operators.

Many function names are the same as special method names, just without double underscores. For backward compatibility, many functions with double underscores are also retained. For clarity, it is recommended to use functions without double underscores.

Example

# Python Example

# add(), sub(), mul()

# Import operator module

import operator

# Initialize variables

a = 4

b = 3

# Use add() to add two values

print("add() result: ", end="")

print(operator.add(a, b))

# Use sub() to subtract two values

print("sub() result: ", end="")

print(operator.sub(a, b))

# Use mul() to multiply two values

print("mul() result: ", end="")

print(operator.mul(a, b))

The output of the above code is:

add() result: 7
sub() result: 1
mul() result: 12
OperationSyntaxFunction
Additiona + badd(a, b)
String Concatenationseq1 + seq2concat(seq1, seq2)
Containment Testobj in seqcontains(seq, obj)
Divisiona / btruediv(a, b)
Divisiona // bfloordiv(a, b)
Bitwise ANDa & band_(a, b)
Bitwise XORa ^ bxor(a, b)
Bitwise NOT~ ainvert(a)
Bitwise ORa | bor_(a, b)
Exponentiationa ** bpow(a, b)
Identitya is bis_(a, b)
Identitya is not bis_not(a, b)
Index Assignmentobj = vsetitem(obj, k, v)
Index Deletiondel objdelitem(obj, k)
Index Accessobjgetitem(obj, k)
Left Shifta << blshift(a, b)
Moduloa % bmod(a, b)
Multiplicationa * bmul(a, b)
Matrix Multiplicationa @ bmatmul(a, b)
Negation (Arithmetic)- aneg(a)
Negation (Logical)not anot_(a)
Positive+ apos(a)
Right Shifta >> brshift(a, b)
Slice Assignmentseq[i:j] = valuessetitem(seq, slice(i, j), values)
Slice Deletiondel seq[i:j]delitem(seq, slice(i, j))
Slice Accessseq[i:j]getitem(seq, slice(i, j))
String Formattings % objmod(s, obj)
Subtractiona - bsub(a, b)
Truth Testobjtruth(obj)
Comparisona < blt(a, b)
Comparisona <= ble(a, b)
Equalitya == beq(a, b)
Inequalitya != bne(a, b)
Comparisona >= bge(a, b)
Comparisona > bgt(a, b)
← Ref Math ECsharp Quiz β†’