Python Operators

In Python, operators are special symbols that perform operations on variables and values. There are several types of operators in Python, each serving a different purpose. Here’s a detailed breakdown of the different types of operators in Python:

1. Arithmetic Operators

These operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, etc.

OperatorDescriptionExampleOutput
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 31.666...
//Floor Division5 // 31
%Modulus (Remainder)5 % 32
**Exponentiation5 ** 3125

Example:

x = 5
y = 3

# Addition
print(x + y)  # Output: 8

# Subtraction
print(x - y)  # Output: 2

# Multiplication
print(x * y)  # Output: 15

# Division
print(x / y)  # Output: 1.666...

# Floor Division
print(x // y)  # Output: 1

# Modulus
print(x % y)  # Output: 2

# Exponentiation
print(x ** y)  # Output: 125

2. Comparison (Relational) Operators

These operators compare two values and return a boolean result (True or False).

OperatorDescriptionExampleOutput
==Equal tox == yFalse
!=Not equal tox != yTrue
>Greater thanx > yTrue
<Less thanx < yFalse
>=Greater than or equal tox >= yTrue
<=Less than or equal tox <= yFalse

Example:

x = 5
y = 3

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

3. Assignment Operators

These operators are used to assign values to variables.

OperatorDescriptionExampleOutput
=Assign valuex = 5x = 5
+=Add and assignx += 3x = 8
-=Subtract and assignx -= 3x = 5
*=Multiply and assignx *= 3x = 15
/=Divide and assignx /= 3x = 1.666...
//=Floor division and assignx //= 3x = 1
%=Modulus and assignx %= 3x = 2
**=Exponentiation and assignx **= 3x = 125

Example:

x = 5

# Using assignment operators
x += 3  # x = x + 3
print(x)  # Output: 8

x *= 2  # x = x * 2
print(x)  # Output: 16

4. Logical Operators

Logical operators are used to combine conditional statements. They return a boolean result.

OperatorDescriptionExampleOutput
andReturns True if both statements are truex > 3 and x < 10True
orReturns True if at least one statement is truex < 3 or x > 10False
notReverses the result (True to False, False to True)not(x > 3)False

Example:

x = 5

# Logical Operators
print(x > 3 and x < 10)  # Output: True
print(x < 3 or x > 10)   # Output: False
print(not(x > 3))        # Output: False

5. Identity Operators

Identity operators are used to compare the memory location of two objects.

OperatorDescriptionExampleOutput
isReturns True if both variables point to the same objectx is yTrue/False
is notReturns True if both variables point to different objectsx is not yTrue/False

Example:

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

print(x is y)     # Output: False (they are different objects in memory)
print(x == y)     # Output: True (they have the same content)
print(x is not y) # Output: True (they are not the same object)

6. Membership Operators

Membership operators are used to test if a value is present in a sequence (such as a list, tuple, or string).

OperatorDescriptionExampleOutput
inReturns True if value is found in the sequencex in yTrue/False
not inReturns True if value is not found in the sequencex not in yTrue/False

Example:

x = 5
y = [1, 2, 3, 4, 5]

print(x in y)        # Output: True (5 is in the list)
print(6 not in y)    # Output: True (6 is not in the list)

7. Bitwise Operators

Bitwise operators are used to perform operations on binary numbers at the bit level.

OperatorDescriptionExampleOutput
&Bitwise AND5 & 31
``Bitwise OR`5
^Bitwise XOR5 ^ 36
~Bitwise NOT~5-6
<<Left Shift5 << 110
>>Right Shift5 >> 12

Example:

x = 5  # Binary: 0101
y = 3  # Binary: 0011

# Bitwise AND
print(x & y)  # Output: 1 (Binary: 0001)

# Bitwise OR
print(x | y)  # Output: 7 (Binary: 0111)

# Left Shift
print(x << 1)  # Output: 10 (Binary: 1010)

8. Precedence of Operators

Python evaluates expressions using the precedence of operators. For example, arithmetic operators (*, /) have higher precedence than logical operators (and, or), and operators like not have higher precedence than and and or.

Summary of Operator Categories:

  • Arithmetic Operators: Perform mathematical operations like addition, subtraction, etc.
  • Comparison Operators: Compare values and return True or False.
  • Assignment Operators: Assign values to variables.
  • Logical Operators: Combine boolean expressions.
  • Identity Operators: Compare memory locations of objects.
  • Membership Operators: Check if a value exists in a sequence.
  • Bitwise Operators: Perform bit-level operations.
Leave a Reply 0

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