Python Operators
Operators in Python
Operators in python are the symbols used to perform an operation. Some of the most common operators used in Python are
Category |
Operators |
Arithmetic Operators |
+,-,*,/, %,// |
Relational Operators |
==,!=,>,<,>=,<= |
Assignment Operators |
=,+=,-=,*=,/=,%= |
Logical Operators |
and,or,not |
Arithmetic operators:
Operator |
Explanation |
Example |
+ |
Used for addition operation |
“+” is used as addition operator where 11+2 is evaluated as 13 |
- |
Used for subtraction operation |
'-' is used as subtraction operator where 11-2 is evaluated as 9, 2-11 is evaluated as -9 |
* |
Used for multiplication operation |
'*' is used as multiplication operator where 11*2 is evaluated as 22 |
/ |
Used for division operation |
'/' is used as division operator where 11/2 is evaluated as 5.5 |
// |
Used for integer division operation |
“//” is used for integer division where 11//2 is evaluated as 5 |
% |
Used for modulo operation, consider the expression num1% num2 which finds the remainder after dividing num1 by num2 |
“%” is used as modulo operator where 11%2 is evaluated as 1, 9%11 is evaluated as 9 |
Relational operators :
Operator |
Explanation |
Example |
== |
Used for checking the equality of two values/variable |
10 == 10 is evaluated as True
10 == 100 is evaluated as False |
!= |
Used for checking the in-equality of two values/variable |
10 != 10 is evaluated as False
10 != 100 is evaluated as True |
> |
Used for checking the of num1 is greater than num2 in num1 > num2 |
10 != 10 is evaluated as False
100 > 10 is evaluated as True> |
< |
Used for checking the of num1 is lesser than num2 in num1 > num2 |
10 < 10 is evaluated as False
10 < 100 is evaluated as True |
>= |
Used for checking the of num1 is lesser than or equal to num2 in num1 > num2 |
10 >= 10 is evaluated as True
10 >= 100 is evaluated as True |
<= |
Used for checking the of num1 is lesser than or equal to num2 in num1 > num2 |
10 <= 10 is evaluated as True
100 <= 10 is evaluated as False |
Assignment operators :
Operator |
Explanation |
Example |
= |
Used for assigning value to a variable |
num=5
Here num is assigned with the value 5 |
+= |
Used as short hand assignment operator addition |
num=num+1 can be represented using short hand assignment operator as num+=1 |
-= |
Used as short hand assignment operator subtraction |
num=num-1 can be represented using short hand assignment operator as num-=1 |
*= |
Used as short hand assignment operator multiplication |
num=num*1 can be represented using short hand assignment operator as num*=1 |
/= |
Used as short hand assignment operator division |
num=num/1 can be represented using short hand assignment operator as num/=1 |
%= |
Used as short hand assignment operator modulo operation |
num=num%1 can be represented using short hand assignment operator as num%=1 |
Logical operators are used to combine one or more relational expressions.
Operators |
Description |
AND |
Result will be true, if both the expressions are true. If any one or both the expressions are false, the result will be false |
OR |
Result will be true, even if one of the expression is true. If both the expressions are false, the result will be false |
NOT |
If the expression is true, result will be false and vice versa |
If A and B are two relational expressions, say A = (Num1>2000), B= (Num2>100), the result of combining A and B using logical operator is based on the result of A and B as shown below:
A |
B |
A and B |
True |
True |
True |
True |
False |
False |
False |
True |
False |
False |
False |
False |
A |
B |
A or B |
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
A |
not A |
True |
False |
False |
True |