Python operates:
Let us take an expression 23 * 3 = 69. Here, 23 and 3 are called operands, and * is called the operator. With the help of Operators you can which can manipulate the value of operands.
Different types of operators in python:
1. Arithmetic operator
| Operator | Description | Example |
| Addition + | Adding two values | 6 + 4= 10 |
| Subtraction – | Subtract right hand operand to left end operand | 6 – 4= 2 |
| Multiplication * | Multiply the values | 6 * 4= 24 |
| Division / | dividing left hand operand by right hand operand and return answer | 6 / 4= 1 |
| Modulus % | Return remainder by dividing left hand operand by right hand operand | 6 % 4= 2 |
| Exponent ** | Calculate exponential expression | 6 ** 4 = 1296 |
| // | Floor Division – The division of operands where the result’s the quotient during which the digits after the percentage point are removed. On the other if one among the operands is negative then the result’s get floored. | 6//4= 1 and 6.0//4.0 = 1.0, -6//4 = -2, -11.0//3 = -4.0 |
x = 15
y = 2
print(x // y)
#the floor division // rounds the result down to the nearest whole number
x = 2
3
y = 5
print(x ** y) #same as 23*23*23*23*23
x = 5
3
y = 2
2
print(x % y)
x = 123
y = 34
print(x / y)
x = 51
y = 3
4
print(x * y)
x = 523
y = 3
print(x - y)
x = 54
y = 3
5
print(x + y)
2. Relational operator
| Operator | Description | Example |
|---|---|---|
| == | It returns true if the values of two operands are equal.. | (a == b) is not true. |
| != | Here condition becomes true, If values of two operands are not equal. | (a != b) is true. |
| <> | If values of two operands are not equal, then condition becomes true. | (a <> b) is true. This is similar to != operator. |
| > | It will return true, if the value of left operand is greater than the value of right operand. | (a > b) is not true. |
| < | It will return true, If the value of left operand is less than the value of right operand. | (a < b) is true. |
| >= | The condition becomes true, If the value of left operand is greater than or equal to the value of right operand. | (a >= b) is not true. |
| <= | The condition becomes true, If the value of left operand is less than or equal to the value of right operand. | (a <= b) is true. |
x = 5
y = 3
print(x <= y)
# It will returns False because 5 is neither less than or equal to 3
x = 5
y = 3
print(x >= y)
# it will returns True because five is greater, or equal, to 3
x = 5
y = 3
print(x < y)
# It will returns False because 5 is not less than 3
x = 5
y = 3
print(x > y)
# it willreturns True because 5 is greater than 3
x = 5
y = 3
print(x != y)
# it will returns True because 5 is not equal to 3
x = 5
y = 3
print(x == y)
# it will returns False because 5 is not equal to 3
3. Assignment operator
| Operator | Description | Example |
|---|---|---|
| = | Assignment operator assigns the values from right side operands to left side operand | a= b + c assigns value of b + c into a |
| += | It’s works by adding right operand to the left operand and assign the result to left operand | a+= b is equivalent to a = a + b |
| -= | It’s works by subtracting right operand from the left operand and assign the result to left operand | a -= b is equivalent to a = a – b |
| *= | It’s works by multiplying right operand with the left operand and assign the result to left operand | a *= b is equivalent to a = a * b |
| /= | It’s works by dividing left operand with the right operand and assign the result to left operand | a/= b is equivalent to a = a / b |
| %= | This operator takes modulus using two operands and assign the result to left operand | a %= b is equivalent to a = a % b |
| **= | This operator performs exponential calculation on operators and assign value to the left operand | a **= b is equivalent to a = a ** b |
| //= | This operator performs floor division on operators and assign value to the left operand | a //= b is equivalent to a = a // b |
x = 5
x <<= 3
print(x)
x = 5
x >>= 3
print(x)
x = 5
x ^= 3
print(x)
x = 5
x |= 3
print(x)
x = 5
x &= 3
print(x)
x = 5
x **= 3
print(x)
x = 5
x//=3
print(x)
x = 5
x%=3
print(x)
x = 5
x /= 3
print(x)
x = 5
x *= 3
print(x)
x = 5
x -= 3
print(x)
x = 5
x += 3
print(x)
x = 5
print(x)
4. Logical operator
| Operator | Description | Example |
|---|---|---|
| and (Logical AND) | The condition is true, If both the operands are true . | (x and y) is true. |
| or (Logical OR) | The condition is true, If any of the two operands are non-zero. | (x or y) is true. |
| not (Logical NOT) | This is used to flip (reverse) the logical state of its operand. | Not(x and y) is false. |
x = 9
print(not(x > 3 and x < 10))
# it will returns False because not is used to reverse the result
x = 2
print(x > 3 or x < 4)
# it will returns True because one of the conditions are true (5 is greater than 3, but 5 is not less than 4)
x = 8
print(x > 3 and x < 10)
# it will returns True because 5 is greater than 3 AND 5 is less than 10
5. Bitwise operator
| Operator | Description | Example |
|---|---|---|
| & Bitwise AND | Operator copies a bit to the result if it exists in both operands | (x & y) (means 0000 1100) |
| | Bitwise OR | It copies a bit if it exists in either operand. | (x | y) = 61 (means 0011 1101) |
| ^ Bitwise XOR | It copies the bit if it is set in one operand but not both. | (x ^ y) = 49 (means 0011 0001) |
| ~ Bitwise Ones Complement | It is unary and has the effect of ‘flipping’ bits. | (~x ) = -61 (means 1100 0011 in 2’s complement form due to a signed binary number. |
| << Bitwise Left Shift | The left operands value is moved left by the number of bits specified by the right operand. | x << 2 = 240 (means 1111 0000) |
| >> Bitwise Right Shift | The left operands value is moved right by the number of bits specified by the right operand. | x>> 2 = 15 (means 0000 1111) |
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0
c = a & b; # 12 = 0000 1100
print "Value is ", c
c = a | b; # 61 = 0011 1101
print "Value is ", c
c = a ^ b; # 49 = 0011 0001
print "Value is ", c
c = ~a; # -61 = 1100 0011
print "Value is ", c
c = a << 2; # 240 = 1111 0000
print "Value is ", c
c = a >> 2; # 15 = 0000 1111
print "Value is ", c
6. Membership operator
| Operator | Description | Example |
|---|---|---|
| in | It will evaluates to true if it finds a variable in the specified sequence and false if the sequence not available. | s in t, here in results in a 1 if s is a member of sequence t. |
| not in | It will evaluates to true if it does not finds a variable in the specified sequence and false if the sequence is available. | s not in t, here not in results in a 1 if s is not a member of sequence t. |
x = ["a", "b"]
print("a" in x)
# It will returns True because a sequence with the value "a" is in the list of variable
x = ["a", "b"]
print("c" not in x)
# It will returns True because a sequence with the value "c" is not in the list of variable
7. Identity operator
| Operator | Description | Example |
|---|---|---|
| is | It will return true if the variables on either side of the operator point to the same object and false in reverse. | a is b, here is results in 1 if id(a) equals id(b). |
| is not | It will return to false if the variables on either side of the operator point to the same object and true in reverse. | a is not b, here is not results in 1 if id(a) is not equal to id(b). |
x = ["a", "b"]
y = ["a", "b"]
z = x
print(x is z)
# It will returns True because z is the same object as x
print(x is y)
# It will returns False because x is not the same object as y, even if they have the same content
respectively
print(x == y)
# This is to demonstrate the difference between "is" and "==": this comparison returns True because x is equal to y respectively
x = ["a", "b"]
y = ["a", "b"]
z = x
print(x is not z)
# This will returns False because z is the same object as x
print(x is not y)
# This will returns True because x is not the same object as y, even if they have the same content
print(x != y)
# In order evaluate the difference between "is not" and "!=": this comparison returns False because x is equal to y
respectively.
