Table of Contents
Constructs in Python programming language that instruct the interpreter to perform a certain function involving two or more variables upon which the construct operates are called python operators. Though not actually seen as functions, they have their own identity as concepts because they are different syntactically and semantically from functions. It performs tasks of various nature, including arithmetic, bitwise, membership, identity, comparison, assignment, and logic, etc., with a certain order of execution existing among them.
Python Operators
Operators in python are constructs in Python that instruct the interpreter to perform a certain function; however, these are traditionally not defined as functions; rather, they are syntactically and semantically different from functions. Operators are used to performing operations on variables and values according to their use.
Python language supports the following types of operators.
- Arithmetic Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
- Comparison Operators
- Assignment Operators
- Logical Operators
1. Arithmetic Operator
Arithmetic operators are used to performing mathematical operations
Operator | Description | Syntax | Output |
+ | Addition | a+b | Returns sum of the operands |
â€“ | Subtraction | a-b | Returns Difference of the operands |
/ | Division | a/b | Returns Quotient of the operands |
* | Multiplication | a*b | Returns product of the operands |
** | Exponentiation | a**b | returns exponent of a raised to the power b |
% | Modulus | a%b | returns remainder of the division |
// | Floor division | a//b | returns a real value and ignores the decimal part |
Let us consider an example program for carrying out the arithmetic operations explained above.
Let us consider two integers Xa=2 and Xb=3
Program
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
add = Xa + Xb
diff = Xa - Xb
mul = Xa * Xb
div = Xa / Xb
floor_div = Xa // Xb
power = Xa ** Xb
modulus = Xa % Xb
print('Sum of the numbers is',Xa ,'and' ,Xb ,'is :',add)
print('Difference of the numbers is ',Xa ,'and' ,Xb ,'is :',diff)
print('Product of the numbers is ' ,Xa ,'and' ,Xb ,'is :',mul)
print('Division of the numbers is ',Xa ,'and' ,Xb ,'is :',div)
print('Floor Division of the numbers is ',Xa ,'and' ,Xb ,'is :',floor_div)
print('Exponent of the numbers is ',Xa ,'and' ,Xb ,'is :',power)
print('Modulus of the numbers is ',Xa ,'and' ,Xb ,'is :',modulus)
Output
2. Bitwise Operators
Refers to the operators working on a bit, i.e. they treat the operand as a string of bits; for example, in bitwise operations, 5 will be considered as 0101.
The box below provides the bitwise operators in python
Operator | Description | Syntax | Output |
& | Binary AND | a&b | copies a bit to the result if it exists in both operands |
| | Binary OR | a|b | copies a bit if it exists in either operand. |
^ | Binary XOR | a^b | copies the bit if it is set in one operand but not both. |
~ | Binary Oneâ€™s Complement | a~b | Unary operation of flipping bits |
<< | Binary Left Shift | a<<b | The left operands value is moved left by the number of bits specified by the right operand. |
>> | Binary Right Shift | a>>b | left operands value is moved right by the number of bits specified by the right operand. |
3. Membership Operators
Refers to the operators used in the validation of membership of operand test in a sequence, such as strings, lists, or tuples. There are two types of membership operators in python.
Operator | Syntax | Output |
in | if (a in x): | Evaluates to true if it finds a variable in the specified sequence and false otherwise. |
not in | If ( b not in x ): | Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. |
4. Identity Operators
Used to compare the operandsâ€™ memory locations, they are quite often used to determine if the operand is of a particular type; there are two types ofÂ identity operators in python.
Operator | Syntax | Output |
is | x is y | returns True if the type of the value in y points to the same type in the x. |
is not | x is not y | returnsÂ TrueÂ if the type of the value in y points to a different type than the value in the x |
5. Comparison Operators
Also known as Relational operators, these operators are used in determining the relation between the operand on either side of the operator.
Operator | Syntax | Output |
== | (a == b) | If the values of a and b are equal, then the condition becomes true. |
!= | (a != b) | If values of a and b are not equal, then the condition becomes true. |
<> | (a <> b) | If values of a and b are not equal, then the condition becomes true. |
> | (a > b) | If the value of a is greater than the value of b, then the condition becomes true. |
< | (a < b) | If the value of a is less than the value of b, then the condition becomes true. |
>= | (a >= b) | If the value of a is greater than or equal to the value of b, then the condition becomes true. |
<= | (a <= b) | If the value of b is less than or equal to the value of b, then the condition becomes true. |
6. Assignment Operators
Referred as the name suggests, it is used to declare assignments to the operands; the following are the types ofÂ assignment operators in python.
Operator | Description | Syntax | Output |
= | Equal to | c = a + b | assigns a value of a + b into c |
+= | Add AND | c += a | is equivalent to c = c + a |
-= | Subtract AND | c -= a | is equivalent to c = c â€“ a |
*= | Multiply AND | c *= a | is equivalent to c = c * a |
/= | Divide AND | c /= a | is equivalent to c = c / ac /= a is equivalent to c = c / a |
%= | Modulus AND | c %= a | is equivalent to c = c % a |
**= | Exponent AND | c **= a | is equivalent to c = c ** a |
//= | Floor Division | c //= a | is equivalent to c = c // a |
7. Logical Operators
These operators are used to perform similar operations as that of logical gates; there are 3 types of logical operators in python.
Operator | Description | Syntax | Output |
and | Logical AND | a and b | a condition is true if both a and b are true |
or | Logical OR | a or b | a condition is true if either a and b are true |
not | Logical NOT | not a | Complement the operand |
Python Operators are the backbone of any operations and functions in the programming context.
Grab the opportunity to learn Python with Entri! Click Here
Assignment Operators in Python
Operators are used to perform operations on values and variables. These are the special symbols that carry out arithmetic, logical, bitwise computations. The value the operator operates on is known asÂ Operand.
Here, we will coverÂ Assignment OperatorsÂ in Python. So,Â Assignment OperatorsÂ are used to assigning values to variables.
Operator | Description | Syntax |
---|---|---|
= | Assign value of right side of expression to left side operand | x = y + z |
+= | Add and Assign: Add right side operand with left side operand and then assign to left operand | a += b |
-= | Subtract AND: Subtract right operand from left operand and then assign to left operand: True if both operands are equal | a -= b |
*= | Multiply AND: Multiply right operand with left operand and then assign to left operand | a *= b |
/= | Divide AND: Divide left operand with right operand and then assign to left operand | a /= b |
%= | Modulus AND: Takes modulus using left and right operands and assign result to left operand | a %= b |
//= | Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand | a //= b |
**= | Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand | a **= b |
&= | Performs Bitwise AND on operands and assign value to left operand | a &= b |
|= | Performs Bitwise OR on operands and assign value to left operand | a |= b |
^= | Performs Bitwise xOR on operands and assign value to left operand | a ^= b |
>>= | Performs Bitwise right shift on operands and assign value to left operand | a >>= b |
<<= | Performs Bitwise left shift on operands and assign value to left operand | a <<= b |
Now Letâ€™s see each Assignment Operator one by one.
1) Assign:Â This operator is used to assign the value of the right side of the expression to the left side operand.
Syntax:
x = y + z
Example:
- Python3
# Assigning values usingÂ # Assignment Operator Â a = 3 b = 5 Â c = a + b Â # Output print (c) |
Output:
8
2) Add and Assign:Â This operator is used to add the right side operand with the left side operand and then assigning the result to the left operand.
Syntax:Â
x += y
Example:
- Python3
a = 3 b = 5 Â # a = a + b a + = b Â # Output print (a) |
Output:
8
3)Â Subtract and Assign:Â This operator is used to subtract the right operand from the left operand and then assigning the result to the left operand.
Syntax:
x -= y
Example â€“
- Python3
a = 3 b = 5 Â # a = a - b a - = b Â # Output print (a) |
Output:
-2
Â 4)Â Multiply and Assign:Â This operator is used to multiply the right operand with the left operand and then assigning the result to the left operand.
Syntax:
x *= y
Example:
- Python3
a = 3 b = 5 Â # a = a * b a * = b Â # Output print (a) |
Output:
15
Â 5)Â Divide and Assign:Â This operator is used to divide the left operand with the right operand and then assigning the result to the left operand.
Syntax:Â
x /= y
Example:
- Python3
a = 3 b = 5 Â # a = a / b a / = b Â # Output print (a) |
Output:
0.6
Â 6) Modulus and Assign:Â This operator is used to take the modulus using the left and the right operands and then assigning the result to the left operand.
Syntax:
x %= y
Example:
- Python3
a = 3 b = 5 Â # a = a % b a % = b Â # Output print (a) |
Output:
3
7) Divide (floor) and Assign:Â This operator is used to divide the left operand with the right operand and then assigning the result(floor) to the left operand.
Syntax:
x //= y
Example:
- Python
a = 3 b = 5 Â # a = a // b a / / = b Â # Output print (a) |
Output:
0
Â 8)Â Exponent and Assign:Â This operator is used to calculate the exponent(raise power) value using operands and then assigning the result to the left operand.
Syntax:
x **= y
Example:
- Python
a = 3 b = 5 Â # a = a ** b a * * = b Â # Output print (a) |
Output:
243
9) Bitwise AND and Assign:Â This operator is used to perform Bitwise AND on both operands and then assigning the result to the left operand.
Syntax:
x &= y
Example:
- Python3
a = 3 b = 5 Â # a = a & b a & = b Â # Output print (a) |
Output:
1
10) Bitwise OR and Assign:Â This operator is used to perform Bitwise OR on the operands and then assigning result to the left operand.
Syntax:
x |= y
Example:
- Python3
a = 3 b = 5 Â # a = a | b a | = b Â # Output print (a) |
Output:
7
11)Â Bitwise XOR and Assign:Â This operator is used to perform Bitwise XOR on the operands and then assigning result to the left operand.
Syntax:
x ^= y
Example:
- Python3
a = 3 b = 5 Â # a = a ^ b a ^ = b Â # Output print (a) |
Output:
6
12)Â Bitwise Right Shift and Assign:Â This operator is used to perform Bitwise right shift on the operands and then assigning result to the left operand.
Syntax:
x >>= y
Example:
- Python3
a = 3 b = 5 Â # a = a >> b a >> = b Â # Output print (a) |
Output:
0
Â 13)Â Bitwise Left Shift and Assign:Â This operator is used to perform Bitwise left shift on the operands and then assigning result to the left operand.
Syntax:
x <<= y
Example:
- Python3
a = 3 b = 5 Â # a = a << b a << = b Â # Output print (a) |
Output:
96
Learn to code from industry experts! Enroll here
Python Relational Operators
A relational operator is used to compare two values. Based on the operator and values, the operator returns either True or False.
In Python, there are six Relational Operators. They are
Symbol | Name | Example | Description |
---|---|---|---|
== | Equal to | x == y | Returns True if x and y have same value, else returns False. |
> | Greater than | x > y | Returns True if value of x is greater than value of y, else returns False. |
< | Less than | x < y | Returns True if value of x is less than value of y, else returns False. |
!= | Not equal to | x != y | Returns True if x and y do not have same value, else returns False. |
>= | Greater than or equal to | x >= y | Returns True if value of x is greater than or equal to value of y, else returns False. Equivalent to ( x>y or x==y ) |
<= | Less than or equal to | x <= y | Returns True if value of x is less than or equal to value of y, else returns False. Equivalent to ( x>y or x==y ) |
Equal to Operator
Equal to operatorÂ ==
Â takes two operands and returns boolean value of True, if both the operands are equal in value, else it returns False.
In the following example, we will take two integer values such than both are equal in value, and check the result of equal to operator with these two integers provided as operands to the equal to operator.
Python Program
x = 41
y = 41
result = x == y
print(result)
Â Run
Output
True
In the following example, we will take two integer values such than both are not equal in value, and check the result of equal to operator with these two integers provided as operands to the equal to operator.
Python Program
x = 41
y = 63
result = x == y
print(result)
Â Run
Output
False
Greater than Operator
Greater than operatorÂ >
Â takes two operands and returns boolean value of True, if the left operand is greater than the right operand in value, else it returns False.
In the following example, we will take two integer values such that left operand is greater than the right operand, and check the result of greater than operator.
Python Program
x = 87
y = 63
result = x > y
print(result)
Â Run
Output
True
In the following example, we will take two integer values such that left operand is less than the right operand, and check the result of greater than operator.
Python Program
x = 41
y = 63
result = x > y
print(result)
Â Run
Output
False
Less than Operator
Less than operatorÂ <
Â takes two operands and returns boolean value of True, if the left operand is less than the right operand in value, else it returns False.
In the following example, we will take two integer values such that left operand is less than the right operand, and check the result of less than operator.
Python Program
x = 41
y = 63
result = x < y
print(result)
Â Run
Output
True
In the following example, we will take two integer values such that left operand is greater than the right operand, and check the result of less than operator.
Python Program
x = 92
y = 63
result = x < y
print(result)
Â Run
Output
False
Not Equal to Operator
Not Equal to operatorÂ !=
Â takes two operands and returns boolean value of True, if both the operands are not equal in value, else it returns False.
In the following example, we will take two integer values such that both are not equal in value, and check the result of not equal to operator with these two integers provided as operands to the not equal to operator.
Python Program
x = 92
y = 63
result = x != y
print(result)
Â Run
Output
True
In the following example, we will take two integer values such that both are equal in value, and check the result of not equal to operator with these two integers provided as operands to the not equal to operator.
Python Program
x = 63
y = 63
result = x != y
print(result)
Â Run
Output
False
Greater than or Equal to Operator
Greater than or Equal to operatorÂ >=
Â takes two operands and returns boolean value of True, if the left operand is greater than or equal to the right operand in value, else it returns False.
In the following example, we will take two integer values such that left operand is greater than or equal to the right operand, and check the result of greater than or equal to operator.
Learn Coding in your Language! Enroll Here!
Python Program
x = 97
y = 63
result = x >= y
print(result)
Â Run
Output
True
In the following example, we will take two integer values such that left operand is less than the right operand, and check the result of greater than or equal to operator.
Python Program
x = 42
y = 63
result = x >= y
print(result)
Â Run
Output
False
Less than or Equal to Operator
Less than or Equal to operatorÂ <=
Â takes two operands and returns boolean value of True, if the left operand is less than or equal to the right operand in value, else it returns False.
In the following example, we will take two integer values such that left operand is less than the right operand, and check the result of less than or equal to operator.
Python Program
x = 42
y = 63
result = x <= y
print(result)
Â Run
Output
True
In the following example, we will take two integer values such that left operand is greater than the right operand, and check the result of less than or equal to operator.
Python Program
x = 92
y = 63
result = x <= y
print(result)
Â Run
Output
False
Arithmetic operatorsÂ are used to perform mathematical operations like addition, subtraction, multiplication and division.
There are 7 arithmetic operators in Python :
- Addition
- Subtraction
- Multiplication
- Division
- Modulus
- Exponentiation
- Floor division
1. Addition Operator :Â In Python,Â +Â is the addition operator. It is used to add 2 values.
Example :
val1 = 2 val2 = 3 Â # using the addition operator res = val1 + val2 print (res) |
Output :
5
2. Subtraction Operator :Â In Python,Â â€“Â is the subtraction operator. It is used to subtract the second value from the first value.
Example :
val1 = 2 val2 = 3 Â # using the subtraction operator res = val1 - val2 print (res) |
Output :
-1
3. Multiplication Operator :Â In Python,Â *Â is the multiplication operator. It is used to find the product of 2 values.
Example :
val1 = 2 val2 = 3 Â # using the multiplication operator res = val1 * val2 print (res) |
Output :
6
4. Division Operator :Â In Python,Â /Â is the division operator. It is used to find the quotient when first operand is divided by the second.
Example :
val1 = 3 val2 = 2 Â # using the division operator res = val1 / val2 print (res) |
Output :
1.5
5. Modulus Operator :Â In Python,Â %Â is the modulus operator. It is used to find the remainder when first operand is divided by the second.
Example :
val1 = 3 val2 = 2 Â # using the modulus operator res = val1 % val2 print (res) |
Output :
1
6. Exponentiation Operator :Â In Python,Â **Â is the exponentiation operator. It is used to raise the first operand to power of second.
Example :
val1 = 2 val2 = 3 Â # using the exponentiation operator res = val1 * * val2 print (res) |
Output :
8
7. Floor division :Â In Python,Â //Â is used to conduct the floor division. It is used to find the floorof the quotient when first operand is divided by the second.
Example :
val1 = 3 val2 = 2 Â # using the floor division res = val1 / / val2 print (res) |
Output :
1
Below is the summary of all the 7 operators :
Operator | Description | Syntax |
---|---|---|
+ | Addition: adds two operands | x + y |
â€“ | Subtraction: subtracts two operands | x â€“ y |
* | Multiplication: multiplies two operands | x * y |
/ | Division (float): divides the first operand by the second | x / y |
// | Division (floor): divides the first operand by the second | x // y |
% | Modulus: returns the remainder when first operand is divided by the second | x % y |
** | Power : Returns first raised to power second | x ** y |