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:
| Operator | Description | Example |
|---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 5 - 3 = 2 |
* | Multiplication | 5 * 3 = 15 |
/ | Division | 5 / 2 = 2.5 |
// | Floor Division | 5 // 2 = 2 |
% | Modulus | 5 % 2 = 1 |
** | Exponentiation | 2 ** 3 = 8 |
2. Assignment Operators
Used to assign values to variables:
| Operator | Description | Example |
= | Assign | x = 5 |
+= | Add and assign | x += 3 (x = x + 3) |
-= | Subtract and assign | x -= 1 |
*= | Multiply and assign | x *= 2 |
/= | Divide and assign | x /= 4 |
%= | Modulus and assign | x %= 3 |
3. Comparison Operators
Used to compare two values and return Boolean results:
| Operator | Description | Example |
== | Equal to | x == y |
!= | Not equal to | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal | x >= y |
<= | Less than or equal | x <= y |
4. Logical Operators
Used to combine multiple Boolean expressions:
| Operator | Description | Example |
and | Logical AND | x > 0 and x < 10 |
or | Logical OR | x < 0 or x > 10 |
not | Logical NOT | not(x > 5) |
5. Bitwise Operators
Operate at the binary level:
| Operator | Description | Example | ||
& | AND | 5 & 3 = 1 | ||
| ` | ` | OR | `5 | 3 = 7` |
^ | XOR | 5 ^ 3 = 6 | ||
~ | NOT | ~5 = -6 | ||
<< | Left Shift | 5 << 1 = 10 | ||
>> | Right Shift | 5 >> 1 = 2 |
6. Identity Operators
Check object identity (used in Python):
| Operator | Description | Example |
is | True if same object | x is y |
is not | True if not same object | x is not y |
7. Membership Operators
Test membership in a sequence:
| Operator | Description | Example |
in | True if present | 5 in [1,2,3,5] |
not in | True if not present | 7 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









