Python Operators: A Comprehensive Guide

Introduction

link to this section

In Python, operators are symbols or special keywords used to perform operations on operands (variables, values, expressions). They are fundamental to programming and allow us to perform various computations and manipulations on data. In this blog post, we'll explore the different types of operators available in Python and their usage.

Arithmetic Operators

link to this section

Arithmetic operators are used to perform mathematical operations.

  • Addition (+) : Adds two operands.
  • Subtraction (-) : Subtracts the second operand from the first.
  • Multiplication (*) : Multiplies two operands.
  • Division (/) : Divides the first operand by the second (float division).
  • Floor Division (//) : Divides the first operand by the second and returns the quotient as an integer.
  • Modulus (%) : Returns the remainder of the division.
  • Exponentiation (**) : Raises the first operand to the power of the second.
a = 10 
b = 3 
print(a + b) # 13 
print(a - b) # 7 
print(a * b) # 30 
print(a / b) # 3.3333333333333335 
print(a // b) # 3 
print(a % b) # 1 
print(a ** b) # 1000 

Comparison Operators

link to this section

Comparison operators are used to compare the values of two operands.

  • Equal to (==) : Returns True if the operands are equal.
  • Not equal to (!=) : Returns True if the operands are not equal.
  • Greater than (>) : Returns True if the first operand is greater than the second.
  • Less than (<) : Returns True if the first operand is less than the second.
  • Greater than or equal to (>=) : Returns True if the first operand is greater than or equal to the second.
  • Less than or equal to (<=) : Returns True if the first operand is less than or equal to the second.
x = 5 
y = 10 

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

Logical Operators

link to this section

Logical operators are used to combine conditional statements.

  • AND (and) : Returns True if both operands are True.
  • OR (or) : Returns True if at least one of the operands is True.
  • NOT (not) : Returns the opposite of the operand's value.
p = True 
q = False 

print(p and q) # False 
print(p or q) # True 
print(not p) # False 

Bitwise Operators

link to this section

Bitwise operators perform operations on binary representations of integers.

  • Bitwise AND (&) : Sets each bit to 1 if both bits are 1.
  • Bitwise OR (|) : Sets each bit to 1 if at least one of the bits is 1.
  • Bitwise XOR (^) : Sets each bit to 1 if only one of the bits is 1.
  • Bitwise NOT (~) : Inverts all the bits.
  • Left Shift (<<) : Shifts the bits to the left by a specified number of positions.
  • Right Shift (>>) : Shifts the bits to the right by a specified number of positions.
a = 60 # 0011 1100 
b = 13 # 0000 1101 

print(a & b) # 12 (0000 1100) 
print(a | b) # 61 (0011 1101) 
print(a ^ b) # 49 (0011 0001) 
print(~a) # -61 (1100 0011) 
print(a << 2) # 240 (1111 0000) 
print(a >> 2) # 15 (0000 1111) 

Assignment Operators

link to this section

Assignment operators are used to assign values to variables.

  • Assignment (=) : Assigns the value on the right to the variable on the left.
  • Addition Assignment (+=) : Adds the value on the right to the variable on the left and assigns the result to the variable.
  • Subtraction Assignment (-=) : Subtracts the value on the right from the variable on the left and assigns the result to the variable.
  • Multiplication Assignment (*=) : Multiplies the variable on the left by the value on the right and assigns the result to the variable.
  • Division Assignment (/=) : Divides the variable on the left by the value on the right and assigns the result to the variable.
  • Modulus Assignment (%=) : Computes the modulus of the variable on the left by the value on the right and assigns the result to the variable.
  • Floor Division Assignment (//=) : Performs floor division on the variable on the left by the value on the right and assigns the result to the variable.
  • Exponentiation Assignment (**=) : Raises the variable on the left to the power of the value on the right and assigns the result to the variable.
x = 10 
x += 5 # Equivalent to x = x + 5 
x -= 3 # Equivalent to x = x - 3 
x *= 2 # Equivalent to x = x * 2 
x /= 4 # Equivalent to x = x / 4 
x %= 3 # Equivalent to x = x % 3 
x //= 2 # Equivalent to x = x // 2 
x **= 3 # Equivalent to x = x ** 3 

Conclusion

link to this section

Python provides a wide range of operators that allow you to perform various operations on data and control the flow of your program. Understanding these operators and their usage is essential for writing efficient and concise Python code. We've covered the most commonly used operators in this guide, but Python offers many more operators and functionalities for you to explore and leverage in your programming journey.