Curriculum
In this tutorial, you will learn about operators in Python, their usage and an example program demonstrating the usage of operators in your python application.
Operators are generally special symbols that perform specific operations on one, two, or three operands, and then return a result.
Consider the mathematical equation,
a + b = c
here ‘a’,’ ‘b’, ‘c’ are all variables and ‘+’ and ‘=’ are the operators. These operators perform an arithmetic operation and provide a result.
Python provides a vast set of operators to manipulate variables. They are classified based on the functions they provide.
The following operators are available in Python programming:
Arithmetic operators are used to perform arithmetic operations such as addition, subtraction etc.
Operator | Operation |
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo Operation |
** | Exponentiation |
// | Floor Division |
a=5 b=7 c=(a+b) d=(a-b) e=(a*b) f=(b/a) g=(a%b) print ("The value of c is:", c) print ("The value of d is:", d) print ("The value of e is:", e) print ("The value of f is:", f) print ("The value of g is:", g)
Output
The value of c is: 12 The value of d is: -2 The value of e is: 35 The value of f is: 1.4 The value of g is: 5
Assignment operators in Python are used to assign values to the designated variables.
+= | It is used for adding left operand with right operand and then assigning it to the variable on the left.| |
– , -= | It is used for subtracting the left operand with the right operand and then assigning it to the variable on the left.
|
*= | It is used for multiplying the left operand with the right operand and then assigning it to the variable on the left. |
/= | It is used for dividing the left operand with the right operand and then assigning it to the variable on the left. |
%= | It is used for assigning modulo of left operand with right operand and then assigning it to the variable on the left. |
//= | It is used for floor dividing left operand with right operand and then assigning it to the variable on the left. |
**= | It is used for exponentiation of left operand with right operand and then assigning it to the variable on the left. |
&= | It is used to perform the bitwise ‘and’ operation operand and then assign it to the variable on the left. |
|= | It is used to perform the bitwise ‘or’ operation operand and then assign it to the variable on the left. |
^= | It is used to perform the bitwise ‘xor’ operation operand and then assign it to the variable on the left. |
Sample program
a=5 b=7 a += 5 print ("The value of c is:", a) a*=7 print ("The value of d is:", a) a**=5 print ("The value of d is:", a) a/=7 print ("The value of d is:", a) a%= 5 print ("The value of d is:", a)
Output
The value of c is: 10 The value of d is: 70 The value of d is: 1680700000 The value of d is: 240100000.0 The value of d is: 0.0
The comparison operators are used to compare the given two values. The following operators belong to the comparison operators segment.
Operator | Description |
== | Equal |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Sample program
a=5 b=7 if a==b: print("a is equal to b") else: print("a is not equal to b") c=5 d=7 if c<d : print("c is lesser than d") else: print("c is greater")
Output
a is not equal to b c is lesser than d
Logical operators are used to checking whether an expression is true or false. They are used in decision making.
Operator | Meaning |
and | (Logical AND) |
or | (Logical OR) |
not | (Logical NOT) |
Sample program
a = 4 b = 7 c = 57 if a > 5 or b > 5: print("Either of The number is greater than 5") if a > 5 and b > 5 and c > 5: print("The numbers are greater than 5") else: print("Atleast one number is not greater than 5")
Output
Either of The number is greater than 5 Atleast one number is not greater than 5
As the name indicates, bitwise operators perform operations in bits on binary numbers.
& | Bitwise AND |
| | Bitwise OR |
~ | Bitwise NOT |
^ | XOR |
>> | Right shift Bitwise |
<< | Bitwise left shift |
Membership operators are used to checking whether the given value or a variable is available in a sequence.
Operator | Description |
in | The result is true if the operator finds the given variable in the specified sequence or else false. |
not in | The result is true if the operator does not find a variable in the specified sequence or else false. |
These operators are used to check whether the given set of values has the same memory locations.
is | The result is true if the variables on either side of the operator point to the same object and false otherwise. |
is not | The result is false if the variables on either side of the operator point to the same object and true otherwise. |
An expression is a combination of values, variables, operators, and functions. The Python interpreter can evaluate a valid expression.
Consider the following mathematical equation:
5-7+57*57/7%5
Here “5-7+57*57/7%5” is an expression. There can be any number of operators in an expression.
To perform the operations and to solve the expression/equation, there is a rule of precedence in Python. The rule of precedence shows the compiler the order of operations.
The below table shows the orders of precedence in which python solves the expression. It is given in descending order i.e. the highest precedence is given from the top
Operator | Description |
** | Exponentiation |
~ + – | Complement, unary plus and minus |
* / % // | Multiply, divide, modulo and floor division |
+ – | Addition and subtraction |
>> << | Right and left bitwise shift |
& | Bitwise ‘AND’td> |
^ | | Bitwise exclusive `OR’ and regular `OR’ |
<= < > >= | Comparison operators |
<> == != | Equality operators |
= %= /= //= -= += *= **= | Assignment operators |
is is not | Identity operators |
in not in | Membership operators |
Note: The order of precedence can be altered by using parenthesis (). Operators used inside the parenthesis are given the highest precedence regardless of its power.
Sample program:
a= 5-7+57*57/7%5 print( "The answer is", a)
Here, a lot of operators are used in this equation. When the equation is fed in the compiler, python interpretes the equation with the rule of precedence and equates the equation. This gives us the following result
Output:
The answer is 2.142857142857167