Python Operators

In simple terms, a Python operator is a symbol that performs an operation on one or more values or variables. For example, the addition operator + adds two numbers together, the subtraction operator - subtracts one number from another, and so on. Python provides various types of operators such as arithmetic operators, comparison operators, logical operators, assignment operators, etc., each serving a specific purpose in manipulating data or controlling program flow.

Types of operators.

1. Arithmetic Operators

2. Assignment Operators

3. Comparison Operators

4. Logical Operators

5. Bitwise Operators

6. Special Operators.

1. Arithmetic Operators:

Arithmetic operators are used to perform mathematical operations on variables or values.

  • Addition (+): Adds two operands. Example: 7 + 7 = 14
  • Subtraction (-): Subtracts the right operand from the left operand. Example: 7 - 4 = 3
  • Multiplication (*): Multiplies two operands. Example: 3 * 4 = 12
  • Division (/): Divides the left operand by the right operand. Example: 4 / 2 = 2
  • Modulo (%): Returns the remainder when the left operand is divided by the right operand. Example: 5 % 2 = 1
  • Floor Division (//): Returns the floor value of the division. Example: 10 // 3 = 3
  • Power (**): Raises the left operand to the power of the right operand. Example: 4 ** 2 = 16

2. Assignment Operators:

Assignment operators are used to assign values to variables.

  • Assignment Operator (=``): Assigns the value of the right operand to the left operand. Example: a = 7`
  • Addition Assignment (+=): Adds the value of the right operand to the variable and assigns the result to the variable. Example: a += 1 is equivalent to a = a + 1
  • Subtraction Assignment (-=): Subtracts the value of the right operand from the variable and assigns the result to the variable. Example: a -= 1 is equivalent to a = a - 1
  • Multiplication Assignment (*=): Multiplies the variable by the value of the right operand and assigns the result to the variable. Example: a *= 1 is equivalent to a = a * 1
  • Division Assignment (/=): Divides the variable by the value of the right operand and assigns the result to the variable. Example: a /= 1 is equivalent to a = a / 1
  • Modulo Assignment (%=): Computes the remainder of the variable divided by the value of the right operand and assigns the result to the variable. Example: a %= 5 is equivalent to a = a % 5
  • Power Assignment (**=): Raises the variable to the power of the value of the right operand and assigns the result to the variable. Example: a **= 10 is equivalent to a = a ** 10
  • Floor Assignment (//=): Performs floor division on the variable by the value of the right operand and assigns the result to the variable. Example: a //= 2 is equivalent to a = a // 2

3. Comparison Operators:

Comparison operators are used to compare two values or variables.

  • Equal to (==): Returns True if the operands are equal.
  • Not equal to (!=): Returns True if the operands are not equal.
  • Greater than (>): Returns True if the left operand is greater than the right operand.
  • Less than (<): Returns True if the left operand is less than the right operand.
  • Greater than or equal to (>=): Returns True if the left operand is greater than or equal to the right operand.
  • Less than or equal to (<=): Returns True if the left operand is less than or equal to the right operand.

4. Logical Operators:

Logical operators are used to combine conditional statements. They include and, or, and not.

Example:

# Logical AND
x = 5
print(x > 3 and x < 10) # Output: True, both conditions are true

# Logical OR
y = 7
print(y > 10 or y % 2 == 0) # Output: True, one condition is true

# Logical NOT
z = True
print(not z) # Output: False, negating True gives False

5. Bitwise Operators:

Bitwise operators perform operations on individual bits of operands. They include & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), and >> (right shift).

Example:

# Bitwise AND
a = 10 # 1010
b = 6 # 0110
print(a & b) # Output: 2 (0010)

# Bitwise OR
print(a | b) # Output: 14 (1110)

# Bitwise XOR
print(a ^ b) # Output: 12 (1100)

# Bitwise NOT
c = 5 # 0101
print(~c) # Output: -6 (-0110)

# Left Shift
d = 4 # 0100
print(d << 1) # Output: 8 (1000)

# Right Shift
e = 8 # 1000
print(e >> 2) # Output: 2 (0010)

6. Special Operators:

Special operators in Python include the membership operators (in and not in) and identity operators (is and is not).

Example:

# Membership Operators
my_list = [1, 2, 3, 4, 5]
print(3 in my_list) # Output: True
print(6 not in my_list) # Output: True

# Identity Operators
x = 10
y = 10
z = 20
print(x is y) # Output: True, x and y reference the same object
print(x is not z) # Output: True, x and z reference different objects

Leave a comment