Python Operators

Operators are symbols in Python that carry out a specific computation, or operation. In python, there are various types of operators, and they are used to write the expression or any condition. Python Operators

Types of Operators:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison / Relational Operators
  • Logical Operators
  • Identity Operators
  • Membership Operators
  • Bitwise Operators

Arithmetic Operators:

The arithmetic operators in Python are used to perform math operations, such as addition, subtraction, multiplication, and division. Python also offers a number of libraries that enable you to perform more complex math tasks.

Here is a list of the arithmetic operators in Python:

OperatorDescriptionSyntax
+Addition: adds two operandsx + y
Subtraction: subtracts two operandsx – y
*Multiplication: multiplies two operandsx * y
/Division (float): divides the first operand by the secondx / y
//Division (floor): divides the first operand by the secondx // y
%Modulus: returns the remainder when the first operand is divided by the secondx % y
**Power: Returns first raised to power secondx ** y

Example:

a = 6
b = 4

c = a + b        #addition
print(c) 
 

c = a - b        #Subtraction
print(c) 
 
c = a * b        #Multiplication
print(c) 
 
c = a / b        #Division
print(c)  
 
c = a % b        #Modulus
print(c)  
 
 

a = 2 
b = 3 
c = a**b         #Power
print(c)  
 

a = 10 
b = 5
c = a//b         #Division floor
print(c) 
10
2
24
1.5
2
8
2

Assignment Operators:

Assignment operators are operators that are used to assign a value to a variable. The most basic assignment operator is the equal sign =, which is used to assign a value to a variable.

Here is a list of the assignment operators in Python:

OperatorDescriptionSyntax
=Assign value of right side of expression to left side operand x = y + z
+=Add AND: Add right-side operand with left side operand and then assign to left operanda+=b     a=a+b
-=Subtract AND: Subtract right operand from left operand and then assign to left operanda-=b     a=a-b
*=Multiply AND: Multiply right operand with left operand and then assign to left operanda*=b     a=a*b
/=Divide AND: Divide left operand with right operand and then assign to left operanda/=b     a=a/b
%=Modulus AND: Takes modulus using left and right operands and assign the result to left operanda%=b     a=a%b
//=Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operanda//=b     a=a//b
**=Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operanda**=b     a=a**b
&=Performs Bitwise AND on operands and assign value to left operanda&=b     a=a&b
|=Performs Bitwise OR on operands and assign value to left operanda|=b     a=a|b
^=Performs Bitwise xOR on operands and assign value to left operanda^=b     a=a^b
>>=Performs Bitwise right shift on operands and assign value to left operanda>>=b     a=a>>b
<<=Performs Bitwise left shift on operands and assign value to left operanda <<= b     a= a << b

Example:

a = 5

# Assign value
b = a
print(b)

# Add and assign value
b += a
print(b)

# Subtract and assign value
b -= a
print(b)

# multiply and assign
b *= a
print(b)
5
10
5
25

Comparison / Relational Operators:

Comparison operators are used to comparing the values of two operands and determine whether a certain condition is met.

Here is a list of the Comparison operators in Python:

OperatorDescriptionSyntax
>Greater than: True if the left operand is greater than the rightx > y
<Less than: True if the left operand is less than the rightx < y
==Equal to: True if both operands are equalx == y
!=Not equal to – True if operands are not equalx != y
>=Greater than or equal to True if the left operand is greater than or equal to the rightx >= y
<=Less than or equal to True if the left operand is less than or equal to the rightx <= y
is x is the same as yx is y
is notx is not the same as yx is not y

Example:

a = 1
b = 2

print(a > b)        # a > b is False

print(a < b)        # a < b is True
               
print(a == b)       # a == b is False

print(a != b)       # a != b is True
  
print(a >= b)       # a >= b is False

print(a <= b)       # a <= b is True
False
True
False
True
False
True

Logical Operators:

Logical operators are used on conditional statements (either True or False). There are three logical operators.

Here is a list of the Logical operators in Python:

OperatorDescriptionSyntax
andLogical AND: True if both the operands are truex and y
orLogical OR: True if either of the operands is true x or y
notLogical NOT: True if the operand is false not x

Identity Operators:

Identity operators are is and is not, which are used to determine if two objects are the same object in memory. These operators compare the memory addresses of the objects, rather than their values.

Here is a list of the Identity operators in Python:

OperatorDescription
isTrue if the operands are identical
is notTrue if the operands are not identical

Example of is Operator:

x = [1, 2, 3]
y = [1, 2, 3]
z = x

print(x is y)    # False, because x and y are different objects in memory
print(x is z)    # True, because x and z refer to the same object in memory
False
True

Example of is not Operator:

x = [1, 2, 3]
y = [1, 2, 3]
z = x

print(x is not y)    # True, because x and y are different objects in memory
print(x is not z)    # False, because x and z refer to the same object in memory
True
False

Bitwise Operators:

Bitwise operators are used to perform bitwise calculations on integers. The integers are first converted into binary and then operations are performed on bit by bit, hence the name bitwise operators. Then the result is returned in decimal format.

Here is a list of the Bitwise operators in Python:

OperatorDescriptionSyntax
&Bitwise ANDx & y
|Bitwise ORx | y
~Bitwise NOT~x
^Bitwise XORx ^ y
>>Bitwise right shiftx>>
<<Bitwise left shiftx<<

Membership Operators:

Membership operator in Python is an operator that checks the membership of a value or variable. Membership operator is used to check whether a value, variable, or element exists in the given sequences like string, tuple, list, etc., or not.

Here is a list of the Membership operators in Python:

OperatorDescription
inTrue if value is found in the sequence
not inTrue if value is not found in the sequence

Python Operators
  1. Operators are symbols in Python that carry out a specific computation, or operation.
  2. Arithmetic operators in Python are used to perform math operations, such as addition, subtraction, multiplication, and division. 
  3. Assignment operators are operators that are used to assign a value to a variable.
  4. Logical operators are used on conditional statements (either True or False). There are three logical operators.
  5. Identity operators are is and is not, which are used to determine if two objects are the same object in memory.
  6. Bitwise operators are used to perform bitwise calculations on integers.
  7. Membership operator in Python is an operator that checks the membership of a value or variable.