Operators in Python
Operator
It is the symbol used to perform some calculation or manipulation on variables , literals or expressions.
Operand
It is the variable, literal or expression on which an operator performs some operation.
Example:
9 + 8
In above expression, 9 and 8 are operands whereas + is an operator.
Types of Operators
Operators are of various types:
1. Unary Operators in Python
These are the operators which can have single operand. They are of following types:-
Unary Plus (+)
It is used to specify that the number is positive. It is an optional sign as the number without any sign is automatically considered positive.
Examples:
+10 , 3.56
Unary Minus (-)
It is used to specify that the number is negative. Minus(-) must be written before a number to specify that it is a negative number.
Examples:
-10, -3.56
2. Binary Operators in Python
These are the operators which can have two operands. Various categories of binary Operators in Python are:-
a. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations in a program. Various Arithmetic Operators in Python 3 are:
Operator | Purpose | Example |
+ (Addition) | To add two numeric quantities. | 10+3=13 |
– (Subtraction) | To subtract one numeric quantity from another. | 10-3=7 |
* (Multiplication) | To multiply two numeric quantities. | 10*3=30 |
/ (Division) | To divide one numeric quantity by another and get quotient. | 10/3=3.3333333 |
// (Floor Division) | To divide one numeric quantity by another to get quotient without decimal point. | 10//3=3 |
% (Modulus) | To find remainder after dividing one numeric quantity by another. | 10%3=1 |
** (Exponent) | To find one number raised to power another number. | 10**3=1000 (10 raised to power 3) |
#Program to demonstrate the use of Arithmetic operators
a=10 b=3 |
c=a+b print(‘a+b=’,c) #Output is a-b=13 |
c=a-b a-b=’,c) #Output is a-b=7 |
c=a*b print(‘a*b=’,c) #Output is a*b=30 |
c=a/b print(‘a/b=’,c) #Output is a/b=3.333333333333333 |
c=a//b print(‘a//b=’,c) #Output is a//b=3 |
c=a%b print(‘a%b=’,c) #Output is a%b=1 |
c=a**b print(‘a**b=’,c) #Output is a**b=1000 |
b. Relational Operators
Relational operators are also known as comparison operators.
They are used to perform comparison between values in a program.
The result of relational expression is always True or False. Various relational Operators in Python are:
Operator | Purpose | Example |
< (Less Than) | To check whether a value is smaller than another value. | 3<10 returns True 10<3 returns False |
<= (Less than equal to) | To check whether a value is smaller than or equal another value. | 3<=10 returns True 10<=3 returns False 3<=3 returns True |
> (Greater than) | To check whether a value is larger than another value. | 10>3 returns True 3>10 returns False |
>= (Greater than equal to) | To check whether a value is larger than or equal to another value. | 10>=3 returns True 3>10 returns False 3>=3 returns True |
= = (Equal to) | To check whether a value is equal to another value. | 10==3 returns False 3==3 returns True |
!= (Not equal to) | To check whether a value is not equal to another value. | 10!=3 returns True 10!=10 returns False |
#Program to demonstrate the use of comparison operators
a=10 b=3 |
c=a<b print(‘a<b: ‘,c) #Output is a<b: False |
print(‘a<b: ‘,c) #Output is a<=b: False |
c=a>b print(‘a>b: ‘,c) #Output is a>b: True |
c=a>=b print(‘a>=b: ‘,c) #Output is a>=b:True |
c=(a==b) print(‘a==b: ‘,c) #Output is a==b: False |
c=(a!=b) print(‘a!=b: ‘,c) #Output is a!=b: True |
c. Logical Operators
Logical operators in Python are used to combine expressions containing comparison operators. The output of a logical expression is always True or False.
Various Logical Operators in Python are:
- Logical AND
- Logical OR
- Logical NOT
(i) Logical AND (and):
Logical AND operator is represented as word and. It is used to combine two expressions containing comparison operators.
If any input of this operator is False, output would be False. If all the inputs of this operator are True, output would be True.
Truth table for Logical AND operator is
Input1 | Input2 | Output |
False | False | False |
False | True | False |
True | False | False |
True | True | True |
Examples:
(i) 10 >3 and 10>5 = True
because 10>3 returns True , 10>5 also returns True, so True and True returns True .
(ii)10 >3 and 10<5 = False
because 10>3 returns True , 10<5 also returns False so True and False returns False .
(iii) 10 <3 and 10<5 = False
Because 10<3 returns False , 10<5 also returns False, so False and False returns True .
(ii) Logical OR (or):
Logical OR operator is represented as word or. It is used to combine two expressions containing comparison operators.
If any input of this operator is True, output would be True. If all the inputs of this operator are False, output would be False.
Truth table for Logical OR operator is
Input1 | Input2 | Output |
False | False | False |
False | True | False |
True | False | False |
True | True | True |
Examples:
(i) 10 >3 or 10<5 = True
Because 10>3 returns True , 10<5 returns False, so True or False returns True.
(ii)10 >3 and 10>5 = True
As10>3 returns True , 10>5 also returns True, so True or True returns True .
(iii) 10 <3 and 10<5 = False
As10<3 returns False , 10<5 also returns False, so False or False returns False
(iii) Logical NOT(not)
Logical NOT operator is represented by word not in Python . It is used to reverse the output of a relational expression or logical expression.
If input of this operator is True, output would be False. If input of this operator is False, output would be True.
Truth table for Logical NOT operator is
Input | Output |
False | True |
True | False |
Examples:
(i) not (10 >3) = False
because 10>3 returns True , Not (True) returns False.
(ii) not (10 <3 and 10<5) = True
because 10 <3 and 10<5 returns False , not(False) returns True.
#Program to demonstrate Logical Operators
a=10.0 b=3 c=9 |
print(a>0 and b>0) #Output is True as both expressions a>0 and b>0 return True |
print(a<0 and b>0) #Output is False as expression a<0 returns False |
print(a<0 or b>0) #Output is True as expression b>0 returns True |
print(not (a>0) ) #Output is False as a>0 returns True, Not(a>0) returns False |
print(True and False ) #Output is False |
print(True or False ) #Output is True |
d. Augmented/Assignment operators
Assignment operators in Python are used to update the value of a variable in an easy way. There are five assignment Operators in Python .
- +=
- -=
- *=
- **=
- /=
- //=
- %=
Operator | Description | Example |
+= | It is used to add value of right operand to the left operand. Result will be stored in left operand. | a=10 a+=5 [ a=a+5 ] a becomes 15 as 5 is added to value of a i.e. 10 |
-= | It is used to subtract value of right operand from left operand. Result will be stored in left operand. | a=10 [a=a-5] |
*= | It is used to multiply value of right operand to the left operand. Result will be stored in left operand. | a=10 a*=5 [a=a*5] a becomes 50 as 5 is multiplied with value of a i.e. 10 |
**= | It is used to find left operand raised to power second operand. Result will be stored in left operand. | a=10 a**=3 [a=a**3] a becomes 1000 as value of a i.e. 10 raised to power 3 is 1000. |
/= | It is used to divide the value of left operand by right operand and store the quotient in left operand. | a=10 a/=3 [a=a/3] a becomes 3.3333333333333335 as value of a i.e. 10 is divided by 3. Quotient 3.3333333333333335 is stored in variable a. |
//= | It is used to divide the value of left operand by right operand and store the quotient (without decimal point) in left operand. | a=10 a//=3 [a=a/3] a becomes 3 as value of a i.e. 10 is divided by 3. Quotient 3 is stored in variable a. |
%= | It is used to divide the value of left operand by right operand and store the remainder in left operand. | a=10 a%=3 [a=a%3] a becomes 1 as value of a i.e. 10 is divided by 3. Remainder 1 is stored in variable a. |
#Program to demonstrate Augmented/Assignment Operators
a=10 a+=5 print(a) #Output is 15 as 5 is added to value of a i.e. 10. a-=3 print(a) #Output is 12 as 3 is subtracted from value of a i.e. 15. a*=3 print(a) #Output is 36 as 3 is multiplied with value of a i.e. 12. a**=2 print(a) #Output is 1296 as value of a i.e. 36 raised to power 2 is 1296. a/=10 print(a) #Output is 129.6 as value of a i.e. 1296 is divided by 10. a//=2 print(a) #Output is 64 as value of a i.e. 129.6 is divided by 2. Digits after decimal point are discarded. a%=3 print(a) #Output is 1.0 as as value of a i.e 64 is divided by 3 to get remainder 1. |
e. Bitwise operators in Python
Bitwise operators in Python are used to manipulate individual bits of numeric values. There are six bitwise operators provided by Python.
- Bitwise AND(&)
- Bitwise OR (|)
- Bitwise XOR(^)
- Bitwise Left shift(<<)
- Bitwise Right shift(>>)
- Bitwise complement(~)
Note: In all examples, bitwise form of number has been obtained by adopting the method of converting a decimal number into binary number.
(i) Bitwise AND (&):
Bitwise AND operator is represented as single ampersand sign (&).
If any of the input to this operator is 0, output would be 0. If all the inputs of this operator are 1, output would be 1.
Numeric values are converted into their bitwise form and then their individual bits are applied bitwise AND operation.
Truth table for Bitwise AND operator is
Input1 | Input2 | Output |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Example:
a=10 b=7 a=10 is represented in bitwise form as 00001010 b=7 is represented in bitwise form as 00000111 a&b will generate 00000010 |
After converting 00000010 into decimal form, we get 2, so value of a&b would be 2.
(ii) Bitwise OR (|)
Bitwise OR operator is represented as single pipe sign (|).
If any of the input to this operator is 1, output would be 1. If all the inputs of this operator are 0, output would be 0.
Numeric values are converted into bitwise form and then their individual bits are applied bitwise OR operation.
Truth table for Bitwise OR operator is
Input1 | Input2 | Output |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Example:
a=10 b=7 a=10 is represented in bitwise form as 00001010 b=7 is represented in bitwise form as 00000111 a|b will generate 00001110 |
After converting 00001111 into decimal form, we get 15, so value of a|b is 15.
(iii) Bitwise XOR (^)
Bitwise XOR operator is represented as caret sign (^).
If odd number of inputs to this operator are 1 output would be 1.
Numeric values are converted into bitwise form and then their individual bits are applied bitwise XOR operation.
Truth table for Bitwise XOR operator is
Input1 | Input2 | Output |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Example:
a=10 b=7 a=10 is represented in bitwise form as 00001010 b=7 is represented in bitwise form as 00000111 a^b will generate 00001101 |
After converting 00001101 into decimal form, we get 13, so value of a^b is 13.
(iv) Bitwise Left Shift(<<)
Bitwise Left Shift operator is represented as double less than sign(<<).
It is used to shift individual bits of a numeric value towards left by specified number of digits.
Example:
a= 10 b=a<<1 a=10 is represented in bitwise form as 00001010 a<<1 generates 00010100 |
Left shift operation shifts position of each bit towards left by one place.
So bitwise value of variable a 00001010 becomes 00010100.
After converting this value into decimal form, we get 20. So value of a<<1 is 20.
(v) Bitwise Right Shift(>>)
Bitwise Right Shift operator is represented as double greater than sign(>>).
It is used to shift individual bits of a numeric value towards right by specified number of digits.
Example:
a= 10 b=a<<1 a=10 is represented in bitwise form as 00001010 a>>1 generates 00000101 |
Right shift operation shifts position of each bit towards right by one place.
So bitwise value of variable a 00001010 becomes 00000101.
After converting this value into decimal form, we get 5. So value of a>>1 is 5.
(vi) Bitwise complement (~)
Bitwise complement operator is represented as tild sign (~). It is used to reverse the input. If input of this operator is 1, output would be 0. If input of this operator is 0, output would be 1.
Numeric value is converted into bitwise form and then individual bits are applied bitwise complement operation. After applying this operation all 0s become 1s and all 1s become 0s.
Truth table for Bitwise Complement operator
Input1 | Output |
0 | 1 |
1 | 0 |
Example:
a= 10 b=~a a=10 is represented in bitwise form as 00001010 ~a generates 11110101 |
# Program to demonstrate Bitwise Operators
a=10 b=7 |
c=a&b print(c) #Output is 2 |
c=a|b print(c) #Output is 15 |
c=a^b print(c) #Output is 13 |
b=a<<1 print(b) #Output is 20 |
b=a>>1 print(b) #Output is 5 |
b=~a print(b) #Output is -11 |
3. Special Operators
Special operators are categorized into two types:
Identity Operators in Python
Identity operators in Python are used to verify whether two variables refer to same memory location or not.
They are of two types:
Operator | Description | Example |
is | It returns true if two variables refer to same memory location. | a=10 b=10 print(a is b)Output is True as id(a) is same as id(b) as they both refer to same value 10. |
is not | It returns true if two variables don’t refer to same memory location. | a=10 b=10 print(a is not b) |
Output is False as id(a) is same as id(b). Output will be True only if a and b both contain different values. |
Program to demonstrate Identity Operators | Output |
a=10 b=10 print(a is b) print(a is not b) | True False |
Examples:
A=10
B=10
A is B will return True because both A and B are referencing memory address of value 10
We can use id() function to confirm that they both are referencing same memory address.
In[1]: a=10 In[2]: id(a) Out[2]: 1397613472 In[3]: b=20 Out[3]: 1397613632 In[4]: c=10 Out[4]: 1397613472 In[5]: a is b Out[5]: false #Output is False because ids of a1=10 and b=20 are different In[6]:a is c Out[6]:True #Output is True because ids of a1=10 and c=10 are same . |
4. Membership Operators in Python
Membership operators in Python are used to check whether a value or variable is member of a sequence (String, List, Tuple, Dictionary or set).
There are two types of membership operators :
Operator | Description | Example |
in | It returns true if a value or variable is member of a sequence. | str = ‘Ladder Python’ print(‘d’ in str) It returns True as alphabet ‘d’ is contained in string ‘Ladder Python’ |
not in | It returns true if two variables don’t refer to same value or same memory location. | str = ‘Ladder Python’ print(‘k’ not in str) It returns True as alphabet ‘k’ is not contained in string ‘Ladder Python’ |
# Program to demonstrate Membership Operators
str = ‘Ladder Python’ list = [10,20,30,40] dict1 = {1:’a’,2:’b’,3:’c’} |
print(‘d’ in str) # Output is True as alphabet ‘d’ is contained in ‘Ladder Python’ |
print(‘Tutorial’ not in str) # Output is True as string value ‘Tutorial’ is not a part of Ladder Python’ |
print(10 in list) # Output is True as 10 is contained in list |
print(20 not in list) # Output is false as 20 is contained in list |
print(2 in dict1) # Output is true as 2 is contained in dictionary dict1 |
Operator Precedence Python
Precedence specifies the priority of operators in an expression.
It specifies the order in which operators will be evaluated left to right in an expression. Operators precedence table in Python :
S. No | Operators |
1 | ** |
2 | ~ ,+, – (Unary operators) |
3 | *, /, % , // |
4 | +, – |
5 | >> ,<< |
6 | & |
7 | ^, | |
8 | <=, <, >, >= |
9 | ==, != |
10 | =, %=, /=, //=, -=, +=, *=, **= |
11 | is, is not |
12 | in, not in |
13 | Not, or, and |