What are Python Operators?

In Python, operators are symbols used to perform operations on variables and values. They are the foundation of most programming tasks, helping you perform mathematical calculations, comparisons, logical operations, and more. Python supports a wide variety of operators, categorized into different types based on their functionality.

Types of Python Operators

Python operators are broadly classified into the following categories:

  1. Arithmetic Operators
  2. Comparison (Relational) Operators
  3. Logical Operators
  4. Assignment Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity Operators

1. Arithmetic Operators

Used for basic mathematical operations like addition, subtraction, multiplication, etc.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b
**Exponentiationa ** b
//Floor Divisiona // b

Example: Arithmetic Operators.

a = 10
b = 3

print("Addition:", a + b)         # Output: 13
print("Subtraction:", a - b)      # Output: 7
print("Multiplication:", a * b)   # Output: 30
print("Division:", a / b)         # Output: 3.3333333333333335
print("Modulus:", a % b)          # Output: 1
print("Exponentiation:", a ** b)  # Output: 1000
print("Floor Division:", a // b)  # Output: 3

2. Comparison Operators

Used to compare values and return a Boolean (True or False).

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example:

x = 5
y = 10

print(x == y)   # Output: False
print(x != y)   # Output: True
print(x > y)    # Output: False
print(x <= y)   # Output: True

3. Logical Operators

Used to combine conditional statements.

OperatorDescriptionExample
andReturns True if both conditions are Truex and y
orReturns True if at least one condition is Truex or y
notReverses the resultnot x

Example:

a = True
b = False

print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False

4. Assignment Operators

Used to assign values to variables.

OperatorDescriptionExample
=Assignx = 5
+=Add and assignx += 3
-=Subtract and assignx -= 2
*=Multiply and assignx *= 2
/=Divide and assignx /= 2

Example:

x = 5
x += 3  # Same as x = x + 3
print(x)  # Output: 8

5. Membership Operators

Used to check if a value is in a sequence like a string, list, or tuple.

OperatorDescriptionExample
inReturns True if found'a' in 'apple'
not inReturns True if not found'x' not in 'apple'

Example:

fruits = ["apple", "banana", "cherry"]
print("apple" in fruits)      # Output: True
print("grape" not in fruits)  # Output: True

6. Identity Operators

Used to compare memory locations of two objects.

OperatorDescriptionExample
isReturns True if same objectx is y
is notReturns True if not same objectx is not y

Example:

x = ["apple", "banana"]
y = x
z = ["apple", "banana"]

print(x is y)       # Output: True
print(x is z)       # Output: False
print(x == z)       # Output: True

Python operators are essential for performing various operations on data. Whether you’re calculating numbers, comparing values, or checking conditions, operators make your code more dynamic and functional. Understanding these operators will help you write efficient and powerful Python programs.

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *