Operators are used to doing some operations on any given data stored inside variables.

In python, we 7 types of operators, namely :
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Identity operators
6. Membership operators
7. Bitwise operators

Arithmetic operators

Arithmetic operators make mathematical operations possible on operands in a program.

I know! I know! This is a basic concept! But let’s make it fun!

Starting with addition and subtraction

Output:

Addition 9
Subtraction 8

Now, Multiplication and Division

Output:

Muliplication 314.0
Division 2.0

Special arithmetic operators

  • Floor division — //: rounds off the result to the nearest whole number.
  • Modulus — %: produces the remainder of the numbers
  • Output:
Floor Division 3
Modulus 1
  • Exponentiation — **: produces the power of given numbers
  • Output:
Exponentiation 0.025517964452291125
Exponentiation 37.78343433288728

Bitwise Operators in python

When it comes to binary numbers, bitwise operators are the choice.

Bitwise operators are used to performing operations on binary numbers.

AND, OR, XOR operators

  • AND & operator sets each bit to 1 if both bits are 1.
  • OR | operator sets each bit to 1 if one of two bits is 1.
  • XOR ^ operator sets each bit to 1 if only one of two bits is 1.
  • Output:
AND 82
OR 2039
XOR 1957

Ha Ha, surprised about the outputs?!
The outputs are a result of the binary numbers a and b which get converted into an integer, each time a bitwise operation is performed.

NOT operator

  • NOT ~ operator inverts all the bits.
  • In python, the number gets converted into an inverted signed number.
  • Output:
NOT -11

Shift operators

  • left shift << operator shifts left by pushing zeros in from the right and letting the leftmost bits fall off.
  • right shift >> operator shifts right by pushing copies of the leftmost bit in from the left and letting the rightmost bits fall off.
  • Output:
Right shift 277
Left shift 4444

Comparison Operators in python

So, basically, comparison operators are used to compare two values.

If we level up to be geeky, comparison operators can also be used to compare other data types.

Now, let’s start with equality checks and I hope you like spider-man movies.

== Equal comparison operator

Output:

False
True

!= Not Equal comparison operator

Output:

True
False

Alright, I’m sure you know how to use other operators to compare two number values, right?
OK, now’s the time to level up to be geeky.

For the rest of the operators let us compare the letters from the Alphabet.
Wait, what?!
You heard me right!

Let me explain it at the end of this post.

> Greater than the comparison operator

Output:

False
True
False

< Less than comparison operator

Output:

True
True
False

>= Greater than or equal to the comparison operator

Output:

False
True
True

<= Less than or equal to the comparison operator

Output:

False
True
True

Here’s the answer to the above craziness.

When we compare two letters (or characters), it gets converted into ASCII code. You can check the link where the table contains ‘DEC’ (Decimal values) for the characters from Alphabet.

Now that the characters are converted into ASCII code, which is nothing but numbers and we are back to square one.
We can compare the values as numbers and return True or false.

Assignment Operators in python

Assignment operators are used to assigning values to variables.

That is to store values in variables we use = assignment operator.

Output:

3.14

OK, now comes the real fun.
Have ever been tired to use x = x + 5, where we type the variable x twice?
There’s actually a shortcut for this called Augmented assignment operators.

Augmented assignment operators can be used as a replacement as follows:

x += 3     --->    x = x + 3    
x -= 3 ---> x = x - 3
x *= 3 ---> x = x * 3
x /= 3 ---> x = x / 3
x %= 3 ---> x = x % 3
x //= 3 ---> x = x // 3
x **= 3 ---> x = x ** 3
x &= 3 ---> x = x & 3
x |= 3 ---> x = x | 3
x ^= 3 ---> x = x ^ 3
x >>= 3 ---> x = x >> 3
x <<= 3 ---> x = x << 3

Here’s the Code and Output

9
6
18
6.0
64
1
0
2
3
0
3
24

Quick Note: The code snippets reuse the same variable to assign different arithmetic operations / bitwise operations/shift operations.

So, while coding makes sure you practice using print statements after each operation.

Logical Operators in python

Logical operators are used to combining more than two conditional statements.

These operators are very useful for writing logical conditions in control flow statements of a programming language.

Let’s code them one by one.

and operator

  • and operator return the boolean value True only if both statements are true.
  • Output:
True

or operator

  • or operator returns the boolean value True if any statement is true.
  • Output:
True

not operator

  • not operator acts as a unary operator which returns the boolean value True the statement is true and vice versa.
  • Output:
False

Identity Operators in python

Identity operators are used to check whether the objects are the same or not.

Fact: In python, all data types are implemented as an object.

is operator

Output:

True
False
True

is not operator

Output:

True
False
True

Membership Operators in python

Membership operators are used to testing if a sequence with the specified value is present in the given object.

Fact: In python, all data types are implemented as an object.

Let’s go code through each of them.

in operator

Output:

True

not in operator

Output:

True

Who Am I?

I’m Aswin Barath, a Software Engineering Nerd who loves building Web Applications, now sharing my knowledge through Blogging and Community events during the busy time of my freelancing work life.

Here’s the link to all of my craziness categorized by platforms under one place: https://linktr.ee/AswinBarath

For my blog lovers, read more Bite-sized Medium articles from our FB Page on Software Concepts, New & Trending Technologies. We provide a “Friend Link” 🤝 that guarantees anyone free access to our story, even if they’ve read all of their complimentary stories for this month 😍

Join me to learn Python!

Checkout the TechSoftware Feature Page where my mission is to share my knowledge on Python: https://medium.com/techsoftware/python/home

Learn what I know about Python from any of my favourite knowledge sources:

Keep Learning

Now, I guess this is where I say GoodBye👋.
But, hey it’s time for you to start learning with your newfound Knowledge(Power)👨‍💻👩‍💻 .
Good Job that you made it this far 👏👏
Thank you so much for reading my Blog🙂.

Categorized in:

Python Programming,

Last Update: 28 December 2023