Python Operators: A Comprehensive Guide

Python Operators - Introduction

 

In Python, operators are special symbols or keywords that perform operations on values or variables. These operations range from arithmetic calculations to comparisons, logical operations, and more. Understanding how these operators work is fundamental to writing effective Python code.

1. Arithmetic Operators

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

  • + : Addition
    • Adds two operands.
    • Example: 3 + 2 results in 5.
  • - : Subtraction
    • Subtracts the second operand from the first.
    • Example: 5 - 2 results in 3.
  • * : Multiplication
    • Multiplies two operands.
    • Example: 3 * 2 results in 6.
  • / : Division
    • Divides the first operand by the second. Returns a float.
    • Example: 5 / 2 results in 2.5.
  • % : Modulus
    • Returns the remainder of the division.
    • Example: 5 % 2 results in 1.
  • ** : Exponentiation
    • Raises the first operand to the power of the second.
    • Example: 2 ** 3 results in 8.
  • // : Floor Division
    • Divides the first operand by the second and returns the largest integer less than or equal to the result.
    • Example: 5 // 2 results in 2.

2. Comparison Operators

Comparison operators are used to compare two values. They return either True or False.

  • == : Equal
    • Checks if two operands are equal.
    • Example: 5 == 5 results in True.
  • != : Not Equal
    • Checks if two operands are not equal.
    • Example: 5 != 2 results in True.
  • > : Greater Than
    • Checks if the left operand is greater than the right.
    • Example: 5 > 2 results in True.
  • < : Less Than
    • Checks if the left operand is less than the right.
    • Example: 2 < 5 results in True.
  • >= : Greater Than or Equal To
    • Checks if the left operand is greater than or equal to the right.
    • Example: 5 >= 5 results in True.
  • <= : Less Than or Equal To
    • Checks if the left operand is less than or equal to the right.
    • Example: 2 <= 5 results in True.

3. Logical Operators

Logical operators are used to combine conditional statements.

  • and : Logical AND
    • Returns True if both statements are true.
    • Example: (5 > 2) and (3 < 4) results in True.
  • or : Logical OR
    • Returns True if at least one statement is true.
    • Example: (5 > 2) or (3 > 4) results in True.
  • not : Logical NOT
    • Reverses the result, returns False if the result is true.
    • Example: not(5 > 2) results in False.

4. Assignment Operators

Assignment operators are used to assign values to variables.

  • = : Assign
    • Assigns a value to a variable.
    • Example: x = 5 assigns 5 to x.
  • += : Add and Assign
    • Adds the right operand to the left operand and assigns the result to the left operand.
    • Example: x += 3 is equivalent to x = x + 3.
  • -= : Subtract and Assign
    • Subtracts the right operand from the left operand and assigns the result to the left operand.
    • Example: x -= 3 is equivalent to x = x - 3.
  • *= : Multiply and Assign
    • Multiplies the left operand with the right operand and assigns the result to the left operand.
    • Example: x *= 3 is equivalent to x = x * 3.
  • /= : Divide and Assign
    • Divides the left operand by the right operand and assigns the result to the left operand.
    • Example: x /= 3 is equivalent to x = x / 3.
  • %= : Modulus and Assign
    • Takes modulus using two operands and assigns the result to the left operand.
    • Example: x %= 3 is equivalent to x = x % 3.
  • **= : Exponent and Assign
    • Raises the left operand to the power of the right operand and assigns the result to the left operand.
    • Example: x **= 3 is equivalent to x = x ** 3.
  • //= : Floor Divide and Assign
    • Performs floor division on two operands and assigns the result to the left operand.
    • Example: x //= 3 is equivalent to x = x // 3.

5. Bitwise Operators

Bitwise operators work on bits and perform bit-by-bit operations.

  • & : Bitwise AND
    • Sets each bit to 1 if both bits are 1.
    • Example: 5 & 3 results in 1.
  • | : Bitwise OR
    • Sets each bit to 1 if one of two bits is 1.
    • Example: 5 | 3 results in 7.
  • ^ : Bitwise XOR
    • Sets each bit to 1 if only one of two bits is 1.
    • Example: 5 ^ 3 results in 6.
  • ~ : Bitwise NOT
    • Inverts all the bits.
    • Example: ~5 results in -6.
  • << : Bitwise Left Shift
    • Shifts bits to the left by the number of positions specified.
    • Example: 5 << 1 results in 10.
  • >> : Bitwise Right Shift
    • Shifts bits to the right by the number of positions specified.
    • Example: 5 >> 1 results in 2.

6. Membership Operators

Membership operators are used to test if a sequence contains an element.

  • in : Checks if a value is in a sequence.
    • Example: 3 in [1, 2, 3] results in True.
  • not in : Checks if a value is not in a sequence.
    • Example: 4 not in [1, 2, 3] results in True.

7. Identity Operators

Identity operators are used to compare the memory locations of two objects.

  • is : Checks if two variables point to the same object.
    • Example: x is y results in True if x and y are the same object.
  • is not : Checks if two variables do not point to the same object.
    • Example: x is not y results in True if x and y are not the same object.

8. Special Operators

  • := : Walrus Operator
    • Used for assignment expressions. It assigns values to variables as part of an expression.
    • Example: (n := len(x)) > 10 assigns len(x) to n and then compares it with 10.

Example

Python Operators

Output

 

 

 

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

wpChatIcon
wpChatIcon
Scroll to Top