Description

In computer science and programming, an operator is a symbol or keyword used to perform operations on variables and values. Operators are fundamental components of expressions and enable a variety of computations, including arithmetic, logical comparisons, assignment, bitwise manipulation, and more. Operators can act on one, two, or more operands, depending on their type.

Languages such as Python, C++, Java, and JavaScript provide a rich set of built-in operators to facilitate mathematical computations, decision-making logic, and memory manipulation.

Categories of Operators

1. Arithmetic Operators

Used for performing basic mathematical calculations:

OperatorDescriptionExample
+Addition5 + 3 = 8
-Subtraction5 - 3 = 2
*Multiplication5 * 3 = 15
/Division5 / 2 = 2.5
//Floor Division5 // 2 = 2
%Modulus5 % 2 = 1
**Exponentiation2 ** 3 = 8

2. Assignment Operators

Used to assign values to variables:

OperatorDescriptionExample
=Assignx = 5
+=Add and assignx += 3 (x = x + 3)
-=Subtract and assignx -= 1
*=Multiply and assignx *= 2
/=Divide and assignx /= 4
%=Modulus and assignx %= 3

3. Comparison Operators

Used to compare two values and return Boolean results:

OperatorDescriptionExample
==Equal tox == y
!=Not equal tox != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equalx >= y
<=Less than or equalx <= y

4. Logical Operators

Used to combine multiple Boolean expressions:

OperatorDescriptionExample
andLogical ANDx > 0 and x < 10
orLogical ORx < 0 or x > 10
notLogical NOTnot(x > 5)

5. Bitwise Operators

Operate at the binary level:

OperatorDescriptionExample
&AND5 & 3 = 1
``OR`53 = 7`
^XOR5 ^ 3 = 6
~NOT~5 = -6
<<Left Shift5 << 1 = 10
>>Right Shift5 >> 1 = 2

6. Identity Operators

Check object identity (used in Python):

OperatorDescriptionExample
isTrue if same objectx is y
is notTrue if not same objectx is not y

7. Membership Operators

Test membership in a sequence:

OperatorDescriptionExample
inTrue if present5 in [1,2,3,5]
not inTrue if not present7 not in [1,2,3]

Operator Precedence

Precedence determines the order in which operations are performed.

Example:

result = 5 + 2 * 3
# Output: 11, because * has higher precedence than +

Parentheses () can be used to override precedence.

Operator Overloading

In object-oriented languages like Python and C++, operators can be overloaded to behave differently depending on the operands.

Example:

class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)

Compound Expressions

Operators can be chained together:

if 1 < x < 10:
    print("x is between 1 and 10")

Use in Other Domains

  • Database Queries: SQL uses =, >, <, LIKE, etc.
  • Spreadsheets: Excel uses operators like +, -, *, / within formulas.
  • Machine Learning: Operators in tensor libraries (e.g., TensorFlow) support element-wise computation.

Summary

Operators are building blocks of programming expressions and logic. By mastering their types, usage, and precedence, developers can write efficient, concise, and readable code. Operator overloading and compound logic expressions further expand their versatility.

Related Terms

  • Expression
  • Operand
  • Precedence
  • Operator Overloading
  • Boolean Logic
  • Bitwise Operation
  • Assignment
  • Arithmetic Expression