Qayyum Siddiqui logo
Qayyum Siddiqui
python

python operators

python operators
0 views
4 min read
#python

Operators in Python are special symbols that perform operations on operands (values and variables). Here's a comprehensive overview of the main types of operators in Python:

1. Arithmetic Operators

These are used for basic mathematical operations:

  • Addition: +

    a = 1 + 2  # Result: 3
  • Subtraction: -

    b = 5 - 3  # Result: 2
  • Multiplication: *

    c = 4 * 6  # Result: 24
  • Division: / (returns a float)

    d = 10 / 3  # Result: 3.3333333333333335
  • Floor Division: // (returns the largest possible integer not greater than the exact result)

    e = 10 // 3  # Result: 3
  • Modulus (Remainder): %

    f = 10 % 3  # Result: 1
  • Exponentiation: **

    g = 2 ** 3  # Result: 8

2. Comparison (Relational) Operators

These operators compare values and return True or False:

  • Equal to: ==

    3 == 3  # True
  • Not Equal to: !=

    3 != 4  # True
  • Greater than: >

    5 > 3  # True
  • Less than: <

    3 < 5  # True
  • Greater than or equal to: >=

    5 >= 5  # True
  • Less than or equal to: <=

    3 <= 3  # True

3. Assignment Operators

Used to assign values to variables:

  • Assignment: =

    x = 10
  • Add and Assign: +=

    x += 5  # Equivalent to x = x + 5
  • Subtract and Assign: -=

    x -= 3  # Equivalent to x = x - 3
  • Multiply and Assign: *=

    x *= 2  # Equivalent to x = x * 2
  • Divide and Assign: /=

    x /= 2  # Equivalent to x = x / 2
  • Modulus and Assign: %=

    x %= 3  # Equivalent to x = x % 3
  • Exponentiation and Assign: **=

    x **= 2  # Equivalent to x = x ** 2

4. Logical Operators

These operators combine conditional statements:

  • AND: and (True if both operands are true)

    True and False  # False
  • OR: or (True if at least one operand is true)

    True or False  # True
  • NOT: not (Inverts the boolean state)

    not True  # False

5. Identity Operators

Check if two variables point to the same object in memory:

  • Is: is

    a = [1, 2]
    b = a
    a is b  # True
  • Is Not: is not

    a = [1, 2]
    b = [1, 2]
    a is not b  # True, because they are different objects in memory

6. Membership Operators

Check if a sequence is present in an object:

  • In: in

    2 in [1, 2, 3]  # True
  • Not In: not in

    4 not in [1, 2, 3]  # True

7. Bitwise Operators

Operate on binary representations of integers:

  • AND: &

    5 & 3  # Binary: 101 & 011 = 001 (Result: 1)
  • OR: |

    5 | 3  # Binary: 101 | 011 = 111 (Result: 7)
  • XOR: ^

    5 ^ 3  # Binary: 101 ^ 011 = 110 (Result: 6)
  • NOT: ~ (Unary operator, flips bits)

    ~5  # Depends on the number of bits used for representation
  • Left Shift: <<

    5 << 2  # Binary: 101 << 2 = 10100 (Result: 20)
  • Right Shift: >>

    5 >> 1  # Binary: 101 >> 1 = 10 (Result: 2)

8. Special Operators

  • Ternary Operator: Not a keyword but a shorthand for conditional expressions.
    x = True if 5 > 3 else False

Understanding these operators is crucial for performing various operations in Python, from basic arithmetic to complex logical evaluations. They form the foundation of many algorithms and control structures in programming.