What are the Operators?
Operators are used to performing operations on variables as well as values. These are special symbols that use to carry out arithmetic as well as logical operations. The value that the operator operates on is Operand. Operators can manipulate individual items and returns results. The data items are referred to as operands or arguments.
Consider an example:
8 + 5 = 13. So here 8 and 5 are called operands and whereas + is Operator.
Types:
Arithmetic Operators |
Assignment Operators |
Logical Operators |
Comparision Operators |
Identity Operators |
Membership Operators |
Bitwise Operators |
Arithmetic Operator
Arithmetic operators are used to perform arithmetic operations between variables. In Python assignment operators are used for assigning the values of the right operand to the left operand. There are many methods for calculations like eval function, call function, declare variable etc. Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division etc.
+ – * / ** | Addition Subtraction Multiplication Division Exponent | % // | Modulus Floor Division |
Examples:
>>>a=5 >>>b=6 >>>print(a+b) | (Addition) Add Two Operands or Unary Plus |
Output=11 | |
>>>a=11 >>>b=5 >>>print=(a-b) | (Subtraction) Subtract Two Operands or Unary minus |
Output=6 | |
>>>a=5 >>>print(a*8) | (Multiplication) Multiply Two Operands |
Output=40 | |
>>>a=25 >>>print(a/5) | (Divide) Divide two operands and result is always in float or decimals |
Output=5 | |
>>>a=5 >>>b=2 >>>print(a%b) | (Modulus) Remainder of division of left operand to right |
Output=1 | |
>>>a=2 >>>b=5 >>>print(a ** b) | (Exponential) Left operand raised to power of right |
Output=32 | |
>>>a=19 >>>b=2 >>>print(a // b) | (Floor Division) Division which results into whole number adjusted to left line. |
Output=9 |
Assignment Operator
The assignment operator is used to assign values into variables. The assignment operator works from right to left. It means that the value of its RHS is copied to LHS must be a variable or some container and its RHS may be variable, literal or equation.
Operator | Example | Equvivalent to | Operator | Example | Equvivalent to |
+= -= *= /= %= &= |= | x+=7 x-=7 x*=7 x/=7 x%=7 x&=7 x|7 | x=x+7 x=x-7 x=x*7 x=x/7 x=x%7 x=x&7 x=x|7 | ^= <<= >>= | x^=7 x<<=7 x>>=7 | x=x^7 x=x<<7 x=>>7 |
Examples:
>>>x=12 >>>y=7 x+=y >>>print(x) |
Output=19 |
>>>x-=y >>>print(x) |
Output=5 |
>>>x*=5 >>>print(x) |
Output=84 |
>>>x/y >>>print(x) |
Output=1.72 |
>>>x*=*y |
Output=78125 |
Logical Operator
Logical Operator are used for conditional statements are true or false. In python logical operator are AND, OR, NOT.
For AND operator: This operator returns true but with mandatory condition if both the operands are true.
For OR operator: This operator returns true if either of the operand is true.
For NOT operator: This operator returns true if operand is false.
And Or not | Returns True if both statements are true Returns true if one of the statement is true Reverse the result, returns false if the result is true | X<5 and x<10 X<5 or x<4 Not(x<5 and x<10) |
Example:
x= True y= False print(‘x and y is’ ,x and y) print(‘x or y is’ ,x or y) print(‘not x is’ , not x) |
Output= x and y is False x or y is True not x is False |
Comparison Operator
Comparison operator are used to compare values. It either returns true or false according to the condition mentioned. These operators are also known as relational operator. No operator precedence in comparison operator. In comparison operator we compare the given values like x and y for resultant.
These operator are used (==,!=, >, <, >=, <=)
== != > < >= <= | Equal Not Equal Greater Than Smaller Than Greater Than Equals to Smaller Than Equals to |
Examples:
X=10 Y=12 Print(‘x > y is’ ,x>y) Print(‘x < y is’ ,x=y) Print(‘x >= y is’ ,x>=y) Print(‘x <= y is’ ,x<=y) |
Output: False True False True |
Membership Operator
The operator are used to validate the membership of value. It test for membership in a sequence such as strings, tuple, list.
“in operator” > This operator is used to access that whether if the value is existing in sequence or not. It access to true if it finds that a variable in a specified sequence and false otherwise.
“not in operator” > It is accessed for true if it doesn’t find variable in specified sequence and false otherwise.
Example:
x= ‘Hello World’ y={1: ‘a’, 2: ‘b’} print(‘H’ in x) print(‘hello’ not in x) print(1 in y) print(‘a’ in y) |
Output=True True True False |
Explanation: Here ‘H’ is in x but Hello is not present in x. Similarly, 1 is the key and ‘a’ is the value in dictionary y. Hence ‘a’ in y returns for False.
Identity Operator
Identity operator is used to compare the memory location of two identity operator, objects are used.
“is operator” > It access to true if the variables mentioned are on either side of the operator points to the same objects and results to false otherwise.
‘‘is not operator” Accesses to false if the variable if variable on either side of the operator directs to same object and results to true otherwise.
Example:
x1= 5 y1=5 x2=’Hello’ y2=’Hello’ x3=[1,2,3,] y3=[1,2,3] print(x1 is not y1) print(x2 is not y2) print(x3 is not y3) |
Output=False True False |
Explanation: Here, we see that x1 and y1 are integers of same values, so they are equal as well as identical. Same case with x2 and y2 (strings). But in case of x3 and y3 they are list. They are equal but not identical. Since lists are mutable (which can be change), interpreter locates them separately in memory although they are equal.
Difference between == and is operator:
Equal to equal operator compares the values of the opponent and check for the value equality whereas is operator checks whether both the opponents refers to same object or not.
Bitwise Operator
Bitwise operates acts on as if are string on binary digits. It operates bit by bit as name suggests. It inverts all bits of its operand. It always works with integer values. It will convert digital values into binary.
& | ~ ^ >> << | Bitwise AND Bitwise OR Bitwise NOT Bitwise XOR Bitwise Right Shift Bitwise Left Shift |
AND: Bits which are set in both x and y and resulted as set.
OR: Bits that are set in either x or y are sets.
XOR: Bits that are set in x or y but not both are sets.
NOT: Bits that are et in x are not set.
Shift Left: Shift the bits of x and y towards left.
Shift Right: Shift the bits of x and y towards right.
Examples:
- Right Shift Left Shift
a>>3 a<<3
a = 110 0000 1011 a = 1100 0000 1010
110 0000 0101 1000 0001 010
11 0000 0010 0000 0010 10
1 1000 0001 0000 0101 0
- A = 0011 1100
b = 0000 1101
Here,
A & b = 0000 1100
A | b = 0011 1101
A ^ b = 0011 0001
~ a = 1100 0011
Recommended Posts:
- http://www.shoutcoders.com/sap-scholar-interview-questions/
- http://www.shoutcoders.com/features-of-python-3-9/