Post

# Python Operators Overview

• 14.4k
• 0
• 4

## 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:
1. a=14
2. b=4
3. print("%d + %d = %d"%(a,b,a+b)) #Addition (+) Operator
4. print("%d - %d = %d"%(a,b,a-b)) #Subtraction (-) Operator
5. print("%d * %d = %d"%(a,b,a*b)) #Multiplication (*) Operator
6. print("%d / %d = %d"%(a,b,a/b)) #Division (/) Operator
7. print("%d ** %d = %d"%(a,b,a**b)) #Exponent (**) Operator
8. print("%d // %d = %d"%(a,b,a//b)) #Floor Division (//) Operator
Output:

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)

The relational operator always give result in Boolean means result will be either true or false. If the expression of the relational operator will be true then the result will be true and if the expression of the relational operator will be fast then the result will be false.

Example:
1. a=5
2. b=6
3. print("%d <  %d : %s"%(a,b,(a<b)))#is less than
4. print("%d <= %d : %s"%(a,b,(a<=b)))#is less than od equal to
5. print("%d >  %d : %s"%(a,b,(a>b))) #is greater than
6. print("%d >= %d : %s"%(a,b,(a>=b)))#is greater than or equal to
7. print("%d == %d : %s"%(a,b,(a==b)))#is equal to
8. print("%d != %d : %s"%(a,b,(a!=b)))#is not equal to
Output:

Logical Operator: The logical operators are used when we want to test more than one condition and make decisions. Python's logical operators are listed below:
• AND (and)
• OR (or)
• NOT (not)
As I said  when we want to compare more than one expression then we use Logical operator but logical operators work on the truth table which is given below:

 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

 Expression NOT Expression False True True False Truth Table for NOT

Example :
1. a=5
2. b=6
3. c=7
4. print((a>b) and (a<c))# AND Operation
5. print((a>b) or (a<c))# OR Operation
6. print(not(a>b))#NOT Operation

Assignment Operators: Assignment operators are used to assigning the result of an expression to a variable. The usual assignment operator is '=' operator.  Same like other languages like C, C++, Java, and C#; Python also contains a set of "shorthand assignment operators" of the form:

Where v: is a Variable exp: is an expression op= : Assignment Operator.   The assignment statement
1. v op=exp
is equal to
1. v =v op (exp)
Shorthand assignment operators are listed below:

 Operators Meaning += Add Shorthand -= Subtract Shorthand *= Multiplication Shorthand /= Division Shorthand %= Modulo Shorthand **= Exponent Shorthand //= Floor Division Shorthand

Example
1. a=14 #Assign 5 to a
2. b=4 #Assign 6 to b
3.
4. a+=b;#Add Shorthand operator (Means a=a+b)
5. print(a);
6.
7. a-=b;#Subtract Shorthand operator (Means a=a+b)
8. print(a);
9.
10. a*=b;#Multiplication Shorthand operator (Means a=a+b)
11. print(a);
12.
13. a/=b;#Division Shorthand operator (Means a=a+b)
14. print(a);
15.
16. a%=b;#Modulo Shorthand operator (Means a=a+b)
17. print(a);
18.
19. a**=b;#Exponent Shorthand operator (Means a=a+b)
20. print(a);
21.
22. a//=b;#Floor Division Shorthand operator (Means a=a//b)
23. print(a);
Output :

Bitwise Operators: For manipulating data at the bit level Python has bitwise operators. These operators are used to testing the bits or shifting the bits. Python's Bitwise Operators are listed as following:

 Operators Meaning & Bitwise AND || Bitwise OR ^ Bitwise XOR(Exclusive OR) ~ One's Complement << Shift Left >> Shift Right

Bitwise AND, Bitwise OR, and Bitwise XOR works on truth table which is given below:

 a b a & b 0 0 0 0 1 0 1 0 0 1 1 1 Truth Table for BITWISE AND

 a b a | b 0 0 0 0 1 1 1 0 1 1 1 1 Truth Table for BITWISE OR

 a b a ^ b 0 0 0 0 1 1 1 0 1 1 1 0 Truth Table for BITWISE XOR

Bitwise operators perform on the bits so Assume we have two variables a=40 and b=14 now you want to calculate a & b, a | b and a^b then, first of all, you have to convert a and b to binary then you have to perform AND, OR and XOR on each and every bits then again convert the result to decimal then only you will get a result. like as following:

One's Compliment (~): The ones' complement of a binary number is defined as the value obtained by inverting all the bits in the binary representation of the number (swapping 0s for 1s and vice versa). The ones' complement of the number then behaves like the negative of the original number in some arithmetic operations.

Left Shift (<<): The left shift (<<) operator shifts the bits of expression 1 left by the number of bits specified. Example: Assume you have a number a=2 and you want to shift the bits left two times (a<<2) then, first of all, you have to convert then shift the bits two times and convert that result as decimal.

Recommended Free Ebook
Similar Articles