Python AND OperatorThere are two AND operators in Python-'Logical AND' and 'Bitwise AND'. This article discusses both the operators and differentiates them. Logical AND:This 'AND' belongs to Logical operators. These operators mainly deal with decision-making needs along with conditional statements like if-else. There are three logical operators in Python:
Now, let us get an understanding about Logical AND operator: Basic:In languages like C and Java, "&&" represents the AND operator, but in Python, we say "and" without using any special symbol. Syntax: a and b Return value: This operator either returns "True" or "False," depending on the operands. Operation:
Truth tale for "AND" Operator:
Need of AND operator:Suppose you are trying to write a code to find if a number is greater than 20 and is even. You need to check two conditions:
Both the conditions must be satisfied. Generally, we write it as: Output: ![]() It took 4 lines of code. Now let us use the functionality of the AND operator: Output: ![]() Understanding: In the line if (num > 20 and num % 2 == 0), first the left condition num > 20 is checked. For num = 34, it is true; hence the right condition is checked 34 is divisible by 2. Both the conditions are satisfied. The print statement is executed.
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 must be of age between 18 to 28. Any younger or older person is strictly not allowed to enter the competition. So, we need to check both conditions to enroll the student. Code: Output: ![]() "and" with just two numbers:Let us now see what happens if we give two decimal integers on both sides of the "and" operator: Pre-requisite: Generally, "and" checks if both the operands are True. Any number greater than 0 represents True, and 0 represents False. Sample Output: ![]() Understanding: 1. In the first case:
2. In the second case:
3. In the third case:
Let us see another example: Sample Output: ![]() Understanding: We checked if the num belongs in the intervals 0 to 10 or 10 to 50. If the number doesn't belong to both intervals, the number can either be less than 0 or greater than 50. "and" with multiple operands: Output: ![]() Understanding: First, num1 is checked and it is True, second number num2 is checked and it is True. But, the third number num3 is False, hence, num1 and num2 and num3 returned False. With multiple expressions: Output: ![]() Understanding: The first condition, num1 > 30, is itself False. Hence, directly False is printed. Even if the first condition turned out to be true, the second condition is False; if that is also true, the third one is False. Hence, all three conditions are False for the given values of num1, num2, and num3. Bitwise AND (&):There is another "and" operator in Python. It is a bitwise operator. We represent it as "&". Binary language is the language of a computer. All the inner mechanisms happen with respect to 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 the word 'bitwise' itself.
The operation of bitwise and: The given integers are converted into bits (binary), and 'and' 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 and on 3 and 4, we'll get True and 4 will be returned: Output: ![]() These are the two "and" operators available to use in Python language. Let us see the difference between these two operators: Logical AND vs. Bitwise AND
Next TopicPython OR Operator
|