Page Contents
Python operator is used to perform different operations between different variables and values. In the operation a+b
, a
and b
is operand and +
is operator .
Example 1:
number1=5
number2=6
#add operation between two values
print(1+2)
#in the operation 1+2 , 1 & 2 is operand and + is operator .
#+ operator adds the two operands.
#add operation between a variable and a value
print(number1+2)
#add operation between two variables
print(number1+number2)
3 7 11
In the above code we have created two variables named number1
and number2
and performed different operations –
- Through the
print(1 + 2)
statement, Python first added1
and2
and print3
to the output. - Through the
print(number1 + 2)
statement, Python first addednumber1
and2
.As the value ofnumber1
is5
, addition ofnumber1
and2
is7
and Python print7
to the output . - Through the
print(number1 + number2)
statement Python first addednumber1
andnumber2
. Since the values ofnumber1
andnumber2
are5
and6
, respectively, Python has printed11
in output by addingnumber1
andnumber2
.
Output :
Python offers a total 7 types of operators, theose are following :
- Arithmetic operators
- Comparison operators
- Logical operators
- Bitwise operators
- Assignment operators
- Membership operators
- Identity operators
We will now discuss about these operators in detail –
Arithmetic operators
Arithmetic operators are used to perform various mathematical processes (such as addition, division, multiplication, subtraction, etc.). All the arithmetic operators and their application are shown below in the form of a table taking value of x
and y
as 9
and 2
–
Operator(name) | Example | Description | Result |
---|---|---|---|
+ (Addition) | x + y | Add two operands | 11 |
- (Subtraction) | x – y | Subtract right operand from left operand | 7 |
* (Multiplication) | x * y | Multiply two operands | 18 |
/ (Division) | x / y | Divide left operand by right operand (returns a float) | 4.5 |
// (Floor division) | x // y | Divide left operand by right operand (returns a whole number less than the float number return from division) | 4 |
% (Modulus) | x % y | Remainder of the division of left operand by the right operand | 1 |
** (Exponential) | x ** y | Left operand raised to the power of right operand | 81 |
To learn more about arithmetic operator go to – Arithmetic Operators
Comparison operators
The comparison operator compares any two variables or values. All the comparison operators and their application are shown below in the form of a table taking value of x
and y
as 9
and 2
–
Operator(name) | Example | Description | Result |
---|---|---|---|
== (Equal to) | x == y | Returns True if both operands are equal else False | False |
!= (Not equal to) | x != y | Returns True if both operands are not equal else False | True |
> (Greater than) | x > y | Returns True if left operand is greater than right operand else False | True |
>= (Greater than or equal to) | x >= y | Returns True if left operand is greater than or equal to right operand else False | True |
< (Less than) | x < y | Returns True if left operand is less than right operand else False | False |
<= (Less than or equal to) | x <= y | Returns True if left operand is less than or equal to right operand else False | False |
To learn more about comparison operator go to – Comparison Operators
Logical operators
Logical operators are used to check various conditions. All the logical operators and their application are shown below in the form of a table taking value of x
and y
as True
and False
–
Operator | Example | Description | Result |
---|---|---|---|
and | x and y | Returns True if both operands are True else False | False |
or | x or y | Returns True if any of the operands is True else False | True |
not | x not y | Returns True if the operand is False else False | False |
To learn more about logical operator go to – Logical Operators
Bitwise operators
Bitwise operator works bit by bit. The bitwise operator is used to perform various operation on binary numbers. All the bitwise operators and their application are shown below in the form of a table.
The values of variables x
and y
used in the table are 6
and 10
respectively whose binary number is 0110
and 1010
respectively.
Operator(name) | Example | Description | Result |
---|---|---|---|
& (Bitwise AND) | x & y | Set each bit to 1 if both bits are 1 else 0 | 6 & 10 = 0110 & 1010 = 0010 = 2 |
| (Bitwise OR) | x | y | Set each bit to 1 if any of the bits is 1 else 0 | 6 | 10 = 0110 | 1010 = 1110 = 14 |
~ (Bitwise NOT or one’s complement) | ~ x | Returns -(x +1) | ~ 6 = ~ 0110 = -(0110+1) = -0111 = -7 , ~10 = ~1010 = -(1010+1) = -1011 = -11 |
^ (Bitwise XOR) | x ^ y | Set each bit to 1 if only one of two bits is 1 else 0 | 6 ^ 10 = 0110 ^ 1010 = 1100 = 12 |
<< (Bitwise Left Shift) | x << 2 | Shift left by pushing zeros at the right side | 6 << 2 = 0110 << 2 = 011000 = 24 ,10 << 2 = 1010 <<2 = 101000 = 40 |
>> (Bitwise Right Shift) | x >> 2 | Shift right by removing bits from right side | 6 >> 2 = 0110 >> 2 = 01 = 1 ,10 >> 2 = 1010 >> 2 = 10 = 2 |
To learn more about bitwise operator go to – Bitwise Operators
Assignment operators
Assignment operator is used to assign a value to a variable. All the assignment operators and their application are shown below in the form of a table. Every time we will take the value of x
as 10
.
Operator | Example | Description | Result |
---|---|---|---|
= (Assign) | x = 2 | x = 2 | 2 |
+= (Add and Assign) | x += 2 | x = x+2 | 12 |
-= (Subtract and Assign) | x -= 2 | x = x-2 | 8 |
*= (Multiply and Assign) | x *= 2 | x = x*2 | 20 |
/= (Divide and Assign) | x /= 2 | x = x/2 | 5.0 |
//= (Divide (floor) and Assign) | x //= 2 | x = x//2 | 5 |
%= (Modulus and Assign) | x %= 2 | x = x%2 | 0 |
**= (Exponent and Assign) | x **= 2 | x = x**2 | 100 |
&= (Bitwise AND and Assign) | x &= 2 | x = x&2 | 2 |
|= (Bitwise OR and Assign) | x |= 2 | x = x|2 | 10 |
^= (Bitwise XOR and Assign) | x ^= 2 | x = x^2 | 8 |
>>= (Bitwise Right Shift and Assign) | x >>= 2 | x = x>>2 | 2 |
<<= (Bitwise Left Shift and Assign) | x <<= 2 | x = x<<2 | 40 |
To learn more about assignment operator go to – Assignment Operators
Membership operators
Membership operator is used to check whether a value or variable is in a sequence (eg – String, List, Tuple, Dictionary, Set) or not. All the membership operators and their application are shown below in the form of a table taking x
and y
as 1
and [1,2,3,4]
. In the table, left operand x
is the value or variable and right operand y
is the sequence.
Operator | Example | Description | Result |
---|---|---|---|
in | x in y | Returns True if left operand is in the right operand else False | True |
not in | x not in y | Returns True if left operand is not in the right operand else False | False |
To learn more about membership operator go to – Membership Operators
Identity operators
Identity operator compares two objects. Identity operator never checks the value of two objects but checks whether the two objects are the same (same memory address) or not. All the identity operators and their application are shown below in the form of a table taking x
and y
as 2
and 3
.
Operator | Example | Description | Result |
---|---|---|---|
is | x is y | Returns True if x and y are same object else False | False |
is not | x is not y | Returns True if x and y are different object else False | True |
To learn more about identity operator go to – Identity Operators
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment