Python Operator

Arithmetic Operators:

  • Arithmetic Operators: Used for performing mathematical calculations.
    • Addition: +
    • Subtraction: -
    • Multiplication: *
    • Division: /
    • Floor Division (returns the integer quotient): //
    • Modulus (returns the remainder of division): %
    • Exponentiation: **

Here's an overview of some common types of operators with examples:

Example:

                                  
                                    a = 10
b = 5

# Addition
sum_result = a + b  # 15

# Subtraction
difference = a - b  # 5

# Multiplication
product = a * b  # 50

# Division
division_result = a / b  # 2.0 (float division)

# Modulus (remainder)
remainder = a % b  # 0

# Exponentiation
power = a ** b  # 100000                          
                                  
                                

Comparison Operators:

  • Comparison Operators: Used to compare values and determine the relationship between them.
    • Equal to: ==
    • Not equal to: !=
    • Greater than: >
    • Less than: <
    • Greater than or equal to: >=
    • Less than or equal to: <=

Here's are some examples of Comparison Operators:

Example:

                                  
                                    x = 10
y = 20

# Equal to
is_equal = (x == y)  # False

# Not equal to
not_equal = (x != y)  # True

# Greater than
greater_than = (x > y)  # False

# Less than
less_than = (x < y)  # True

# Greater than or equal to
greater_equal = (x >= y)  # False

# Less than or equal to
less_equal = (x <= y)  # True
                                  
                                

Logical Operators:

  • Logical Operators: Used to perform logical operations on boolean values.
    • Logical AND: and
    • Logical OR: or
    • Logical NOT: not

Here's are some examples of Logical Operators:

Example:

                                  
                                    p = True
q = False

# Logical AND
logical_and = p and q  # False

# Logical OR
logical_or = p or q  # True

# Logical NOT
logical_not = not p  # False
                                  
                                

Assignment Operators:

  • Assignment Operators: Used to assign values to variables.
    • Assign: =
    • Add and assign: +=
    • Subtract and assign: -=
    • Multiply and assign: *=
    • Divide and assign: /=
    • Modulus and assign: %=
    • Exponentiation and assign: **=
    • Floor division and assign: //=

Here's are some examples of Assignment Operators:

Example:

                                  
                                    x = 5

# Assign
x = 10

# Add and Assign
x += 3  # Equivalent to x = x + 3

# Subtract and Assign
x -= 2  # Equivalent to x = x - 2
                                  
                                

Identity Operators:

  • Identity Operators: Used to compare the memory addresses of two objects.
    • is: Returns True if both operands refer to the same object.
    • is not: Returns True if both operands refer to different objects.

Here's are some examples of Identity Operators:

Example:

                                  
                                    a = [1, 2, 3]
b = [1, 2, 3]

# Identity (checks if two objects reference the same memory location)
is_same_object = (a is b)  # False

# Not Identity
is_not_same_object = (a is not b)  # True
                                  
                                

Membership Operators:

  • Membership Operators: Used to test whether a value is a member of a sequence (e.g., list, tuple, string).
    • in: Returns True if the value is found in the sequence.
    • not in: Returns True if the value is not found in the sequence.

Here's are some examples of Membership Operators:

Example:

                                  
                                    fruits = ["apple", "banana", "cherry"]

# In (checks if a value is present in a sequence)
is_in_list = ("banana" in fruits)  # True

# Not In
is_not_in_list = ("orange" not in fruits)  # True

                                  
                                

They are just a few of the various operators, to be had in Python. Operators are an quintessential part of sporting out all the operations and comparisons for your code. Understanding the way to use them effectively will will let you write compact and clear Python code.