PYTHON

Operators in Python


Operators are special symbols in Python that perform specific operations on one or more operands. Python supports a variety of operators, which are categorized as follows:

  • Arithmetic Operators
  • Comparison (Relational) Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators

 

Arithmetic Operators

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

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
//Floor Divisiona // b
%Modulus (remainder of division)a % b
**Exponentiation (power)a ** b

Examples:

a = 10
b = 3
print(a + b)   # Output: 13
print(a - b)   # Output: 7
print(a * b)   # Output: 30
print(a / b)   # Output: 3.3333333333333335
print(a // b)  # Output: 3
print(a % b)   # Output: 1
print(a ** b)  # Output: 1000
 

 

Comparison (Relational) Operators

Comparison operators compare the values of two operands and return a boolean value (True or False).

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Examples:

a = 10
b = 3
print(a == b)  # Output: False
print(a != b)  # Output: True
print(a > b)   # Output: True
print(a < b)   # Output: False
print(a >= b)  # Output: True
print(a <= b)  # Output: False

 

 

Logical Operators

Logical operators are used to combine conditional statements.

OperatorDescriptionExample
andLogical AND (both conditions must be true)a and b
orLogical OR (one of the conditions must be true)a or b
notLogical NOT (inverts the boolean value)not a

Examples:

a = True
b = False
print(a and b)  # Output: False
print(a or b)   # Output: True
print(not a)    # Output: False
 

 

Bitwise Operators

Bitwise operators perform bit-level operations on binary representations of integers.

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Bitwise left shifta << b
>>Bitwise right shifta >> b

Examples:

a = 10  # Binary: 1010
b = 4   # Binary: 0100
print(a & b)   # Output: 0 (Binary: 0000)
print(a | b)   # Output: 14 (Binary: 1110)
print(a ^ b)   # Output: 14 (Binary: 1110)
print(~a)      # Output: -11 (Binary: -(1010 + 1))
print(a << 2)  # Output: 40 (Binary: 101000)
print(a >> 2)  # Output: 2 (Binary: 10)

 

 

Assignment Operators

Assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Assignmenta = 5
+=Addition assignmenta += 3 (a = a + 3)
-=Subtraction assignmenta -= 3 (a = a - 3)
*=Multiplication assignmenta *= 3 (a = a * 3)
/=Division assignmenta /= 3 (a = a / 3)
//=Floor division assignmenta //= 3 (a = a // 3)
%=Modulus assignmenta %= 3 (a = a % 3)
**=Exponentiation assignmenta **= 3 (a = a ** 3)
&=Bitwise AND assignmenta &= 3 (a = a & 3)
`=`Bitwise OR assignment
^=Bitwise XOR assignmenta ^= 3 (a = a ^ 3)
<<=Bitwise left shift assignmenta <<= 3 (a = a << 3)
>>=Bitwise right shift assignmenta >>= 3 (a = a >> 3)

Examples:

a = 10
a += 3   # Equivalent to: a = a + 3
print(a) # Output: 13
a -= 3   # Equivalent to: a = a - 3
print(a) # Output: 10
a *= 3   # Equivalent to: a = a * 3
print(a) # Output: 30
a /= 3   # Equivalent to: a = a / 3
print(a) # Output: 10.0
a //= 3  # Equivalent to: a = a // 3
print(a) # Output: 3.0
a %= 3   # Equivalent to: a = a % 3
print(a) # Output: 0.0
a **= 3  # Equivalent to: a = a ** 3
print(a) # Output: 0.0
a = 10
a &= 3   # Equivalent to: a = a & 3
print(a) # Output: 2
a |= 3   # Equivalent to: a = a | 3
print(a) # Output: 3
a ^= 3   # Equivalent to: a = a ^ 3
print(a) # Output: 0
a <<= 3  # Equivalent to: a = a << 3
print(a) # Output: 0
a >>= 3  # Equivalent to: a = a >> 3
print(a) # Output: 0

 


PYTHON