Skip to main content

Operators in python with example

 In Python, operators are symbols or special characters that perform various operations on operands (variables, values). Python supports a wide range of operators, including arithmetic, comparison, logical, assignment, bitwise, and more. Let's go through some of the commonly used operators with examples:

1. Arithmetic Operators:

   - Addition (+): Adds two operands.
   ```python
   a = 5
   b = 3
   result = a + b
   print(result)  # Output: 8
   ```

   - Subtraction (-): Subtracts the right operand from the left operand.
   ```python
   a = 5
   b = 3
   result = a - b
   print(result)  # Output: 2
   ```

   - Multiplication (*): Multiplies two operands.
   ```python
   a = 5
   b = 3
   result = a * b
   print(result)  # Output: 15
   ```

   - Division (/): Divides the left operand by the right operand, returning a floating-point result.
   ```python
   a = 5
   b = 3
   result = a / b
   print(result)  # Output: 1.6666666666666667
   ```

   - Floor Division (//): Divides the left operand by the right operand, returning the largest integer that is less than or equal to the result.
   ```python
   a = 5
   b = 3
   result = a // b
   print(result)  # Output: 1
   ```

   - Modulus (%): Returns the remainder when the left operand is divided by the right operand.
   ```python
   a = 5
   b = 3
   result = a % b
   print(result)  # Output: 2
   ```

   - Exponentiation (**): Raises the left operand to the power of the right operand.
   ```python
   a = 2
   b = 3
   result = a ** b
   print(result)  # Output: 8
   ```

2. Comparison Operators:

   - Equal to (==): Checks if two operands are equal.
   ```python
   x = 5
   y = 5
   result = x == y
   print(result)  # Output: True
   ```

   - Not equal to (!=): Checks if two operands are not equal.
   ```python
   x = 5
   y = 3
   result = x != y
   print(result)  # Output: True
   ```

   - Greater than (>): Checks if the left operand is greater than the right operand.
   ```python
   x = 5
   y = 3
   result = x > y
   print(result)  # Output: True
   ```

   - Less than (<): Checks if the left operand is less than the right operand.
   ```python
   x = 5
   y = 3
   result = x < y
   print(result)  # Output: False
   ```

   - Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
   ```python
   x = 5
   y = 3
   result = x >= y
   print(result)  # Output: True
   ```

   - Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
   ```python
   x = 5
   y = 3
   result = x <= y
   print(result)  # Output: False
   ```

3. Logical Operators:

   - Logical AND (and): Returns True if both operands are True.
   ```python
   x = True
   y = False
   result = x and y
   print(result)  # Output: False
   ```

   - Logical OR (or): Returns True if at least one operand is True.
   ```python
   x = True
   y = False
   result = x or y
   print(result)  # Output: True
   ```

   - Logical NOT (not): Returns the opposite of the operand's value.
   ```python
   x = True
   result = not x
   print(result)  # Output: False
   ```

4. Assignment Operators:

   - Assignment (=): Assigns the value of the right operand to the left operand.
   ```python
   x = 5
   ```

   - Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
   ```python
   x = 5
   x += 3  # Equivalent to x = x + 3
   print(x)  # Output: 8
   ```

   The above pattern applies to other arithmetic operators as well, such as `-=`, `*=`, `/=`, `//=`, `%=`, `**=`, etc.

5. Bitwise Operators:

   - Bitwise AND (&): Performs a bitwise AND operation on the binary representations of the operands.
   ```python
   x = 5   # Binary representation: 101
   y = 3   # Binary representation: 011
   result = x & y
   print(result)  # Output: 1 (Binary representation: 001)
   ```

   - Bitwise OR (|): Performs a bitwise OR operation on the binary representations of the operands.
   ```python
   x = 5   # Binary representation: 101
   y = 3   # Binary representation: 011
   result = x | y
   print(result)  # Output: 7 (Binary representation: 111)
   ```

   - Bitwise XOR (^): Performs a bitwise XOR operation on the binary representations of the operands.
   ```python
   x = 5   # Binary representation: 101
   y = 3   # Binary representation: 011
   result = x ^ y
   print(result)  # Output: 6 (Binary representation: 110)
   ```

   - Bitwise NOT (~): Performs a bitwise NOT operation, which inverts the bits of the operand.
   ```python
   x = 5   # Binary representation: 0000 0000 0000 0101 (assuming 32-bit)
   result = ~x
   print(result)  # Output: -6 (Binary representation: 1111 1111 1111 1010)
   ```

   - Bitwise Left Shift (<<): Shifts the bits of the left operand to the left by the number of positions specified in the right operand.
   ```python
   x = 5   # Binary representation: 0000 0000 0000 0101 (assuming 32-bit)
   result = x << 2
   print(result)  # Output: 20 (Binary representation: 0000 0000 0001 0100)
   ```

   - Bitwise Right Shift (>>): Shifts the bits of the left operand to the right by the number of positions specified in the right operand.
   ```

python
   x = 5   # Binary representation: 0000 0000 0000 0101 (assuming 32-bit)
   result = x >> 1
   print(result)  # Output: 2 (Binary representation: 0000 0000 0000 0010)
   ```

These are some of the commonly used operators in Python. Understanding how to use these operators is essential for performing various calculations, comparisons, and logical operations in Python programs.

Comments