Skip to main content

Command Palette

Search for a command to run...

Operator in Operation: The Critical Surgery of Python

Published
4 min read

Here We will do a surgical operation on operators and try to go deeper inside the operators.We will have fun to do it.Operators are sets of symbols that can operate specific operations.Operation on operators are the backbone for doing any task.So it's really necessary doing any task in Programming language.

There are 7 types of Operation using Operator in Python. So let's do a surgery of each operation one by one.

Arithmetic Operations

Let’s have two variables. Now, I am going to do some arithmetic operations on these variables.

x = 20
y = 10

## Perform the arithmetic operations
print(x + y) # Addition
print(x - y) # Subtraction
print(x * y) # Multiplication
print(x / y) # Division
print(x // y) # Floor Division
print(x % y) # Modulus (remainder)
print(x ** y) # Exponentiation

Output:

35

15

250

2.5

2

5

95367431640625

Comparison Operators

You can get idea of operation in it’s name.So comparison operator compare two variable.Let’s understand with an example.

x = 5
y = 10

## Perform comparison operator
print(x == y) # Equal to
print(x != y) # Not Equal to
print(x > y)  # Greater than
print(x < y)  # Less than
print(x >= y) # Greater than or equal to
print(x <= y) # Less than or equal to

Output:

False True False True False True

In Comparison Operation you will always get a boolean value either true or false.

Logical Operators

It’s also known as Boolean Operators.It’s also return true or false as value.

x = 5
y = 10
z = 15

## Perform logical operators
print(x < y and y < z)
print(x < y or x > z)
print(not(x < y))

Output:

True True False

Bitwise Operators

It operates directly on binary values 1 and 0.It operates on individual bits of a numbers.

x = 10
y = 6

## Perform Bitwise Operations
print(x & y) # AND Operation
print(x | y) # OR Operation
print(x ^ y) # XOR Operation

## Task
print(~x) # NOT Operation


print(x << 1) # Left Shift Operation
print(x >> 1) # Right Shift Operation

Output: 2 14 12 -11 20 5

Let’s discuss how it’s internally working:
a) Bitwise And(&)

Binary Value of 10 is 1010 and for 6 it is 0110.

1010  (10)
0110  (6)
-------
0010  => 2 (in decimal)

b) Bitwise Or(|)

1010  (10)
0110  (6)
-------
 1110  => 14 (in decimal)

c) Bitwise XOR Operation(^)

  1010  (10)
  0110  (6)
-------
  1100  => 12 (in decimal)

d) BItwise NOT OPeration(~)

x =  10 =>  00000000 00000000 00000000 00001010
~x       => 11111111 11111111 11111111 11110101 => -11 (in decimal)

e) Left Shift («)

Before:        1   0   1   0
               B3  B2  B1  B0

Shifted:   →   1 → 0 → 1 → 0 → 0 
           B4  B3  B2  B1  B0

\=> 20 (in decimal)

f) Right Shift(»)

Before:        1   0   1   0
               B3  B2  B1  B0

Shifted:       0 ← 1 ← 0 ← 1
               B3  B2  B1  B0

\=> 5 (in decimal)

Assignment Operators

This operator is usually used for assigning any operation to variables


x = 20
y = 3

## Perform assignment operators
x += y # x = x + y
print(x)

x -= y # x = x - y
print(x)

x *= y
print(x)

x /= y
print(x)

x %= y
print(x)

Output:

23 20 60 20.0 2.0

Membership Operators

It is used to check if a variable is present in an object or not.It returns true or false value.

print('a' in 'Trina')

Output: True

Identity Operators

If two variable is present in same memory location it returns true else will return false.


x = 10
y = 10

## Perform identity operators
print(x is y)
print(x is not y)

Output: True False

Now question is how you can find memory of a variable and how you can ensure your mind this code is working as expected.So in Python we can check memory with id.

print(id(x))
print(id(y))

check memory location is same if variable is same.

This is it.Operation is completed successfully.These are the operators in Python that rule over coding.And now after this operation on operators now you can get a healthy position in the journey of Python.