OPERATORS IN PYTHON
Operator: It is defined as a symbol which is responsible for a particular operation between two operands.
Python supports different types of operators:
1.Arithmetic Operators
2.Assignment Operators
3.Logical Operators
4.Comparison Operators
5.Bitwise Operators
6.Identity Operators
7.Membership Operators
Arithmetic Operators: They are used to perform Mathematical operations between two operands. i.e. Addition, Subtraction, Multiplication, Division etc.
Operator | Description | Syntax | Example(x=5,y=2) |
+ (Addition) | Add two Operands | (x+y ) | (x+y )5+2=10 |
– (Subtraction) | Subtract two Operands | ( x-y ) | 5-2=3 |
* (Multiplication) | multiply two Operands | ( x*y ) | 5*2=10 |
/ (Division) | Divide first operand by second | ( x/y ) | 5/2=2.5 |
** (Power) | First Operand raised to power of second | (x**y ) | 5**2=25 |
% (Modulus) | Remainder when first Operand is Divide by second | ( x%y ) | 5%2=1 |
// (floor Division) | Divide first operand by second | ( X//y ) | 5//2=2 |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
1 a = 10 2 b = 5 3 # Addition of numbers 4 sum = a + b 5 # Subtraction of numbers 6 sub = a - b 7 # Multiplication of numbers 8 mul a*b 9 # Division(float) of numbers 10 div1 = a / b 11 # Division (floor) of numbers 12 div2 = a // b 13 # Modulo of both numbers 14 mod = a % b 15 # Power of the numbers 16 power = a ** b 17 print("Addition of numbers: ", sum) 18 print("Subtraction of numbers: ", sub) 19 print("Multiplication of numbers: ", mul) 20 print("Division(float) of numbers: ", div1) 21 print("Division (floor) of numbers: ",div2) 22 print("Modulo of both numbers: ", mod) 23 print("Power of the numbers: ", power) |
Output
1 2 3 4 5 6 7 |
Addition of numbers: 15 Subtraction of numbers: 5 Multiplication of numbers: 50 Division (float) of numbers: 2.0 Division (floor) of numbers: 2 Modulo of both numbers: 0 Power of the numbers: 100000 |
Assignment operators: They are used to assign values of right expression to the left operand .
Operator | Description | Syntax(take s and t) |
(=) | Assign value of right side of expression to left side operand | s=t |
(+=) | Add right operand from left operand and then assign to left operand | s+=t (or) s=s+t |
(-=) | Subtract right operand from left operand and then assign to left operand | s-=t (or) s=s-t |
(*=) | Multiply right operand from left operand and then assign to left operand | s*=t (or) s=s*t |
(/=) | Divide left operand with right operand and then assign to left operand | s/=t (or) s=s/t |
(%=) | Takes modulus using left and right operands and assign the result to left operand | s%=t (or) s=s%t |
(**=) | Calculate exponent value and assign value to left operand | s**=t (or) s=s**t |
(//=) | Divide left operand with right operand and then assign the value(floor) to left operand | s//=t (or) s=s//t |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
1 #assining the value 2 s=10 3 t=2 4 print(s) 5 # adding and assigning 6 s+=t 7 print(s) 8 #subtracting and assiging 9 t--s 10 print(t) # multiply and assigning 12_t*=t 13 print(t) 14 #divide and assigning 15 t/=s 16 print(t) |
Output:
1 2 3 4 5 |
10 12 -10 100 8.333333333333334 |
Comparison operators : Comparison operators compare the values of the two operands and return a True or False i.e Boolean Value
Operator | Description | Syntax |
(==) Equal to | True if both operands are equal | S==T |
(!=) Not Equal to | True if operands are not equal | S!=T |
( >) Greater than | True if the left operand is greater than the right | S>T |
( <) Less than | True if the left operand is Less than the right | S<T |
( >=) Greater than or equal to | True if the left operand is greater than or equal to the right | S>=T |
( <=) Less than or equal to | True if the left operand is less than or equal to the right | S<=T |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
1 s=10 2 t=25 3 # validate for equality 4 print(s==t) 5 #validate for unequality 6 print(s!=t) 7 # validate for greater than 8 print(s>t) 9 # validate for lessthan 10 print(s<t) 11 #validate for greater than or equal to 12 print(s>=t) 13 #validate for less than or equal to 14 print(s<=t) |
Output:
1 2 3 4 5 6 |
False True False True False True |
Bitwise Operators: Performs bit by bit operations on two operands.
Operator | Description | Syntax(X, Y) |
& (BITWISE AND) | Resulting bit will be 1 only if both the bits are 1 else 0 | X&Y |
| (BITWISE OR) | Resulting bit will be 1 if either of the bits is 1 | X|Y |
~ (BITWISE NOT) | Resulting will be compliment of the given operand | ~X |
^ (BITWISE XOR) | Resulting will be 1 when both the bits are different otherwise 0 | X^Y |
>> (RIGHT SHIFT) | Shift the left operand bits towards the right for right operand number of times | X>>Y |
<< (LEFT SHIFT) | Shift the left operand bits towards the left for the right operand number of times | X<<Y |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1 S=10 2 t=2 3 print("The binary value of s ",bin(s)) 4 print("The binary value of s ",bin(t)) 5 #Bitwise AND 6 print(s&t, bin(s&t)) 7 #Bitwise OR 8 print(st,bin(s|t)) 9 # Bitwise not 10 print(~s, bin(~s)) 11 # Bitwise XOR 12 print(s^t, bin(s^t)) 13 # Bitwise Right shift 14 print(s>>t, bin(s>>t)) 15 # Bitwise left shift 16 print(s<<t, bin(s<<<<t)) |
Output:
1 2 3 4 5 6 7 8 |
The binary value of s : Ob1010 The binary value of s : Ob10 2 Ob10 10 Ob1010 -11 -0b1011 8 Ob1000 2 0b10 40 0b101000 |
Logical Operators: Used in the expression evaluation to make a decision
They are three logical operators
- Logical AND
- Logical OR
- Logical NOT
Operator | Description | Syntax(S,T are operands) |
AND | True if both the operands are False | S and T |
OR | True when either of the operands is True | S or T |
NOT | True if the operand is False | not S |
Example:
1 2 3 4 5 6 7 8 9 |
1 s=True 2 t=True 3 #Logical and 4 print(s and t) 5 #Logical or 6 print(s or t) 7 #logical not 8 print(not s) 9 print(not t) |
Output
1 2 3 4 |
True True False False |
Identity Operators: They are used to check if two values are located in the same part of memory or both belong to the same class or not.
There are two identical operators:
- Is
- Is Not
Operator | Description Of Operator |
is | True if the two operands share same memory location |
Is not | True if the two operands share different memory location |
We can verify it using the in built function of python called id ( )
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1 t=99 2 m-99 3 #checking for Unique id 4 print("the location of t is ",id(t)) 5 print("the location of m is ",id(m)) 6 print(t is m) 7 print(t is not m) 8 v=60 9 k=90 10 print("the location of v is ",id(v)) 11 print("the location of k is ",id(k)) 12 print(v is not k) 13 print(v is k) |
Output
1 2 3 4 5 6 7 |
The id of t is: 9792128 The id of m is: 9792128 True False The id of v is: 9790880 The id of k is: 9791840 True False |
Membership operators: It is used to check whether a value is present in the sequence or not
They are two membership Operators:
- IN
- Not IN
Example:
1 2 3 4 5 6 7 8 9 10 11 |
1 l=[10,20,30,40,50,90] 2 #using in Operator 3 if 90 in 1: 4 print("90 is Present in list 1") 5 else: 6 print("Not Present in list 1") 7 #using not in operator 8 if 99 not in 1: 9 print("99 is not present in list 1") 10 else: 11 print("99 is present in list 1") |
Ouput
1 2 |
90 is Present in list 1 99 is not present in list 1 |
Author : Teja |
LinkedIn : https://www.linkedin.com/in/teja-sai-nadh-reddy-tatireddy-048882201
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTExperts is always active on social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform