and is a Logical AND that returns True if both the operands are true whereas ‘&’ is a bitwise operator in Python that acts on bits and performs bit by bit operation.
When an integer value is 0, it is considered as False when using logically.
[/sourcecode]
Example:
# Python program to demonstrate # the difference between and, & # operator year = 1992 print (year%4==0 and (year%100==0 or year%400==0))#print_stat1 print (year%4==0 & (year%100==0 or year%400==0))#print_stat2 |
Output:
False True
This is because ‘and’ tests whether both expressions are logically True while ‘&’performs bitwise AND operation on the result of both statements.
In print statement 1, compiler checks if the first statement is True. If the first statement is False, it does not check the second statement and returns False immediately. This is known as “lazy evaluation”. If the first statement is True then the second condition is checked and according to rules of AND operation, True is the result only if both the statements are True.
In print statement 2, the compiler is doing bitwise & operation of the results of statements. Here, the statement if getting evaluated as follows:
(year%100==0 or year%400==0)
year%100 = 92, which is a true value. Because only one True value is enough to return a True value, the statement will not be checked further, hence the result of this will be 92.
next, (year%4==0 & 92) will be evaluated.
year%4==0 = 0
(0 & 92) ==> 0000000 & 1011100 ==> 1011100 ==>92, which is a True value because it is non zero value.
Hence the result will be True.
To elaborate on this, we can take another example.
Example:
# Python program to demonstrate # the difference between and, & # operator a, b = 9, 10print(a & b)#line 1 print(a and b)#line 2 |
Output:
8 10
The first line is performing bitwise AND on a and b and the second line is evaluating the statement inside print and printing answer.
In line 1, a = 1001, b = 1010, Performing & on a and b, gives us 1000 which is the binary value of decimal value 8.
In line 2, the expression ‘a and b’ first evaluates a; if a is False (or Zero), its value is returned immediately because of “lazy evaluation” explained above, else, b is evaluated. If b is also non-zero then the resulting value is returned. The value of b is returned because it is the last value where checking ends for the truthfulness of the statement.
Hence the use of boolean and ‘and’ is recommended in a loop.
Recommended Posts:
- Python set operations (union, intersection, difference and symmetric difference)
- Difference between == and is operator in Python
- Difference between Method and Function in Python
- Python | Difference between iterable and iterator
- Difference between List and Array in Python
- Python | Difference between Pandas.copy() and copying through variables
- Difference between List comprehension and Lambda in Python
- Difference between Python and C#
- Difference between C and Python
- Python | Difference Between List and Tuple
- Difference Between Go and Python Programming Language
- Difference between continue and pass statements in Python
- Difference between input() and raw_input() functions in Python
- Difference between dict.items() and dict.iteritems() in Python
- Python - Difference between sorted() and sort()
- Difference between List and Dictionary in Python
- Python: Difference between Lock and Rlock objects
- Python program to find difference between current time and given time
- Python - Difference between json.dump() and json.dumps()
- Difference Between Python and Bash
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shrishti_m180521ca

