Operators in Python

  • Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

Types of Arithmetic Operators

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

Example

a = 10
b = 3print("Addition:", a + b) # 13
print("Subtraction:", a - b) # 7
print("Multiplication:", a * b) # 30
print("Division:", a / b) # 3.33
print("Floor Division:", a // b) # 3
print("Modulus:", a % b) # 1
print("Exponent:", a ** b) # 1000

Key Points

  • / always returns a float
  • // removes the decimal part
  • % is useful for checking even/odd numbers

  • Comparison Operators

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

Operators

OperatorMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example

x = 5
y = 10print(x == y)   # False
print(x != y)   # True
print(x > y)    # False
print(x < y)    # True
print(x >= y)   # False
print(x <= y)   # True

Use Case

Used in decision-making statements like if, while.


  • Logical Operators

Logical operators are used to combine multiple conditions.

Operators

OperatorDescription
andTrue if both conditions are true
orTrue if at least one condition is true
notReverses the condition

Example

a = True
b = Falseprint(a and b)  # False
print(a or b)   # True
print(not a)    # False

Real Example

age = 20
print(age > 18 and age < 60)  # True

  • Assignment Operators

Assignment operators are used to assign values and update variables.

Operators

OperatorExampleMeaning
=x = 5Assign
+=x += 3x = x + 3
-=x -= 2x = x – 2
*=x *= 4x = x * 4
/=x /= 2x = x / 2
%=x %= 3x = x % 3
//=x //= 2x = x // 2
**=x **= 2x = x ** 2

Example

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

Advantage

  • Reduces code length
  • Improves readability

  • Bitwise Operators

Bitwise operators work on binary representations of numbers.

Operators

OperatorMeaning
&AND
^XOR
~NOT
<<Left Shift
>>Right Shift

Example

a = 5  # 101
b = 3  # 011print(a & b)  # 1
print(a | b)  # 7
print(a ^ b)  # 6
print(~a)     # -6
print(a << 1) # 10
print(a >> 1) # 2

Explanation

  • & compares bits and returns 1 if both bits are 1
  • | returns 1 if at least one bit is 1
  • << shifts bits to the left (multiplies by 2)

  • Membership Operators

Membership operators check whether a value exists in a sequence.

Operators

  • in
  • not in

Example

nums = [1, 2, 3, 4]print(2 in nums)       # True
print(5 not in nums)   # True

Use Case

  • Searching elements in lists, strings, tuples

  • Identity Operators

Identity operators check whether two variables refer to the same object in memory.

Operators

  • is
  • is not

Example

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

Key Difference

  • == checks value equality
  • is checks memory location

Conclusion

Operators play a crucial role in Python programming by enabling calculations, comparisons, and logical decision-making. From simple arithmetic to complex bitwise operations, operators are used in almost every Python program.

A strong understanding of operators allows learners to write efficient, clean, and logical code. Mastering this topic will make it easier to understand control flow statements, loops, and advanced programming concepts.

Comments

Leave a Reply

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