Operators | Dataplexa

Operators in Python

Operators are special symbols in Python that perform actions on values or variables. They allow your program to do tasks like calculations, comparisons, combining conditions, assigning values, and much more. Understanding operators is essential because they appear in almost every Python program you will ever write.

What Are Operators?

An operator works with one or more values, called operands, to produce a result. For example, in 10 + 5, the operator is + and the operands are 10 and 5.

Types of Operators in Python

Python supports several categories of operators. Each category performs different types of tasks.

Operator TypeDescription
ArithmeticUsed for mathematical operations
AssignmentAssign or update values in variables
ComparisonCompare two values
LogicalCombine multiple conditions
IdentityCheck object identity in memory
MembershipCheck if a value exists in a sequence
BitwiseWork on bits (0s and 1s)

1. Arithmetic Operators

Used for basic mathematical tasks such as addition, subtraction, multiplication, etc.

a = 10
b = 3

print(a + b)   # Addition
print(a - b)   # Subtraction
print(a * b)   # Multiplication
print(a / b)   # Division
print(a % b)   # Modulus (remainder)
print(a ** b)  # Exponent (power)
print(a // b)  # Floor division

2. Assignment Operators

Assignment operators store values into variables. Some also update the variable automatically.

OperatorMeaningExample
=Assign valuex = 10
+=Add and assignx += 5
-=Subtract and assignx -= 3
*=Multiply and assignx *= 2
/=Divide and assignx /= 4
%=Modulus and assignx %= 2

3. Comparison Operators

These operators compare two values and return either True or False.

x = 10
y = 20

print(x == y)  # Equal
print(x != y)  # Not equal
print(x > y)   # Greater than
print(x < y)   # Less than
print(x >= y)  # Greater or equal
print(x <= y)  # Less or equal

4. Logical Operators

Combine multiple conditions. Very useful in decision-making programs.

a = 5
b = 10

print(a > 2 and b > 5)   # True
print(a > 10 or b > 2)   # True
print(not(a > 2))        # False

5. Identity Operators

Identity operators compare memory locations, not values.

x = [1, 2, 3]
y = x
z = [1, 2, 3]

print(x is y)   # True (same memory)
print(x is z)   # False (different memory)
print(x is not z)

6. Membership Operators

Check if a value exists in a list, string, tuple, etc.

numbers = [10, 20, 30]

print(20 in numbers)       # True
print(50 not in numbers)   # True

7. Bitwise Operators

Work directly with bits (0s and 1s). Rare in basic programs, but useful in low-level tasks.

a = 5   # 0101
b = 3   # 0011

print(a & b)   # AND
print(a | b)   # OR
print(a ^ b)   # XOR
print(~a)      # NOT
print(a << 1)  # Left shift
print(a >> 1)  # Right shift

Real-World Example: Simple Salary Calculator

This program calculates salary based on workdays, daily pay, and bonus conditions using operators.

days_worked = 22
daily_pay = 1500

base_salary = days_worked * daily_pay

bonus = 2000 if days_worked > 20 else 0

total_salary = base_salary + bonus

print("Base Salary:", base_salary)
print("Bonus:", bonus)
print("Total Salary:", total_salary)

📝 Practice Exercises


  1. Create a program that adds, subtracts, multiplies, and divides two numbers entered by the user.
  2. Write a program that checks if a number is greater than 50 and also even.
  3. Use membership operators to check if your name exists in a list of names.
  4. Use comparison and logical operators to check if a person is eligible for a loan (age > 21 and salary > 30000).

✅ Practice Answers


Answer 1:

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)

Answer 2:

num = int(input("Enter a number: "))

if num > 50 and num % 2 == 0:
    print("Number is greater than 50 and even")
else:
    print("Condition not met")

Answer 3:

names = ["Sreekanth", "Ravi", "Asha"]

print("Sreekanth" in names)

Answer 4:

age = int(input("Enter age: "))
salary = int(input("Enter salary: "))

if age > 21 and salary > 30000:
    print("Eligible for loan")
else:
    print("Not eligible")