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

Below is the list of Arithmetic operators:

- + -> Addition
- - -> Subtraction
- * -> Product/Multiplication
- / -> Division
- // -> Floor Division
- % -> Modulus - remainder of the division of left operand by the right.
- ** -> Exponent - left operand raised to the power of right.

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)

51

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

print(a-b)

29

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

print(a/b)

3.6363636363636362

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

print(a*b)

440

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

print(a//b)

3

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

print(a%b)

7

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

print(a**b)

print(2**3)

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)

KashishGakkar

To add blank space between two strings:

print(f_name + ' ' + l_name)

Kashish Gakkar

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

print(f_name - l_name)

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

print(f_name * l_name)

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

print(f_name*3)

KashishKashishKashish

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

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

KashishKashishKashish

GakkarGakkarGakkarGakkarGakkar

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

L = ['kashish', 10]

print(L+20)

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

l = [20,30]

print(L+l)

['kashish', 10, 20, 30]

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))

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)))

2010 1718949905520

Shortcut to achieve above:

num1 += 10

print(num1, id(num1))

2040 1718949906256

num1 -= 10

print(num1, id(num1))

2030 1718949906480

num1 *= 10

print(num1, id(num1))

20300 1718949906736

num1 /= 10

print(num1, id(num1))

2030.0 1718949904848

num1 //= 10

print(num1, id(num1))

20.0 1718948944912

num1 %= 10

print(num1, id(num1))

0.0 1718949906640

num1 = 2

num1 **= 10

print(num1, id(num1))

1024 2150686457072

Below is the list of Comparison operators:

- > - greater than
- < - less than
- == - equal to
- <= - Less than equal to
- = - greater than equal to
- != - not equal to

**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

False

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

num1 > 2

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

True

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

num1 <= 10

True

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

num1 >= 10

True

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

num1 != 10

False

Below is the list of Logical operators:

- AND - All the conditions must be met.
- OR - Any one of the conditions must be met.
- NOT - Makes the statement negative/opposite.

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

num1 = 10

num2 = 20

num1 <= 10 and num2 >= 20

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

False

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

num1 < 10 or num1 <= 20

True

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

not (num1 < 10 or num1 <= 20)

False

NOT operator is used to make the statement negative.

not (num1 == 10)

False

Below is the list of Identity operators:

- is -> Returns true if both variables are the same object.
- is not -> Returns true if both variables are not the same object.

**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

True

The output is true because the memory location is same.

num1 is num2

True

Look at the IDS, both are same.

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

140729197434144 140729197434144

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

num1 = 500

num2 = 500

num1 == num2

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

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))

2150687232400 2150687232496

The output is false because .

num1 = 200

num2 = 200

num1 is not num2

False

The output is false because .

num1 = 500

num2 = 500

num1 is not num2

True

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

num1 is not num2

True

Below is the list of Membership operators:

- in -> Returns True if a sequence with the specified value is present in the object.
- not in -> Returns True if a sequence with the specified value is not present in the object.

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

True

Similarly, to check if 56 is present in list:

56 in l

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

True

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

89 not in l

False

Bitwise operators are used to compare (binary) numbers:

Below is the list of Bitwise operators:

- & AND - Sets each bit to 1 if both bits are 1.
- | OR - Sets each bit to 1 if one of two bits is 1.
- ^ XOR - Sets each bit to 1 if only one of two bits is 1.
- ~ NOT - Inverts all the bits.
- << Zero - fill left shift - Shift left by pushing zeros in from the right and let the leftmost bits fall off.
- >> Signed right shift - Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off.

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)

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