Python OR OperatorLogical ORProgramming is a mixture of mathematics, logic, and many more domains. To solve any problem in mathematics, from textbook to real-time, we need addition, subtraction, multiplication and division operators. These are the fundamental operators needed in mathematics. In similar way, to perform any operation on variables or values, Python has 7 types of operators, and each is useful. Among these, "OR" is one of the operators in Python, and it belongs to Logical operators. Logical operators mainly deal with decision-making needs. There are three operators under logical operators:
This article discusses the OR operator with examples for a clear understanding. Basic: In languages like C and Java, "||" represents the OR operator, but in Python, we say "or" without using any special symbol. Syntax: a or b Return value: This operator either returns "True" or "False", depending on the operands.
Truth tale for "OR" Operator:
Need for OR operator:Suppose we need a number; if it is positive, it has to be greater than 20; if it is negative, it has to be less than -20. To meet these conditions, if we write the code using conditional statements: Output: Understanding: In the above code,
Output:
Let us take another example with a real-time application: Suppose you are organizing a coding competition for b-tech and m-tech students; for any student to take part, he should either be a b-tech or an m-tech student. Any person who is neither a b.tech or m.tech student is not allowed to enter the competition. So, we need to check if at least one condition is true. Code: Output: "or" with just two numbers:Let us now see what happens if we give two decimal integers on both sides of the "or" operator: Pre-requisite: Generally, "or" checks if at least one operand is True. Any number greater than 0 represents True, and 0 represents False. Sample outputs: Understanding:
"or" with multiple operands:Sample outputs: Understanding: It is the same as having two operands. "or" checks for a "True", and if it finds one, it returns "True", and if it doesn't find at least one True, it returns "False". Bitwise OR (|):There is another "or" operator in Python. It is a bitwise operator. We represent it as "|". Both are called "or" operators. Now, let us see the difference between the two operators: Binary language is the language of a computer. All the inner mechanisms happen concerning bits. Bitwise operators are the set of operators that allow the programmer to perform bitwise operations on integers. There are six bitwise operators:
The difference between (logical or, bitwise or), (logical and, bitwise and), (logical not, bitwise not) lies in their names.
The operation of bitwise or: The given integers are converted into bits (binary), and The operator will operate on every corresponding bit of the two numbers.
Let us take an example: If num1 = 3 and num2 = 4: 3 -> 011 4 -> 100 Performing |: If we perform logical or on 3 and 4, we'll get True and 3 will be returned: Output: These are the two "or" operators available to use in Python language. Logical OR vs Bitwise OR:
Next TopicPython Bitwise XOR Operator |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India