- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is different in | and OR operators in Python?
In Python or is a logical operator and | is a bitwise operator. The or operator requires two opeans of any type and may be true or false. It returns true if any one operand evaluates to true.
>>> a=50 >>> b=25 >>> a>40 or b>40 True >>> a>100 or b<50 True >>> a==0 or b==0 False >>> a>0 or b>0 True
The | operator takes bits as operands and returns 1 if any one operand is 1
>>> a=10 #0000 1010 >>> bin(a) '0b1010' >>> b=20 #0001 0100 >>> bin(b) '0b10100' >>> c=a|b >>> c 30 #0001 1110 >>> bin(c) '0b11110'
Advertisements