Operators
An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables. They usually form a part of the mathematical or logical expressions.
Python Operators: In this article, I will explain to you the following operators-
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Identity operators
- Membership operators
Arithmetic Operators: Same like C, C++, Java, C#; Python provides all the basic arithmetic operators, which are used to perform an arithmetic operation. Python. Python has the following Arithmetic Operators:
| Operators | Meaning |
| + | Addition Operators |
| - | Subtraction Operators |
| * |
Multiplication Operators
|
| / |
Division Operators
|
| % |
Modulo
|
| ** |
Exponent
|
| // |
Floor Division
|
Example:
- a=14
- b=4
- print("%d + %d = %d"%(a,b,a+b)) #Addition (+) Operator
- print("%d - %d = %d"%(a,b,a-b)) #Subtraction (-) Operator
- print("%d * %d = %d"%(a,b,a*b)) #Multiplication (*) Operator
- print("%d / %d = %d"%(a,b,a/b)) #Division (/) Operator
- print("%d ** %d = %d"%(a,b,a**b)) #Exponent (**) Operator
- print("%d // %d = %d"%(a,b,a//b)) #Floor Division (//) Operator
From the above example you was thinking that what is actually difference between simple divide and floor divide?
Relational Operators: We often compare two quantities and depending on their relation, take certain decisions. For example, we compare the age of the person for voting, On online shopping sites, we compare the price of two items and so on. These types of comparisons can be done with the help of relational operators. Python supports the following list of relational operators.
| Operators |
Meaning
|
| < |
is less than
|
| <= |
is less than or equal to
|
| > |
is greater than
|
| >= |
is greater than or equal to
|
| == |
is equal to
|
| != |
is not equal to (preferred)
|
| <> |
is not equal to (deprecated)
|
Example:
- a=5
- b=6
- print("%d < %d : %s"%(a,b,(a<b)))#is less than
- print("%d <= %d : %s"%(a,b,(a<=b)))#is less than od equal to
- print("%d > %d : %s"%(a,b,(a>b))) #is greater than
- print("%d >= %d : %s"%(a,b,(a>=b)))#is greater than or equal to
- print("%d == %d : %s"%(a,b,(a==b)))#is equal to
- print("%d != %d : %s"%(a,b,(a!=b)))#is not equal to
- AND (and)
- OR (or)
- NOT (not)
| Expression-1 | Expression-2 | (Expression-1 AND Expression-2) |
| False | False | False |
| False | True | False |
| True | False | False |
| True | True | True |
| Truth Table for AND | ||
| Expression-1 | Expression-2 | (Expression-1 OR Expression-2) |
| False | False | False |
| False | True | True |
| True | False | True |
| True | True | True |
| Truth Table for OR | ||

Shaili DashoraPosted Jul 28, 2015, 1:18 PM
Nice one..
Gopi ChandPosted Jul 20, 2015, 1:00 PM
Great
Santhakumar MunuswamyPosted Jul 20, 2015, 12:46 PM
Good one
Sibeesh VenuPosted Jul 20, 2015, 9:07 AM
Good one :)
Gowtham RajamanickamPosted Jul 20, 2015, 6:52 AM
good one...
SharadPosted Jul 20, 2015, 6:39 AM
Good one..
Nilesh JadavPosted Jul 20, 2015, 5:16 AM
Nice one sir