Python Operators
What are Operators?
Operators are symbols applied to the variables or constants (Operands). Different actions are performed by different operators depending on the type of operands they are applied to. For example, when the '+' is used between two integers like
As a result, we get the sum of the operands, i.e., integer 5. But when this same operator is applied on two strings like
"Hello!" + "How are you." = "Hello! How are you."
We get a concatenated string as a result.
What are Operands?
Operands are the entities on which the operator is applied. For example
In the above example, the integers 7 and 3 are operands while * and = are the operators.
Types of Python Operators
The following are the operators supported by Python.
- Arithmetic Operators
- Logical Operators
- Assignment Operators
- Comparison Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Arithmetic Operators
They perform the usual arithmetic operations like addition, subtraction, multiplication, division, etc. In Python, arithmetic operators are in three forms. These are
- Unary Operators - These work on a single operand.
- Binary Operators - These work on two operands.
- Ternary Operators - These work on three operands. These are the conditional operators.
Python Unary Arithmetic Operators
Operators | Operations | Examples |
---|---|---|
Unary plus (+) | It gives the value of the operand | Let x = 15 Then +x means 15 Let x = -15 Then +x means -15 |
Unary Minus (-) | It results in the negated value of the operand | Let x = 15 Then -x means -15 Let x = -15 Then -x means 15 |
Binary Arithmetic Operators
Operators | Operations | Examples |
---|---|---|
Addition (+) | Returns the sum of operands | 1 + 2 = 3 5.0 + 3 = 8.0 2.5 + 3.5 = 6.0 "A" + "B" = "AB" [5,6,7] + [8,9] = [5,6,7,8,9] (1,2,3,4) + ('Bus','Truck') = (1,2,3,4,'Bus','Truck') |
Subtraction (-) | Subtracts the 2nd operand from the 1st operand | 6 - 5 = 1 1.0 - 3 = -2.0 2.5 - 3.5 = -1.0 |
Division (/) | Divides the 1st operand with 2nd operand and returns the quotient | 10 / 4 = 2.5 10.0 / 5 = 2.0 1.0 / 0.3 = 3.3333333333333335 |
Modulus (%) | Divides the 1st operand with 2nd operand and returns the remainder | 5 % 2 = 1 10.0 % 4 = 2.0 12.0 % 7.0 = 5.0 |
Multiplication (*) | Returns the product of the two operands | 2 * 3 = 6 -2.2 * 2 = -4.4 6.2 * 4.5 = 27.900000000000002 "Hello"*2 = “HelloHello” |
Exponent (**) | Returns the result as 1st operand raised to the power of 2nd operand | 5**2 = 25 2.5**3 = 15.625 10.5**3.2 = 1852.7027964066515 |
Truncated Division(//) | Divides the 1st operand with 2nd operand and returns the whole part of quotient | 10 // 4 = 2 10.25 // 4 = 2.0 -10 // 6.0 = -1.0 |
If you notice in the above examples, the result of an operation is implicitly type-casted to the type of operand, which is bigger in terms of byte size. For example
Comparison Operators
Python Comparison operators compare the two operands on both sides. If the comparison is successful or equal, Boolean value True is returned. While the comparison fails, Boolean value False is returned.
When comparing two numeric type operands, the trailing zeros after the decimal point are truncated; for example,
So, after truncation, 9.0 becomes 9; hence LHS and RHS are equal.
When two strings are being compared, the comparison is made lexicographically.
When comparing lists or tuples, the comparison is done assuming that both operands have the same elements in the same order and are equal.
While comparing two floating-point numbers, one thing to remember is that floating-point numbers are stored in binary form in memory. The conversion from binary to decimal is an approximation. This approximation can lead to unexpected results while using comparison operators. For example
0.1 + 0.2 == 0.3 (will return false)
This is because 0.1 + 0.2 = 0.30000000004. is the result of approximation.
These are the supported comparison operators in Python
Operators | Operations | Examples |
---|---|---|
> | Returns True if the left operand is greater than the right operand Else returns False | 4>1 (returns True) 3>7 (returns False) 'mystring' > 'abcde' (returns True) |
< | Returns True if the right operand is greater than the left operand Else returns False | 5<6 (returns True) 6<6 (returns False) |
>= | Returns True if the left operand is greater than Or equal to the right operand Else returns False | 6>=6 (returns True) 5.0 >= 7.5 (returns False) |
<= | Returns True if the left operand is lesser than Or equal to the right operand Else returns False | 5<=6 (returns True) 5.0 <= 7.5 (returns True) |
== | Returns True if the left operand is equal to the right operand Else returns False | 3 == 4 (returns False) 0.1+0.1+0.1 == 0.3 (returns False) ['Thislist'] == ['Thislist'] (returns True) |
!= | Returns True if the left operand is not equal to the right operand Else returns False | 3 != 4 (returns True) 0.1+0.1+0.1 != 0.3 (returns True) ['Thislist'] != ['Thislist'] (returns False) |
Assignment Operators
Python Assignment operators assign values to variables. The right operand's value is assigned to the left operand. Like
A = 44
Here, A is assigned the value 44 using ('=') the python's assignment operator.
Python also provides the compound assignment feature. These are explained in the below-given table.
Assignment operators in Python are:
Operators | Operations | Examples |
---|---|---|
= | It assigns the right operand's value to the left operand |
x=5 My_list = [1,5,99,65] t = 'ab' + 'c' |
+= | Add the left operand's value to the right operand, then assign it to the left operand |
A=5 A+=2 (returns 7) B=9 B+=9 (returns 18) |
-= | Subtract the right operand from the left operand, then assign it to the left operand |
A=7 A-=2 (returns 5) B=9 B-=9 (returns 0) |
*= | Multiply the left operand's value with the right operand's value, then assign it to the left operand |
A=7 A*=2 (returns 14) B=9 B*=9 (returns 81) |
/= | Divide the left operand's value with the right operand, then assign it to the left operand |
A=7 A/=2 (returns 3.5) B=9 B/=9 (returns 1) |
//= | Truncated operator divides the left operand by the right operand then assign to the left operand |
A=7 A//=2 (returns 3) B=9 B//=9 (returns 2) |
%= | Divide the left operand's value by the right operand, then assign the remainder to the left operand |
A=4 A%=3 (returns 1) B=8 B%=2 (returns 0) |
**= | The left operand's value raised to the power equal to the right operand, then assign it to the left operand |
A=2 A**=2 (returns 4) B=5 B**=3 (returns 125) |
&= | &(Bitwise and) the left operand's value is added with the right operand and then assigned to the left operand |
A = 2 B = 4 A &= B(returns 0) |
|= | | (Bitwise or) the left operand's value is bitwise OR with the right operand then assigned to the left operand |
A = 2 B = 4 A |= B(returns 6) |
^= | ^ (Bitwise xor) the left operand to the right operand then assign to the left operand |
A = 2 B = 4 A ^= B(returns 6) |
>>= | This operator right shifts the left operand's value to the number of positions equal to the right operand, then assign it to the left operand |
A = 2 B = 4 A >>= B(returns 0) |
<<= | This operator shifts the left operand's value to the number of positions equal to the right operand, then assign it to the left operand |
A = 2 B = 4 A <<= B(returns 32) |
Logical Operators
Python Logical Operators take two conditional expressions as operands and return the logical operation as a result. For example
Condition1 = True
Condition2 = False
Condition1 and Condition2 (will return false)
Logical operators that are supported by Python are
Operators | Operations | Examples |
---|---|---|
and | Results True if both of the operand expressions are True Else returns False. | 3==3 and 4==5 (Returns False) 1<2 and 4>2 (Returns True) |
or | Returns True if either of the operand expressions is True Else returns False. |
3==3 or 4==5 (Returns True) 1<2 or 4>2 (Returns True) |
not | Returns the inverted expression result. |
not True (returns False) not 4==2+1 (returns True) |
Bitwise Operators
This performs operations on their operands that change the state of their constituent bits (1s and 0s).
These are the bitwise operators available in Python are:
Operators | Operations | Examples |
---|---|---|
& | Returns 1 if both the operand bits are 1 Else returns 0. | 10101 & 10000 = 10000 11111 & 11001 = 11001 |
| | Returns 1 if either of the Operand bits is 1 Else returns 0. | 10101 | 10000 = 10101 11111 | 11001 = 11111 |
^ | Returns 1 if both the operand bits are different (either 1 and 0 or 0 and 1). Returns 0 if Boths bits are the same. | 10101 ^ 10000 = 00101 11111 ^ 11001 = 00110 |
~ | Returns the inverted Operand | ~11111 = 00000 ~01010 = 10101 |
>> | Shifts all the binary bits of the left operand to the right side as per number of positions given by the right operand. | 8 >> 1 = 4 (1000 >> 1 = 0100) 12 >> 3 = 1 (1100 >> 3 = 0001) |
<< | Shifts all the binary bits of the left operand to the left side as per the number of positions given by the right operand. | 8 << 1 = 16 (1000 << 1 = 10000) 12 << 3 = 96 (1100 << 3 = 1100000) |
Membership Operators
Python membership operators check whether the given operand is present in the sequence/series type object(list, tuple, etc.). It returns the True value if the operand is present in that sequence else, it returns False. in and not in are the two membership operators provided by Python.
Operators | Operations | Examples |
---|---|---|
in | Results in True if the left operand is present in the right operand(sequence). Else it returns False. | 'Mango' in ['Apple','Watermelon','Orange','Mango'] (returns True) 'Mango' in ('Apple','Watermelon','Orange','Banana') (returns False) 'hi' in 'hello hi how do you do?' (returns True) |
not in | Results in True if the left operand is not present in the right sequence. Else it returns False. | 'Mango' not in ['Apple','Watermelon','Orange','Mango'] (returns False) 'Mango' not in ('Apple','Watermelon','Orange','Banana') (returns True) 'hi' not in 'hello hi how do you do?' (returns False) |
Identity Operators
Identity operators took two operands and determined whether these two variables point to the exact memory location or not. They can also check if a variable is of a particular data type or not. Python provides two Identity Operators: is and is not.
Operators | Operations | Examples |
---|---|---|
is | Returns True if the left operand points to the same memory location as the right operand. Returns False if not. | A=5 B=5 A is B(returns True) A=2 B=22 A is B (returns False) A = 51 type(A) is int (returns True) |
is not | Returns True if the left operand does not result in the same memory location as the right operand.Else returns False. | A=5 B=5 A is not B(returns False) A=2 B=22 A is not B (returns True) A = 51 type(A) is not int (returns False) |
Note that Python Identity operators check the memory location of the operands and not their values like the '==' comparison operator.