Operators and Operand in Python Programming
Operators in Python: Like other programming languages python also implements the concepts of operators. Operators are special symbols that represent operations like addition, multiplication etc.
Operand: The values required by the operator to operate is called operand.
Following are some valid expressions:
32+20 hour-1 hour*60+minutes
When the variable name appears at the place of an operand, it is replaced with its (variable) value. Like as you see above three examples are there. The first one is very clear ie. 32+20 but the second one little bit confusing because it’s showing hour-1. It does not return any meaning here, what does hour indicate?. I will let you know that here hour is a variable whose value can be 2,3,4 or anything. It is more clear in the example below:
Priority of Operations: In most cases, sometimes more than one operator appears in an expression, In that case, the order of operator evaluation depends upon rules of Precedence. Python follows the same precedence rules for its mathematical operators that mathematics does. We follow the PEMDAS acronym to remember that order of operations.
P: Parenthesis have the highest precedence.
E: Exponent has the next highest precedence.
M: Multiplication and division have the same precedence.
S: Precedence of subtraction and addition is next to the multiplication and division.
Types of Operators in Python
Python Arithmetic Operators
Arithmetic operations in Python are used to perform mathematical operations. Some of the mathematical operations are addition, subtraction and multiplication etc. We can combine these operators to perform more complex operations as well.
|/||Division (returns quotient)||10/2||5|
|%||Modulus (returns remainder)||4%3||1|
Assignment Operators in Python
Assignment operations in Python are mainly used to assign some values to variables. A very simple assignment operation is x = 10 that assigns value 10 to variable x.
There are many assignment operators in Python i.e x *= 5. This operation is same as x = x*5
|=||y = 5||y = 5|
|+=||y += 3||y = y + 3|
|-=||y -= 3||y = y – 3|
|*=||y *= 3||y = y * 3|
|/=||y /= 3||y = y / 3|
|%=||y %= 3||y = y % 3|
|//=||y //= 3||y = y // 3|
|**=||y **= 3||y = y ** 3|
|&=||y &= 3||y = y & 3|
|| =||y |= 3||y = y | 3|
|^=||y ^= 3||y = y ^ 3|
|>>=||y >>= 3||y = y >> 3|
|<<=||y <<= 3||y = y << 3|
Comparison Operators in Python
Comparison operators are used to compare two values. If returns the result as True or False. Suppose we have two variables a and b. If both values of both variables are equal to each other then it returns the result as True and if the values of both differ from each other then it returns False.
|==||Double Equal||x == y|
|!=||Not Equal||x != y|
|>||Greater than||x > y|
|<||Less than||x < y|
|>=||Greater than or equal to||x <= y|
|<=||Less than or equal to||x <= y|
Logical Operators in Python
Logical operators in Python are used in combining conditional statements. It also returns True or False based on conditions. It works on the principle of Logic gates i.e. and, or, not. Find the list of available logical operators in the below table.
|and||Returns True if both statements are true||x < 5 and x < 10|
|or||Returns True if one of the statement is true||x < 5 or x < 4|
|not||Reverse the result, returns False if the result is true||not(x < 5 and x < 10)|
Identity Operators in Python
Identity operators in Python are used to compare the objects. is and is not are the identical operators in Python. It does not check the equality but it checks the same object with the same memory allocation. Two equal values do not mean that they are identical. It returns True or False as an output.
|is||Returns True if both variables are the same object||x is y|
|is not||Returns True if both variables are not the same object||x is not y|
Python Membership Operators
Python membership operators are used to check whether the value or sequence of values are present in an object or not. i.e. (string, list, tuple, set and dictionary). It returns the result in boolean value i.e. True or False. in and not in are the membership operators in Python.
|in||Returns True if a sequence with the specified value is present in the object||x in y|
|not in||Returns True if a sequence with the specified value is not present in the object||x not in y|
Python Bitwise Operators
Bitwise operators in python are mainly used to compare Binary numbers.
|&||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