Python operators are similar to C, C++ operators. Other programming language operators are similar to python operators. Operators are symbols that tell us which operation is performed on operands. An operand is a value on which we performed operations. There are different types of standard types of operators:-
1. Arithmetic Operators in Python
2. Relation Operators in Python
3. Assignment operator in Python
4. Logical Operators in Python
5. Bitwise Operator in Python
6. Member-ship Operator in Python
1. Arithmetic Operators in Python
Different arithmetic or logical operations are performed by python Programmers.. Arithmetic operators are used to performing arithmetic operations such as addition, subtraction and multiplication etc on numeric values.
Addition(+):-
This operator is used to add two operands and provide sum as a result.
>>x = 5
>>y = 3
>>x + y
Result:-
7
Subtraction(-):-
This operator is used to subtract two operands and provide a difference as a result.
>>x = 5
>>y = 3
>>x – y
Result:-
2
Multiplication(*):-
This operator is used to multiply two operands and provide the product as a result.
>>x = 5
>>y = 3
>>x * y
Result:-
15
Exponentiation(**):-
This operator provides the product as a result by raising the second number as power on the first number.
>>x = 5
>>y = 3
>>x ** y
Result:-
125
Division(/):-
This operator is used to divide two operands and provide a quotient as a result.
>>x = 15
>>y = 5
>>x / y
Result:-
3
Floor-Division(//):-
This operator is used to divide two operands and provide the nearest round(whole number ) quotient as a result.
>>x = 18
>>y = 4
>>x // y
Result:-
4
Modulus(%):-
This operator is used to divide two operands and provide the remainder as a result.
>>x = 18
>>y = 4
>>x % y
Result:-
2
2. Relational Operators in Python
Comparison operator is used to finding the relation between operands. It gives either True or False based on conditions.
Greater Than(>):-
This operator returns true if the right operand is less than left operand.
>>x = 5
>>y = 3
>>x > y
Result:-
True
Greater Than or Equal to(>=):-
This operator returns true if the left operand is greater than the right operand or if both operands are equal.
>>x = 5
>>y = 5
>>x >= y
Result:-
True
Less Than(<):-
This operator returns false if the left operand is greater than the right operand.
>>x = 5
>>y = 3
>>x < y
Result:-
False
Less Than or Equal to(<=):-
This operator returns true if the left operand is smaller than the right operand or if both operands are equal.
>>x = 5
>>y = 3
>>x <= y
Result:-
False
Equal to(==):-
This operator returns true if both operands are equal.
>>x = 5
>>y = 3
>>x == y
Result:-
False
Not Equal to(!=):-
This operator returns true if both operands are not equal.
>>x = 5
>>y = 3
>>x != y
Result:-
True
3. Assignment Operator in Python
Assignment operators are used to assigning values to the operands(variables).
Assign(=):-
This operator is used to assign the right-side value to the left operand.
>>x = 5
Result:-
5
Add and Assign(+=):-
This operator is used to add the left side operand with the right side operand and assign it to the left operand.
>>x = 5
>>x+=2
Result:-
7
Subtract and Assign(-=):-
This operator is used to subtract the right side operand with the left side operand and assign it to the left operand.
>>x = 5
>>x-=2
Result:-
3
Multiply and Assign(-=):-
This operator is used to multiply the right side operand with the left side operand and assign it to the left operand.
>>x = 5
>>x*=2
Result:-
10
Exponentiation and Assign(**=):-
This operator provides the product as a result by raising the right side operand as power on the left operand and assign it to the left operand.
>>x = 5
>>x**=2
Result:-
3
Division and Assign(/=):-
This operator is used to divide the right side operand with the left side operand and assign it to the left operand.
>>x = 15
>>x /= 5
Result:-
3
Floor-Division and Assign(//=):-
This operator is used to divide the right-side operand with the left side operand and provide the nearest round(whole number ) quotient as a result.
>>x = 18
>>x //= 4
Result:-
4
Modulus(%=):-
This operator is used to divide the right-side operand with the left side operand and provide the remainder as a result.
>>x = 18
>>x % =4
Result:-
2
4. Logical Operators in Python
It gives either True or False based on conditions. There are three types of logical operator and, or and not.
and:-
This operator gives the result as a True, if and only if both expressions are True.
>>x = 5
>>y = 3
>>x > y and x==3
Result:-
False
or:-
This operator gives the result as a True if one of the expressions is True.
>>x = 5
>>y = 3
>>x > y or x==3
Result:-
True
not:-
Not operator is used to reverse the result.
>>x = 5
>>y = 3
>>x > y
Result:-
False
5. Bitwise Operator in Python
AND(&):-
It perform and operation on binary bits of values.
>>x = 2
>>y = 3
>>x & y
Result:-
2
Explanation:-
Binary of 2 is 10
Binary of 3 is 11
Performed ‘and’operation on {10 } {11}
1 0
1 1 and operation
1 0
AND return true only if both numbers are 1, otherwise false.
10 in decimal is 2
OR(|):-
It perform or operation on binary bits of values.
>>x = 2
>>y = 3
>>x | y
Result:-
3
Explanation:-
Binary of 2 is 10
Binary of 3 is 11
Performed ‘and’operation on {10 } {11}
1 0
1 1 or operation
1 1
OR return true if one of the numbers is 1, otherwise false.
11 in decimal is 3
XOR(^):-
It perform xor operation on binary bits of values.
>>x = 2
>>y = 3
>>x ^ y
Result:-
1
Explanation:-
Binary of 2 is 10
Binary of 3 is 11
Performed ‘xor’ operation on {10 } {11}
1 0
1 1 xor operation
0 1
XOR returns true if odd numbers 1 are present, otherwise false.
01 in decimal is 1
NOT(~):-
It performs not operation on binary bits of values.
>>x = 2
>>~x
Result:-
-3
Explanation:-
Binary of 2 is 10
Performed ‘not’ operation on {10 }
1 0
0 1 Here 01 followed by sixty-two 1. That is why it is -3 instead of 1.
01 in decimal is 1
Left Shift(<<):-
It shifts the binary bits to the specified values.
>>x = 2
>>x<<3
Result:-
16
Explanation:-
Binary of 2 is 10
Shift 10 to 3 bits left.
1 0
1 0 0 0 0
10000 in decimal is 16
Right Shift(>>):-
It shifts the binary bits to the specified values.
>>x = 16
>>x>>2
Result:-
4
Explanation:-
Binary of 16 is 10000
Shift 10000 to 2 bits right.
1 0 0 0 0
0 0 1 0 0
100 in decimal is 4
6. Member-ship Operator in Python
It is used to check whether the given value is present in a statement or not.
in operator:-
It returns true if the value that you are looking for is present in a sequence of values.
>>x = 2
>>y = [1,2,4,6]
>>x in y
Result:-
True
not in operator:-
It returns false if the value that you are looking for is present in a sequence of values.
>>x = 2
>>y = [1,2,4,6]
>>x not in y
Result:-
False
To know different data types refer the link below.
Post a Comment
Post a Comment