python bitwise and or operator VS logical operator

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: 

  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 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: 

  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 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: 

  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    

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= :
The assignment statement: 
  1. v op=exp   

is equal to:

  1. 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: 

  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 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: 

  1. b= 14    
  2. print ( "a:%d" %a)   
  3. print ( "b:%d" %b)   
  4. print ( "a & b : %d" %(a&b)) #BITWISE AND    
  5. print ( "a | b : %d" %(a&b)) #BITWISE OR    
  6. print ( "a ^ b : %d" %(a&b)) #BITWISE EX-OR    
  7. print ( "~a : %d" %(~a)) #One's Compliment    
  8. 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: 

  1. a= 2    
  2. b= 16    
  3. print ( "a : %d" %a)   
  4. print ( "b : %d" %b)   
  5. print ( "a<<2 : %d" %(a<< 2 )) #a<<2    
  6. 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: 

  1. a= 5    
  2. b= 10    
  3.    
  4. x=a   
  5. y=b   
  6. z=b   
  7.    
  8. print (x  is  y) #false    
  9. print (y  is  z) #true    
  10.    
  11. print (x  is   not  y) #true    
  12. 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: 

  1. list=[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]    
  2. a= 5     
  3. b= 10     
  4. print ( "List is : %s" %list)    
  5. print ( "a : %d" %a)    
  6. print ( "b : %d" %b)    
  7. print (a  in  list) # this is true because a=5 and 5 is present in list     
  8. print (b  in  list) # this is false because b=10 and 10 is not present in list     
  9.     
  10. print (a  not   in  list) # this is false because a=5 and 5 is present in list     
  11. 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. 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值