Operators in Python

You can perform various operations on data using Operators in Python. Operators are special symbols which are assigned to do a particular operation like addition, subtraction etc.

Following Operators are available in Python:

  1. Arithmetic Operators
  2. Comparison Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operator
  6. Identity Operators
  7. Membership Operators

These operators are applied to the values which are known as Operands. The operands are manipulated by the operators and a result is produced.
You can apply the operators directly to the values or to the variables referring to that value.

1. Arithmetic Operators

Arithmetic Operations include basic operations like addition, subtraction, multiplication etc which are performed using arithmetic operators.

Following arithmetic operators are available in Python.

Name Symbol Operation Result
Addition + 10+3=13 Adds the operands Sum
Subtraction - 10-3=7 Subtracts the operands Difference
Multiplication * 10*3=30 Multiplies the operands Product
Division / 10/3=3.333 Divides the operands Quotient
Floor Division // 10//3=3 Divides the operands Quotient
(Whole number)
Modulus % 10%3=1 Divides the operands Remainder
Exponentiation ** 10**3=1000 Raises the power of the left operand by the given number.  Raised value

Let us create a program which will perform arithmetic calculations.

>>> a = 10 
>>> b = 3

>>> c = a+b #Addition
>>> print(c)
13

>>> c = a-b #Subtraction
>>> print(c)
7

>>> c = a*b #Multiplication
>>> print(c)
30

>>> c = a/b #Division
>>> print(c)
3.3333333333333335

>>> c = a//b #Floor Dividion
>>> print(c)
3

>>> c = a%b #Modulus
>>> print(c)
1

>>> c = a**b #Exponentiation
>>> print(c)
1000

2. Comparison Operators

Comparison operator simply compares the values and produces a result as either 'True' or 'False' i.e Boolean Value.

Comparison operators are generally used in loops or if statements, where the further execution of the program depends on a condition. This condition is created using comparison operators.

The value produced by the comparison operator can also be stored in a variable.

Following Comparison operators are available in python.

Name Symbol Operation Result
Greater than > 10 > 3 True
Greater than or equal to >= 10 >= 3 True
Less than < 10 < 3 False
Less than or equal to <=  10 <= 3 False
Equal to == 10 == 3 False
Not equal to != 10 != 3 True

Let us create a program to see how comparison operators work.

>>> a = 10
>>> b = 20

>>> c = a > b #Greater than
>>> print(c)
False

>>> c = a>=b #Greater than or equal to
>>> print(c)
False

>>> c = a < b #Less than
>>> print(c)
True
>>> c = a<= b #Less than or equal to
>>> print(c)
True
>>> c = a==b #Equal to
>>> print(c)
False
>>> c = a != b #Not equal to
>>> print(c)
True
>>>

3. Logical Operators

Logical Operators work on the comparison statements. These operators also produce the result as 'True' or 'False' depending on the result of the comparison statements.

Following logical operators are available in Python.

Name Symbol Operation Result
And and X and Y True, if both X & Y are true.
Or or X or Y True, if at least X or Y is true.
Not  not X not Y Inverts the value.
If True, then False.
If False, then True.

Let us create a program using logical operators

>>> a = 50
>>> b = 20

>>> c = (a==b) and (a>b) #and operator returns false because one condition false.
>>> print(c)
False

>>> c = (a==b) or (a>b) #or operator returns true because atleast one condition is true.
>>> print(c)
True

>>> c = not(a==b) #not operator returns true because the given condition is true.
>>> print(c)
True
>>>

4. Bitwise Operator

Operators that operate on the bits present in the data are bitwise operators. The operations performed by the arithmetic logic unit of the computer are done at bit-level.

Following Bitwise operators are available in Python:

Name Symbol Operation Result
Bitwise And  & 8 & 5 = 0 Returns 1 only if the corresponding bits are 1.
Bitwise Or | 8 | % = 13 Returns 1 if at least one of the corresponding bits is 1
Bitwise Not ~ ~8 = -9 Returns –( X + 1 ), where X is the operand
Bitwise XOR ^ 8 ^ 5 = 13 Returns 1 only if corresponding bits are not same
Left Shift << 8 << 2 = 32 Shifts the bits towards left by adding zero’s
Right Shift >> 8 >> 2 = 2 Shifts the bits towards right by removing the rightmost bits and adding zero’s to the left

Let us create a program to see the working of Bitwise operators.

>>> a = 8
>>> b = 5
>>> c = a & b #Bitwise And operator
>>> print(c)
0
>>> c = a | b #Bitwise Or operator
>>> print(c)
13
>>> c = ~a #Bitwise Not operator
>>> print(c)
-9
>>> c = a ^ b #Bitwise XOR operator
>>> print(c)
13
>>> c = a<<2 #Left Shift operator
>>>print(c)
32
>>> c = a>>2 #Right Shift operator
>>>print(c)
2
>>>
  • Bitwise AND (&) Operation:



  • Bitwise OR (|) Operation:



  • Bitwise NOT (~) Operation:



  • Bitwise XOR (^) Operation:



  • Left Shift Operation:



  • Right Shift Operation:

5. Assignment Operators

Assignment operators simply assign a value to the variable using '= operator' and the variable starts referring to the value. The variable name is always to the left of = sign and the value is at the right side.

For example:

>>> name = 'Atif'
>>> print(name)
Atif

Here, a value (string) is assigned to the variable 'name'.

  • Compound Assignment: With the compound assignment, you can write the statement
    p = p + 2​​

    as

    p+ = 2​

    This is also called as shorthand assignment because it provides a more compact way to write the code. Following operators can be used with the compound assignment.

    p += 2 #Addition
    
    p -= 2 #Subtraction
    
    p *= 2 #Multiplication
    
    p /= 2 #Division
    
    p //= 2 #Floor Division
    
    p %= 2 #Modulus
    
    p **= 2 #Exponentiation

6. Identity Operators

Identity Operator is a special operator that is used to test if the two values are identical. Being identical does not mean that the values are equal, but it means that they are stored at the same location.

The result of the identity operator is either 'True' or 'False'. Python provides 2 Identity Operators.

Symbol Operation Result
is X is Y True, if X and Y are referring to the same value. False, otherwise.
is not X is not Y True, if X and Y are not referring to the same value. False, otherwise.

Let us create a program and see how the identity operators work.

>>> a = 10
>>> b = 10
>>> c = 30

>>> a is b
True

>>> a is not b
False

>>> b is c
False

>>> b is not c
True

Explanation:

  • In the above program, 3 variables are created. Variable a and b refer to the same value i.e 10. Therefore, when is operator is applied to a and b, it returns 'True' & is not operator returns 'False'.
  • Similarly, variable b and c refer to different variables. Therefore, when is operator is applied to b and c, it returns 'False' and is not operator returns 'True'.

7. Membership Operators

Membership operators look for a given character (or characters) in a sequence of characters. It checks the presence of the character.

This operator may have one of the two results, either 'True' or 'False'. Membership operators can be applied to strings, tuples, lists, sets and dictionaries. Python has 2 membership operators.

Symbol Operation Result
in  a in Y True, if character a is present in variable Y. Otherwise, False
not in a not in Y True, if a is not present in variable Y. Otherwise, False.

Let us see an example.

>>> num = [10, 20, 30, 40]

>>> 30 in num
True
>>> 80 in num
False
>>> 80 not in list
True

>>> name = "My name is Johnny"

>>> "Johnny" in name
True
>>> "Harry" in name
False 
>>> "Harry" not in name
True

Explanation:

  • In the above program, we have created a list named num. It has four items.
  • In the next statement, we check if the value '30' is present in the list num or not using in operator. Since it is present in the list, the output is 'True'.
  • Again, we look for another value '80' in the list using in operator. Since it is not present in the list, it returns 'False'.
  • In the next statement, the not in operator is used with the value '80' which returns 'True'.
  • Similarly, the membership operator is applied to a string.