The conceptual understanding of operators in python
Hey there, let’s start from the basics of python. An operator is the smallest unit in python for performing operations on variables and values. There are various classification of operators in python. I am going to discuss their working and functions with you.
Arithmetic Operators in Python
As the word suggests, arithmetic operators are used to perform mathematical (or arithmetic) operations like addition, subtraction, etc.
3 + 5 #addition operator 7 - 2 #subtraction operator 11 * 2 #multiplication operator
>> 8 >> 5 >> 22
I have introduced you to the three operators, which you have probably learned in pre-school : + ( for addition), – ( for subtraction), * (for multiplication). They are binary operators which simply means that they require more than one operand to work on. An interesting point to know here is that python allows addition and multiplication operators to be applied on string type operands as well. For example:,"CodeSpeedy" *2 --->
"CodeSpeedyCodeSpeedy"
"String " + "Concatenate"
---> "String Concatenate"
. All the other arithmetic operators are applicable only for numeric data types (which are int, float, complex) . Let’s make it a little more complex.
12 / 3 12 // 3 12 % 3
>> 4.0 >> 4 >> 0
Don’t panic. It’s easy. Here, I just introduced you to three more operators: / , // , % . Remember that the division operator (/) always returns a float value (even if the dividend type is int). This means that if you divide 12 by 3 or you divide 12.0 by 3, it will return the output of 4.0 in both cases. Now let’s talk about the floor division operator (//). This operator simply discards the fractional part of the output value that is, it evaluates the integer part of the result. Its return type is same as that of the dividend. For instance : 14.0 // 3 gives 4.0 but 14 // 3 returns 4 as the output. As for the mod operator (%), short for modulus operator, it simply returns the remainder obtained on division. Its return type is also similar to that of the dividend.
Another operator is the pow operator (**). The examples below demonstrate its usage.
5 ** 5 5.0 ** 2 (3+5j) ** 2
>> 3125 >> 25.0 >> (-16+30j)
Relational Operators in Python
Relational operators determine the relation between two and more operands by comparing their values. Let’s understand them with the examples.
5 == 5 # equal to operator 5 != 5 # not equal to operator 5 > 4 #greater than operator 5 >= 4 #greater than equal to operator 5 < 4 #less than operator 5 <= 4 #less than equal to operator
>> True >> False >> True >> True >> False >> False
So yeah, the comparison operators return the boolean value i.e. True or False. These operators are therefore used with conditional clauses of if..else , for and while statements. Operations involving numbers are valid only if both operands are of the same type. The following rules apply:
- If one or both the operands are complex numbers, the results are undefined and thus compiler throws the TypeError.
- If the either operand is float, then the other one is also converted to float.
- Else if both operands are same, no conversion takes place.
You can use this operator to build an interesting game like a Number Guessing Game in Python
Logical Operators in Python
Consider the following logical operators and their examples:
5 and 4 #AND operator 5 and 0 5 or 4 #OR operator 5 or 0 not 0 #NOT operator not 1
>> 4 >> 0 >> 5 >> 5 >> True >> False
The AND operator returns True only when both the conditions are True. It’s working can be explained via the following pseudocode:
if (condition 1 is True) { if ( condition2 is True) { return True } else { return False } }
else { return False}
Its simply means that the compiler checks or evaluates the condition2 only when the first condition is True otherwise it automatically returns False (without evaluating the condition2). In the above example (line 1) , 5 i.e. condition1 is True. It then checks 4 which is also True. Thus it returns the later number.
The OR operator returns True if both or any one of the conditions is True. It’s working can be explained via the following pseudo-code:
if (conditio1 is True) { return True }
else{ if ( condition2 is True) { return True } else { return False } }
This means that the control checks the condition2 only if the condition1 is False. In the above example ( line 3), 5 i.e. condition1 is True. It thus returns 5 without checking the condition2 i.e. 4 .
NOT operator negates the value and returns either True or False.
[Note: Any non-zero number is considered True. ]
Membership Operators in Python
Now come the god of all operators; the ones that have made python so user-friendly: “in” and “not in” operators. The “in” operator checks the presence of the element in the given data structure and returns the output as True or False accordingly. Likewise, the “not in” operator returns True when the element is not present in the given set of elements.
5 in [1,2,3,4,5] 5 not in [1,2,3,4,5]
>> True >> False
Bitwise Operators in Python
Bitwise operators provide for operations on bits.
a=5 #00000101 in binary b=10 #00001010 in binary print(a&b) #binary AND operator print(a|b) #binary OR operator print(a^b) #binary XOR operator print(~a) #binary NOT operator print(a>>2)#binary RIGHT SHIFT operator print(b<<1)#binary LEFT SHIFT operator
>> 0 #00000000 in binary >> 15 #00001111 in binary >> 15 #00001111 in binary >> -6 #11111010 in binary >> 1 #00000001 in binary >> 20 #00010100 in binary
The bitwise AND(&) and bitwise OR(|) perform the AND and OR bit by bit considering the input as a binary string. The bitwise NOT operator is a unary operator that means it operates on one operand. It does the work of flipping the bits. It basically returns the 2’s complement of the signed integer. The XOR operator returns 1 only if one of its operands is 1 but not both. There are two types of shift operators. The left shift operator (<<) shifts the bits left in the cyclic order. Likewise, the right shift operator(>>) shifts the bits right in the cyclic order. All the bitwise operators always return the output of the evaluated binary string in the decimal number system. It is important to remember that these operations are supported only by int data type (not float and complex numbers).
I hope you understood the basic operators in python. For any further queries, hit the comment section below or mail me at [email protected]
Also learn,
Leave a Reply