8

OPERATORS IN PYTHON

by

The Education Machine

[support@theeducationmachine.com]

Operators in Python

Operators are special symbols in Python that carry out specific operations.
Below are different types of operators that we are going to discuss:

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


1 Arithmetic Operators

Below is the list of Arithmetic operators:

What is operand?
The value on which the operator operates.

*Operand and operator together makes an "Expression".


" + " operator is used to perform addition of numbers.

   a = 40
  b = 11
  print(a+b)


Output:
51

" - " operator is used to perform substraction of numbers.

  print(a-b)


Output:
29

" / " operator is used to perform division of numbers.

  print(a/b)


Output:
3.6363636363636362

" * " operator or Aestrisk is used to perform multiplication of numbers.

  print(a*b)


Output:
440

" // " operator or Floor Division provides the lower integer value .

  print(a//b)


Output:
3

" % " operator or Modulus is used to find out the remainder of division of 2 numbers.

  print(a%b)


Output:
7

" ** " operator or Exponentiation is used to find the power of number.

  print(a**b)
  print(2**3)


Output:
419430400000000000
8

Using Arithmetic operators with strings:

" + " is used to add or combine 2 strings.

  f_name = 'Kashish'
  l_name = 'Gakkar'
  print(f_name + l_name)


Output:
KashishGakkar

To add blank space between two strings:

  print(f_name + ' ' + l_name)


Output:
Kashish Gakkar

" - " ," / ", " // ", " ** " - All these operators doesn't work on strings.

  print(f_name - l_name)

reserved_keywords

" * " operator or aestrisk cannot multiple one string with the other.

  print(f_name * l_name)

reserved_keywords

To perform multiplication on string, the string must be multiplied with a number as shown below.

  print(f_name*3)


Output:
KashishKashishKashish

To perfrom 2 operators - " + " and " * " together.

  print((f_name*3) + "\n" + (l_name*5)))


Output:
KashishKashishKashish
GakkarGakkarGakkarGakkarGakkar

User cannot add a integer or number into the list using " + " operator.

  L = ['kashish', 10]
  print(L+20)

reserved_keywords

User can add a list into a list using " + " operator.

  l = [20,30]
  print(L+l)


Output:
['kashish', 10, 20, 30]

2 Assignment Operators

Below is the list of Assignment operators:

To perform arithmetic a variable and to store the updated value in the same variable, assignment operators come handy.
The other operators(" -=", " *= "," /= ", " //= ", " %= ", " **= ") works in similar way.


" = " or equal to operator is used to assign a value to the variable.

  num1 = 2000
  print(id(num1))


Output:
1718949904880

In below cell, the user wants to add 10 in num1 (2000) and then store the new value i.e. 2010 in the same variable num1.

  num1 = num1 + 10
  print(num1, id(num1)))


Output:
2010 1718949905520

Shortcut to achieve above:

  num1 += 10
  print(num1, id(num1))


Output:
2040 1718949906256

  num1 -= 10
  print(num1, id(num1))


Output:
2030 1718949906480

  num1 *= 10
  print(num1, id(num1))


Output:
20300 1718949906736

  num1 /= 10
  print(num1, id(num1))


Output:
2030.0 1718949904848

  num1 //= 10
  print(num1, id(num1))


Output:
20.0 1718948944912

  num1 %= 10
  print(num1, id(num1))


Output:
0.0 1718949906640

  num1 = 2
  num1 **= 10
  print(num1, id(num1))


Output:
1024 2150686457072

3 Comparison Operators

Below is the list of Comparison operators:

Comparison operators are used to compare to or more values. It will always return the boolean value i.e. True or False.


Now, the user has created a variable num1. To check num1 is less than 2 or not, " < " is used.

  num1 = 10
  num1 < 2


Output:
False

To check num1 is greater than 2 or not, " > " is used.

  num1 > 2


Output:
True

To check num1 is equal to 2 or not, " == " is used. It return true only if the two operands are equal. " = " is used only for assigning the values.

  num1 == 10


Output:
True

To check num1 is less than or equal to 2 or not, " <= " is used.

  num1 <= 10


Output:
True

To check num1 is greater than or equal to 2 or not, " >= " is used.

  num1 >= 10


Output:
True

To check num1 is not equal to 2 or not, " != " is used.

  num1 != 10


Output:
False

4 Logical Operators

Below is the list of Logical operators:


AND operator is used when user wants 2 or more conditions to met.

  num1 = 10
  num2 = 20
  num1 <= 10 and num2 >= 20


Output:
True

Num1 is equals to 10 and user wants the number to be less than. But the output is false because num1 is not less than 10..

  num1 < 10 and num1 <= 20


Output:
False

OR operator is used when user wants either of the conditions to met.

  num1 < 10 or num1 <= 20


Output:
True

OR and NOT operator can be used together as shown below:

  not (num1 < 10 or num1 <= 20)


Output:
False

NOT operator is used to make the statement negative.

  not (num1 == 10)


Output:
False

5 Identity Operators

Below is the list of Identity operators:

Identity operators never compares the value instead it checks the memory address of the variable.


The output is true as both the values are same.

  num1 = 200
  num2 = 200
  num1 == num2


Output:
True

The output is true because the memory location is same.

  num1 is num2


Output:
True

Look at the IDS, both are same.

  print(id(num1), id(num2))


Output:
140729197434144 140729197434144

Num1 is equals to num2, that is why the output is true.

  num1 = 500
  num2 = 500
  num1 == num2


Output:
True

Since, the value is greater than 256, that is why the ouput is false a their memory address will be different..

  num1 is num2


Output:
False

What does it mean?
It means that '==' compares if both the object values are identical or not. 'is' compares if both the object belongs to the same memory location

Look the IDs, the IDs are different of both the variables.

  print(id(num1), id(num2))


Output:
2150687232400 2150687232496

The output is false because .

  num1 = 200
  num2 = 200
  num1 is not num2


Output:
False

The output is false because .

  num1 = 500
  num2 = 500
  num1 is not num2


Output:
True

The output is false because the memory location of both the variables are different.

  num1 is not num2


Output:
True

6 Membership Operators

Below is the list of Membership operators:


A list(l) and a variable(num1) is created, and to check if num1 is present inside the list or not, "IN" operator can be used.

  l = [10,203,12,5,89]
  num1 = 5
  num1 in l


Output:
True

Similarly, to check if 56 is present in list:

  56 in l


Output:
False

To check if 56 is not present in list, then "NOT IN" operator is used. Here, 56 is not present in list that is why the output is true.

  56 not in l


Output:
True

Similarly, to check if 89 is not present in list:

  89 not in l


Output:
False

7 Bitwise Operators

Bitwise operators are used to compare (binary) numbers:
Below is the list of Bitwise operators:


  a = 60                                                      # 60 = 0011 1100
  b = 13                                                     # 13 = 0000 1101
  c = 0
  
  c = a & b;                                                # 12 = 0000 1100 -> BINARY AND
  print("Line 1 - Value of c is ", c)
  
  c = a | b;                                                  # 61 = 0011 1101 -> BINARY OR
  print("Line 2 - Value of c is ", c)
  
  c = a ^ b;                                                # 49 = 0011 0001 -> BINARY XOR
  print("Line 3 - Value of c is ", c)
  
  c = ~a;                                                    # -61 = 1100 0011 -> BINARY NOT | COMPLEMENT
  print("Line 4 - Value of c is ", c)
  
  c = a << 2;                                              # 240 = 1111 0000 -> LEFT SHIFT
  print("Line 5 - Value of c is ", c)
  
  c = a >> 2;                                              # 15 = 0000 1111 -> RIGHT SHIFT
  print("Line 6 - Value of c is ", c)


Output:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15