Python
Operators

Understanding Operators in Python: Arithmetic, Comparison, and Logical Operators

Operators are symbols used to perform various operations on variables and values. They allow you to manipulate data and make decisions in your programs. In this post, we will explore three important categories of operators in Python: Arithmetic Operators, Comparison Operators, and Logical Operators. We'll cover each category with examples to demonstrate their usage.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations on numeric values. Here are the common arithmetic operators in Python:

  • Addition +: Adds two operands.
  • Subtraction -: Subtracts the right operand from the left operand.
  • Multiplication *: Multiplies two operands.
  • Division /: Divides the left operand by the right operand (result is a float).
  • Floor Division //: Divides the left operand by the right operand and rounds down to the nearest integer.
  • Modulus %: Returns the remainder of the division of the left operand by the right operand.
  • Exponentiation **: Raises the left operand to the power of the right operand.

Here are some examples of arithmetic operations:

a = 10
b = 3
 
addition_result = a + b
print(addition_result)  # Output: 13
 
subtraction_result = a - b
print(subtraction_result)  # Output: 7
 
multiplication_result = a * b
print(multiplication_result)  # Output: 30
 
division_result = a / b
print(division_result)  # Output: 3.3333333333333335
 
floor_division_result = a // b
print(floor_division_result)  # Output: 3
 
modulus_result = a % b
print(modulus_result)  # Output: 1
 
exponentiation_result = a ** b
print(exponentiation_result)  # Output: 1000

Comparison Operators

Comparison operators are used to compare two values and return a Boolean value (True or False) based on the result of the comparison. Here are the common comparison operators in Python:

  • Equal ==: Returns True if the operands are equal.
  • Not Equal !=: Returns True if the operands are not equal.
  • Greater Than >: Returns True if the left operand is greater than the right operand.
  • Less Than <: Returns True if the left operand is less than the right operand.
  • Greater Than or Equal To >=: Returns True if the left operand is greater than or equal to the right operand.
  • Less Than or Equal To <=: Returns True if the left operand is less than or equal to the right operand.

Here are some examples of comparison operations:

x = 5
y = 10
 
is_equal = x == y
print(is_equal)  # Output: False
 
is_not_equal = x != y
print(is_not_equal)  # Output: True
 
is_greater_than = x > y
print(is_greater_than)  # Output: False
 
is_less_than = x < y
print(is_less_than)  # Output: True
 
is_greater_than_or_equal = x >= y
print(is_greater_than_or_equal)  # Output: False
 
is_less_than_or_equal = x <= y
print(is_less_than_or_equal)  # Output: True

Logical Operators

Logical operators are used to combine and manipulate Boolean values. They allow you to perform logical operations such as AND, OR, and NOT. Here are the common logical operators in Python:

  • Logical AND and: Returns True if both operands are True.
  • Logical OR or: Returns True if at least one of the operands is True.
  • Logical NOT not: Returns True if the operand is False and False if the operand is True.

Here are some examples of logical operations:

p = True
q = False
 
logical_and_result = p and q
print(logical_and_result)  # Output: False
 
logical_or_result = p or q
print(logical_or_result)  # Output: True
 
logical_not_result = not p
print(logical_not_result)  # Output: False