Introduction

Operators in Python are special symbols that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. In this guide, we’ll explore three major types of operators: arithmetic, comparison, and logical.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division.

  • Addition (+): Adds two operands.
  • Subtraction (-): Subtracts right operand from the left.
  • Multiplication (*): Multiplies two operands.
  • Division (/): Divides left operand by the right one (result is always a float).
  • Floor Division (//): Divides and returns the integer value of the quotient. It dumps the digits after the decimal.
  • Modulus (%): Divides left operand by the right and returns remainder.
  • Exponentiation (**): Performs exponential (power) calculation on operators.

Example:

  x = 15
y = 4

print('x + y =', x + y)  # Output: 19
print('x - y =', x - y)  # Output: 11
print('x * y =', x * y)  # Output: 60
print('x / y =', x / y)  # Output: 3.75
print('x // y =', x // y) # Output: 3
print('x % y =', x % y)   # Output: 3
print('x ** y =', x ** y) # Output: 50625
  

Comparison Operators

Comparison operators are used to compare values. They return a Boolean value (either True or False).

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

Example:

  a = 10
b = 20

print('a == b is', a == b)  # Output: False
print('a != b is', a != b)  # Output: True
print('a > b is', a > b)    # Output: False
print('a < b is', a < b)    # Output: True
print('a >= b is', a >= 10) # Output: True
print('a <= b is', a <= 20) # Output: True
  

Logical Operators

Logical operators are used to combine conditional statements.

  • and: True if both operands are true.
  • or: True if at least one of the operands is true.
  • not: True if operand is false (complements the operand).

Example:

  c = True
d = False

print('c and d is', c and d)  # Output: False
print('c or d is', c or d)    # Output: True
print('not c is', not c)      # Output: False
  

Conclusion

Operators play a critical role in constructing expressions and making decisions in your programs. This guide has detailed the usage of arithmetic, comparison, and logical operators in Python with examples, helping you understand how to apply these concepts effectively in your coding tasks.

Last updated 04 May 2024, 04:36 UTC . history