Bitwise And | a & b | and_(a, b) |
Bitwise Exclusive Or | a ^ b | xor(a, b) |
Bitwise Inversion | ~ a | invert(a) |
Bitwise Or | a | b | or_(a, b) |
Exponentiation | a ** b | pow(a, b) |
Identity | a is b | is_(a, b) |
Bitwise Operators
For manipulating data at the bit level, Python has bitwise operators.
These operators are used to test the bits or shift the bits. Python's Bitwise Operators are listed in the following table:
Operators | Meaning |
& | Bitwise AND |
|| | Bitwise OR |
^ | Bitwise XOR(Exclusive OR) |
~ | One's Complement |
<< | Shift Left |
>> | Shift Right |
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 in the following:
- AND (and)
- OR (or)
- NOT (not)
Python Operators Overview
Operators
A 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 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 arithmetic operations. 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
Output:
From the above example you may think that what is the 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 age of person for voting, On online shopping sites we compare the price of two items and so on. These type 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) |
Relational operator always give result in Boolean means result will be either true or false. If the expression of relational operator will be true then the result will be true and if expression of relational operator will be false then the result will be false.
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
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 in the following:
- 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 works 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:
- a= 5
- b= 6
- c= 7
- print ((a>b) and (a<c)) # AND Operation
- print ((a>b) or (a<c)) # OR Operation
- print ( not (a>b)) #NOT Operation
Output:
Assignment Operators
Assignment operators are used to assign the result of an expression to a variable. Usual assignment operator is '=' operator. Same like other language like C, C++, Java and C#; Python also contains set of" shorthand assignment operators " of the form:
Where
v :is a Variable
exp :is an expression
op= :- v op=exp
is equal to:
- v =v op (exp)
Shorthand assignment operators are listed in the following:
Operators | Meaning |
+= | Add Shorthand |
-= | Subtract Shorthand |
*= | Multiplication Shorthand |
/= | Division Shorthand |
%= | Modulo Shorthand |
*= | Exponent Shorthand |
//== | Floor Division Shorthand |
Example:
- a= 14 #Assign 5 to a
- b= 4 #Assign 6 to b
- a+=b; #Add Shorthand operator (Means a=a+b)
- print (a);
- a-=b; #Subtract Shorthand operator (Means a=a+b)
- print (a);
- a*=b; #Multiplication Shorthand operator (Means a=a+b)
- print (a);
- a/=b; #Division Shorthand operator (Means a=a+b)
- print (a);
- a%=b; #Modulo Shorthand operator (Means a=a+b)
- print (a);
- a**=b; #Exponent Shorthand operator (Means a=a+b)
- print (a);
- a//=b; #Floor Division Shorthand operator (Means a=a//b)
- print (a);
Output:
Bitwise Operators
For manipulating data at the bit level, Python has bitwise operators. These operators are used to test the bits or shift the bits. Python's Bitwise Operators are listed in the following table:
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 are given below:
Bitwise operators performs 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 result to decimal to get the result. The same is shown below:
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.
Example:
- b= 14
- print ( "a:%d" %a)
- print ( "b:%d" %b)
- print ( "a & b : %d" %(a&b)) #BITWISE AND
- print ( "a | b : %d" %(a&b)) #BITWISE OR
- print ( "a ^ b : %d" %(a&b)) #BITWISE EX-OR
- print ( "~a : %d" %(~a)) #One's Compliment
- print ( "~b : %d" %(~b)) #One's Compliment
Output:
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 "a" (in binary) and then shift the bits left two times and convert that result as decimal. Operation will be done as in the following:
Right shift(>>)
The rightshift (>>) operator shifts the bits of expression 1 right by the number of bits specified.
Example:Assume you have a number a=16 and you want to shift the bits right two times (a>>2) then first of all you have to convert "a" (in binary ) then shift the bits right two times and convert that result as decimal. Operation will be done as in the following:
Example:
- a= 2
- b= 16
- print ( "a : %d" %a)
- print ( "b : %d" %b)
- print ( "a<<2 : %d" %(a<< 2 )) #a<<2
- print ( "b>>2 : %d" %(b>> 2 )) #b>>2
Output:
Identity Operator
Identity operator compare the memory locations of two objects and there are two identity operators in Python which are listed below:
- is : Gives the result true if both operators points same memory location.
- is not : Gives the result true if both operators points different memory location.
Example:
- a= 5
- b= 10
- x=a
- y=b
- z=b
- print (x is y) #false
- print (y is z) #true
- print (x is not y) #true
- print (y is not z) #false
Output:
Membership operators
These operators test for membership in a sequence, such as string, list, tuple and sets. Membership operators are listed below:
- in : If a member is the part of sequence then it returns true otherwise false.
- not in : if a member is not part of sequence then it returns true otherwise false.
Example:
- list=[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
- a= 5
- b= 10
- print ( "List is : %s" %list)
- print ( "a : %d" %a)
- print ( "b : %d" %b)
- print (a in list) # this is true because a=5 and 5 is present in list
- print (b in list) # this is false because b=10 and 10 is not present in list
- print (a not in list) # this is false because a=5 and 5 is present in list
- print (b not in list) # this is true because b=10 and 10 is not present in list
Output:
Thanks. Hope you will like this. Keep sharing.