Budding Software Engineer
Operators are used to doing operations on any given data stored inside variables. In Python, we learn 7 types of operators - namely :
1. 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!
Addition 9 Subtraction 8
Muliplication 314.0 Division 2.0
Floor division — // : rounds off the result to the nearest whole number.
Modulus — % : produces the remainder of the numbers.
Floor Division 3 Modulus 1
Exponentiation — **: produces the power of given numbers
Exponentiation 0.025517964452291125 Exponentiation 37.78343433288728
When it comes to binary numbers, bitwise operators are the choice.
Bitwise operators are used to perform operations on binary numbers.
AND, OR, XOR operators
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 gets converted into an integer, each time bitwise operation is performed.
NOT ~ operator inverts all the bits. In Python, the number gets converted into an inverted signed number.
Right shift 277 Left shift 4444
So, basically, comparison operators are used to compare two values that are numbers.
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
!= Not Equal comparison operator
Alright, I’m sure that you are aware of 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 comparison operator
False True False
< Less than comparison operator
True True False
>= Greater than or equal to comparison operator
False True True
<= Less than or equal to comparison operator
False True True
Here’s the answer for 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, we are back to square one. That is, we can compare the values as numbers and return true or false.
Assignment operators are used to assign values to variables.
That is to store values in variables we use = assignment operator.
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 is the Code and Output
9 6 18 6.0
64 1 0
0 3 24
So, while coding make sure you practice to use print statements after each operation.
Logical operators are used to combine 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 returns the boolean value True only if both statements are true.
or operator returns the boolean value True if any statement is true.
not operator acts as a unary operator which returns the boolean value True the statement is true and vice versa.
Identity operators are used to checking whether the objects are the same or not.
True False True
True False True
Membership operators are used to testing whether a sequence with the specified value is present in the given object.
Let’s go code through each of them.
not in operator
Code along and have fun ;)
Create your free account to unlock your custom reading experience.