Python AND Operator

Introduction:

In this article, we are discussing AND Operator in Python. There 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:

  1. Logical AND
  2. Logical OR
  3. Logical XOR

Now, let us get an understanding about Logical AND operator:

Basic:

In C and Java, "&&" represents the AND operator, but in Python, we say "and" without using any special symbol. We write "and" between two operands in Python.

Syntax:

The syntax of logical AND in Python is given below -

Return value:

The return value of logical AND in Python is given below -

This operator either returns "True" or "False," depending on the operands.

Operation:

  1. If all the operands/ expressions participating are true, AND returns True.
  2. If at least one of the operands is false, it returns False.
  3. If all the operands/ expressions participating are false AND returns false.
  4. If at least one of the operands is true, it returns False.

Truth tale for "AND" Operator:

Now here we give the truth table of the "AND" operator. The table is given below -

Expression 1Expression 2(Expression 1 and Expression 2) returns:
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

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:

  1. If num > 20
  2. If num is even

Both the conditions must be satisfied.

Program code:

In the code below, we check that the given number is larger than 20 and is also an even number. The code is given below -

In the above code, we solve the given problem in 4 lines. But here, we use AND operator, and the code is done in just three lines. The code is given below -

Python AND Operator

It took 4 lines of code. Now let us use the functionality of the AND operator:

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Enter a number: 34
34 is greater than 20 and is even

Explanation:

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. If we have 31, even if it is 20, as it is not divisible by 2, it will not be printed. Both the conditions are satisfied here. So, the result of this AND operator is true.

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.

Program Code:

Here we first check that the given age is greater than or equal to 18 and less than or equal to 28. If this condition is satisfied, then the student is eligible. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Please enter the participant's name: Priyanka
Please enter the valid age of the participant: 23
Congratulations!, You are eligible for the competition

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

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Enter a number: 3
Enter another number: 4
4
Enter a number: 0
Enter another number: 3
0
Enter a number: 0
Enter another number: 0
0

Explanation:

1. In the first case:

  1. Both the numbers are greater than 0 - True and True condition. Hence, "and" returns True.
  2. The condition of "and" is that both the operands should be true. Hence, after confirming that the first number- 3 (>0) is true, it checks the second number-4 (>0), which is true. So, it returns "True" with the second number.

2. In the second case:

  1. False and True condition
  2. The first number is 0-False.
  3. There should be no "False" for "and" to be True. Hence, once it confirms that the first number is "False", without checking the second number, it returns "False".

3. In the third case:

  1. The first number is 0-False.
  2. Immediately, "and" returns False.
  3. False and False condition

Let us see another example:

Sample Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Enter a number: 67
num is either less than 0 or greater than 50
Enter a number: 3
num is between 0 and 10
Enter a number: 34
num is between 10 and 50

Explanation:

We checked if the num belongs in the intervals 0 to 10 or 10 to 50. If the number does not belong to both intervals, the number can either be less than 0 or greater than 50.

Program code:

Here we give an example using the "and" operator. We multiplied three numbers using the "and" operator and displayed the result. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Enter the first value: 4
Enter the second value: 5
Enter the third value: 0
The result is:  0

Explanation:

Firstly, we enter the value of num1, which is a positive non-zero value. Then it is checked, and it returns true. After that, we enter the value of num2, it is a positive non-zero value. It is checked, and it returns true. Lastly, we enter the value of num3, and it is a zero. Then it is checked, and num3 returns false.

Program code:

Here we give an example using the "and" operator. Using the "and" operator, we checked multiple numbers greater or less than the given number. If all the condition is true, then it returns true. Otherwise, it returns false. Then display the result. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Enter the first value: 4
Enter the second value: 5
Enter the third value: 0
False

Explanation:

Firstly, we enter the value of num1, and it is 4. Then we checked the condition "num1>30," which returned false. After that, we enter the value of num2, which is 5. Then we checked the condition "num2>23," and num2 returned false. Lastly, we enter the value of num3, and it is 0. Then we checked the condition "num3<9" and num3 returns true. Num1 and num2 return false, and num3 returns true. So, the output of this code is false.

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:

  1. Bitwise AND
  2. Bitwise OR
  3. Bitwise NOT
  4. Bitwise XOR
  5. Bitwise right shift
  6. Bitwise left shift

The difference between (logical or, bitwise or), (logical and, bitwise and), (logical not, bitwise not) lies in the word 'bitwise' itself.

  • If we use any bitwise operator, first, the integer is converted into binary bits, and then and will perform the operation bit-by-bit.
  • Here, 1 represents True, and 0 represents False.
  • After the operation, the binary number will be converted to decimal and returned as the output.

The operation of bitwise and:

In the bitwise 'and' operator, we first convert the decimal number into binary numbers. Then 'and' operate on every corresponding bit of the two numbers.

Bit 1 (operand 1)Bit 2 (operand 2)Return value
000
100
111
010

Let us take an example:

If we perform logical and on 5 and 6, we'll get True and 6 will be returned:

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

Enter a number: 5
Enter another number: 6
The result of logical and:  6
The result of bitwise and:  4

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

Logical ANDBitwise AND
Logical AND is represented by "and."Bitwise and is represented by "&."
It is defined only for Boolean values; even if we use expressions, they are evaluated as True or False.It is defined for integer values as well as Boolean values
Print (True & False) -> False
It searches for a False value; if it finds one, it doesn't evaluate the rest of the expression and returns "False". This is called "Lazy evaluation".Even if the operator finds a False value, it continues evaluating the rest expressions.
This operator is mostly used for decision-making and truth testing.Bitwise operators are designed for lower-level bit manipulations.
In logical and, we need not convert the digit into binary form.In bitwise and, we need to convert the decimal digit into binary form.
Every number greater than 0 evaluates to "True" and every number == 0 or < 0 evaluate to "False"1 represents "True," and 0 represents "False".
In the operator hierarchy, Bitwise operators have higher precedence than the logical operators.