Tutorials Home   >   Programming Basics   >   Operators in Programming

Operators in Programming

1. What are Operators?

In programming, operators are special symbols or keywords used to perform operations on data. They can work with numbers, text, or other types of data depending on the operation.

Think of operators as action symbols. For example:

  • + adds numbers

  • * multiplies numbers

  • == checks if two values are equal

Operators allow programs to calculate values, compare data, and make decisions.


2. Types of Operators

Operators are usually divided into several categories:

  1. Arithmetic Operators – For mathematical calculations

  2. Assignment Operators – For storing values in variables

  3. Comparison Operators – For comparing values

  4. Logical Operators – For combining conditions

  5. Bitwise Operators – For manipulating binary data

  6. Membership Operators (Python-specific) – To check membership in sequences

  7. Identity Operators (Python-specific) – To check object identity


3. Arithmetic Operators

Arithmetic operators perform basic mathematical operations.

Operator Description Example (Python) Output
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division (float) 5 / 2 2.5
// Division (integer) 5 // 2 2
% Modulus (remainder) 5 % 2 1
** Exponentiation 5 ** 2 25

Java Example (no // or **):

int a = 5, b = 2;
System.out.println(a + b); // 7
System.out.println(a / b); // 2
System.out.println(a % b); // 1

4. Assignment Operators

Assignment operators are used to store values in variables. The basic operator is =, but there are shorthand operators for combining operations with assignment.

Operator Description Example Output
= Assign value x = 5 5
+= Add and assign x += 3 x = x+3
-= Subtract and assign x -= 2 x = x-2
*= Multiply and assign x *= 4 x = x*4
/= Divide and assign x /= 2 x = x/2
%= Modulus and assign x %= 3 x = x%3

Example:

x = 10
x += 5 # x = 15
x *= 2 # x = 30
print(x)

5. Comparison Operators

Comparison operators compare two values and return a Boolean result (True or False).

Operator Description Example Output
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 5 > 3 True
< Less than 5 < 3 False
>= Greater or equal 5 >= 5 True
<= Less or equal 3 <= 5 True

Example in Python:

a = 5
b = 3
print(a > b) # True
print(a == b) # False

Java example:

int a = 5, b = 3;
System.out.println(a != b); // true
System.out.println(a <= b); // false

6. Logical Operators

Logical operators are used to combine multiple conditions. They return True or False.

Operator Description Python Example Output
and True if both True True and False False
or True if one is True True or False True
not Inverts Boolean value not True False

Example:

age = 20
has_id = True
if age >= 18 and has_id:
print("Entry allowed")

In Java, use &&, ||, !:

int age = 20;
boolean hasId = true;
if(age >= 18 && hasId){
System.out.println("Entry allowed");
}

7. Bitwise Operators

Bitwise operators work on binary representations of integers.

Operator Description Example Output
& AND 5 & 3 1
` ` OR `5
^ XOR 5 ^ 3 6
~ NOT ~5 -6
<< Left shift 5 << 1 10
>> Right shift 5 >> 1 2

Python Example:

a = 5 # 0101 in binary
b = 3 # 0011 in binary
print(a & b) # 1 (0001)
print(a | b) # 7 (0111)

8. Membership and Identity Operators (Python-specific)

  • Membership Operators: Check if a value exists in a sequence.

fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("mango" not in fruits) # True
  • Identity Operators: Check if two objects are the same in memory.

a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True
print(a is c) # False

9. Operator Precedence

Operator precedence determines the order in which operators are evaluated. For example:

  1. Parentheses ()

  2. Exponentiation **

  3. Multiplication *, Division /, Modulus %

  4. Addition +, Subtraction -

  5. Comparison <, >, ==

  6. Logical and, or, not

Example:

result = 5 + 3 * 2 # Multiplication happens first
print(result) # Output: 11
result = (5 + 3) * 2
print(result) # Output: 16

10. Best Practices for Using Operators

  1. Use parentheses to make complex expressions clear.

  2. Understand operator precedence to avoid unexpected results.

  3. Use logical operators carefully to combine conditions.

  4. Avoid mixing types (e.g., string + number) without conversion.

  5. Comment complex expressions to make them understandable.


11. Summary

Operators are the building blocks of computations in programming.

  • Arithmetic operators perform calculations.

  • Assignment operators store and update values.

  • Comparison operators check relationships between values.

  • Logical operators combine conditions.

  • Bitwise operators manipulate binary numbers.

  • Membership and identity operators help check values and objects in Python.

Understanding operators and their precedence is essential for writing correct and readable programs. Operators allow you to perform calculations, make decisions, and manipulate data efficiently.