Python Operators: A Comprehensive Guide

wordpediax
6 min readOct 29, 2023

--

Photo by David Clode on Unsplash

Introduction

Python, a versatile and beginner-friendly programming language, equips developers with a rich set of operators. These operators are the building blocks of expressions, allowing you to perform operations on data, manipulate variables, and make decisions in your code.

In this comprehensive guide, we will explore Python’s operators, learn how to use them and understand their role in everyday programming.

What Are Operators?

Operators in Python are special symbols or keywords that represent computations or actions to be performed on operands. Operands can be variables, constants, or values. Python provides various operators, categorized into various types, each serving a distinct purpose.

Let’s explore some of the most commonly used operators in Python:

1. Arithmetic Operators

Arithmetic operators allow you to perform basic mathematical operations like addition, subtraction, multiplication, and division.

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

Let’s discuss each with an example.

Addition (+)

The addition operator (+) adds two or more numbers or concatenate strings.

#Numbers

x = 5
y = 3
result = x + y # Result is 8
#Strings

x = "coding"
y = "streets"
result = x + y # Result is "codingstreets"

Subtraction (-)

The subtraction operator (-) subtracts one number from another.

x = 7
y = 2
result = x - y # Result is 5

Multiplication (*)

The multiplication operator (*) is used to multiply two or more numbers.

x = 4
y = 6
result = x * y # Result is 24

Division (/)

The division operator (/) is used to divide one number by another.

x = 8
y = 2
result = x / y # Result is 4.0 (division always returns a float)

Floor Division (//)

The floor division operator (//) performs division and returns the largest integer less than or equal to the result.

x = 8
y = 3
result = x // y # Result is 2

Modulus (%)

The modulus operator (%) returns the remainder when one number is divided by another.

x = 7
y = 3
result = x % y # Result is 1 (the remainder of 7 divided by 3)

Exponentiation (**)

The exponentiation operator (**) raises a number to a power.

x = 2
y = 3
result = x ** y # Result is 8 (2^3)

2. Comparison Operators

Comparison operators are used to compare values or expressions and return Boolean results (True or False).

  • Equal (==): Checks if two operands are equal.
  • Not Equal (!=): Checks if two operands are not equal.
  • Greater Than (>): Checks if the left operand is greater than the right.
  • Less Than (<): Checks if the left operand is less than the right.
  • Greater Than or Equal To (>=): Checks if the left operand is greater than or equal to the right.
  • Less Than or Equal To (<=): Check if the left operand is less than or equal to the right.
x = 5
y = 3
result = x == y # Equal: False
result = x != y # Not Equal: True
result = x > y # Greater Than: True
result = x < y # Less Than: False
result = x >= y # Greater Than or Equal To: True
result = x <= y # Less Than or Equal To: False

3. Logical Operators

Logical operators perform logical operations on Boolean values or expressions and return Boolean results.

  • Logical AND (and): Returns True if both operands are True.
  • Logical OR (or): Returns True if at least one operand is True.
  • Logical NOT (not): Returns the opposite Boolean value of the operand.
x = True
y = False
result = x and y # Logical AND: False
result = x or y # Logical OR: True
result = not x # Logical NOT: False

4. Assignment Operators

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 updates the variable.
  • Subtraction Assignment (-=): Subtracts the value on the right from the variable on the left and updates the variable.
  • Multiplication Assignment (*=): Multiplies the variable on the left by the value on the right and updates the variable.
  • Division Assignment (/=): Divides the variable on the left by the value on the right and updates the variable.
  • Floor Division Assignment (//=): Performs floor division and updates the variable.
  • Modulus Assignment (%=): Calculates the modulus and updates the variable.
  • Exponentiation Assignment (=)**: Raises the variable on the left to the power of the value on the right and updates the variable.
x = 5
x += 3 # Addition Assignment: x is now 8
x -= 2 # Subtraction Assignment: x is now 6
x *= 4 # Multiplication Assignment: x is now 24
x /= 2 # Division Assignment: x is now 12.0
x //= 3 # Floor Division Assignment: x is now 4.0
x %= 3 # Modulus Assignment: x is now 1.0
x **= 2 # Exponentiation Assignment: x is now 1.0

5. Bitwise Operators

Bitwise operators perform operations on binary representations of integers.

  • Bitwise AND (&): Performs a bitwise AND operation.
  • Bitwise OR (|): Performs a bitwise OR operation.
  • Bitwise XOR (^): Performs a bitwise XOR operation.
  • Bitwise NOT (~): Performs a bitwise NOT operation.
  • Left Shift (<<): Shifts bits to the left.
  • Right Shift (>>): Shifts bits to the right.
x = 5  # Binary: 0101
y = 3 # Binary: 0011
result = x & y # Bitwise AND: 0001 (Decimal: 1)
result = x | y # Bitwise OR: 0111 (Decimal: 7)
result = x ^ y # Bitwise XOR: 0110 (Decimal: 6)
result = ~x # Bitwise NOT: 11111111111111111111111111111010 (Decimal: -6)
result = x << 2 # Left Shift: 10100 (Decimal: 20)
result = x >> 1 # Right Shift: 0010 (Decimal: 2)

6. Membership Operators

Membership operators check for the existence of a value in a sequence (e.g., a list or string).

  • In: Returns True if a value is found in the sequence.
  • Not In: Returns True if a value is not found in the sequence.
my_list = [1, 2, 3, 4, 5]
result = 3 in my_list # In: True
result = 6 not in my_list # Not In: True

7. Identity Operators

Identity operators compare the memory locations of two objects.

  • Is: Returns True if both operands refer to the same object.
  • Is Not: Returns True if both operands do not refer to the same object.
x = [1, 2, 3]
y = x
z = [1, 2, 3]
result = x is y # Is: True (x and y reference the same object)
result = x is not z # Is Not: True (x and z reference different objects with the same values)

Order of Operations

Just like in traditional mathematics, Python follows a specific order of operations, known as the “PEMDAS” rule, which stands for:

  • Parentheses: Perform operations inside parentheses first.
  • Exponents: Calculate exponentiation (e.g., powers and square roots).
  • Multiplication and Division: Perform these operations from left to right.
  • Addition and Subtraction: Perform these operations from left to right.
result = (2 + 3) * 4  # Here, addition inside the parentheses is done first, resulting in 5 * 4, which equals 20.

Combining Operators

You can combine multiple operators in a single expression to perform complex calculations. Python evaluates expressions by following the order of operations. For example:

result = 5 + 3 * 2  # The multiplication is done first, resulting in 5 + 6, which equals 11.

Using parentheses can help control the order of operations:

result = (5 + 3) * 2  # Now, the addition is done first, resulting in 8 * 2, which equals 16.

Conclusion

Python operators are essential tools that empower you to work with data, make decisions, and control the flow of your programs. Whether you’re performing basic arithmetic calculations, comparing values, making logical decisions, or manipulating bits, Python’s operators provide the necessary functionality.

Understanding the different types of operators and their precedence is a key step in becoming proficient in Python programming. With these foundational skills, you’ll be well-equipped to tackle a wide range of tasks and create powerful and dynamic Python applications.

Want more exciting Python Articles?

https://codingstreets.com/

--

--

wordpediax

I like to read and write articles on tech topics like Python, ML, AI. Beside this, I like to thread life into words and connect myself with nature.