DEV Community

Divya Dixit
Divya Dixit

Posted on

πŸš€ Mastering Python Operators: Comparison, Bitwise & Assignment

In our previous posts, we covered logical and arithmetic operators in Python. Now, let's take a deep dive into comparison (relational) operators, followed by bitwise and assignment operators, which play a crucial role in data processing and optimization.

πŸ” Understanding Comparison (Relational) Operators

As the name suggests, comparison operators are used to compare two values and determine relationships like greater than, lesser than, or equality. They are fundamental in decision-making processes in programming.

πŸ“Œ Examples of Comparison Operators in Action

print(10 > 3)   # True (10 is greater than 3)
print(10 >= 10) # True (10 is equal to 10)
print(5 < 8)    # True (5 is less than 8)
print(7 <= 7)   # True (7 is equal to 7)
print(4 == 4)   # True (Both values are equal)
print(6 != 9)   # True (6 is not equal to 9)
Enter fullscreen mode Exit fullscreen mode

List of Python Comparison Operators

Greater than 10 > 3 True
< Less than 5 < 8 True
= Greater than or equal to 10 >= 10 True
<= Less than or equal to 7 <= 7 True
== Equal to 4 == 4 True
!= Not equal to 6 != 9 True

πŸ”₯ Where Are Comparison Operators Used?
Comparison operators are commonly used in conditional statements, loops, and filtering data.

age = 18
if age >= 18:
    print("You are eligible to vote!")
else:
    print("You are not eligible to vote.")
Enter fullscreen mode Exit fullscreen mode

Bitwise Operators in Python

Bitwise operators allow us to perform operations at the binary level (bit by bit). These operators are commonly used in low-level programming, cryptography, and optimizing algorithms.

πŸ”Ή Examples of Bitwise Operators
Let’s take a simple example using 5 and 3:

a = 5 # Binary: 0101
b = 3 # Binary: 0011

print(a & b)  # AND  -> 1  (Binary: 0001)
print(a | b)  # OR   -> 7  (Binary: 0111)
print(a ^ b)  # XOR  -> 6  (Binary: 0110)
print(~a)     # NOT  -> -6 (Binary: Inverts bits)
print(a << 1) # Left Shift  -> 10 (Binary: 1010)
print(b >> 1) # Right Shift -> 1  (Binary: 0001)
Enter fullscreen mode Exit fullscreen mode

πŸ“Š** List of Bitwise Operators**
(a=5, b=3)
& Bitwise AND 5 & 3 0001 1
Bitwise OR 5 3
^ Bitwise XOR 5 ^ 3 0110 6
~ Bitwise NOT ~5 -0110 -6
<< Left Shift 5 << 1 1010 10

Right Shift 3 >> 1 0001 1

πŸ”₯ Where Are Bitwise Operators Used?
Cryptography πŸ›‘οΈ (e.g., encryption algorithms)
Image Processing πŸ–ΌοΈ (pixel manipulation)
Low-level System Operations πŸ–₯️ (memory optimization)
Fast Computations ⚑ (bitwise tricks can replace arithmetic operations)

Assignment Operators in Python

Assignment operators modify variables efficiently by performing operations and storing the result in the same variable.

πŸ”Ή Examples of Assignment Operators

x = 10  # Simple assignment
x += 5  # Equivalent to x = x + 5
x -= 2  # Equivalent to x = x - 2
x *= 3  # Equivalent to x = x * 3
x /= 2  # Equivalent to x = x / 2
x %= 3  # Equivalent to x = x % 3
x //= 2 # Equivalent to x = x // 2
x **= 2 # Equivalent to x = x ** 2
Enter fullscreen mode Exit fullscreen mode

πŸ“Š List of Assignment Operators

= Assign value x = 10 10
+= Add and assign x += 5 15
-= Subtract and assign x -= 2 8
= Multiply and assign x *= 3 30
/= Divide and assign x /= 2 5.0
%= Modulus and assign x %= 3 2
//= Floor divide and assign x //= 2 5
*
= Exponentiate and assign x **= 2 100

πŸ”₯ Where Are Assignment Operators Used?
Mathematical Calculations (shortcuts in algebraic operations)
Updating Variables in Loops (x += 1 for counter increments)
Game Development (modifying game scores, physics updates)
Data Processing (efficient calculations in big data applications)

🎯 Conclusion
Comparison operators are essential for decision-making in Python.
Bitwise operators allow for efficient binary-level computations.
Assignment operators optimize variable manipulation for better performance.
These operators are powerful tools for writing efficient Python programs. Stay tuned for our next post, where we’ll explore identity and membership operators in Python! πŸš€

Top comments (0)