Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python logical operator AND
#1
#Python Code
a = 6
b = 7
print (a and b)
#End code

Why the result of the print function is 7 instead of 6?

If:
0110 ->6
AND 0111 ->7
-------
0110 ->6

Thanks!
Reply
#2
from the docs:
Quote:The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

also here:
Quote:Boolean Operations — and, or, not

These are the Boolean operations, ordered by ascending priority:
  • x or y: if x is false, then y, else x (1)
  • x and y: if x is false, then x, else y (2)
  • not x: if x is false, then True, else False (3)

Notes:
  1. This is a short-circuit operator, so it only evaluates the second argument if the first one is false.
  2. This is a short-circuit operator, so it only evaluates the second argument if the first one is true.
  3. not has a lower priority than non-Boolean operators, so not a == b is interpreted as not (a == b), and a == not b is a syntax error.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
According to the python Docs :-
In the context of Boolean operations, and also when expressions are used by control flow statements, the following values are interpreted as false: False, None, numeric zero of all types, and empty strings and containers (including strings, tuples, lists, dictionaries, sets and frozensets). All other values are interpreted as true. User-defined objects can customize their truth value by providing a __bool__() method.

The operator not yields True if its argument is false, False otherwise.

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or 'foo' yields the desired value. Because not has to create a new value, it returns a boolean value regardless of the type of its argument (for example, not 'foo' produces False rather than ''.)
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
To get the result that you are expecting, you would use bitwise operators.
a = 6
b = 7
print(a & b) #& is bitwise and
Reply
#5
As a matter of fact, using or will give the same result, but I don't think it will be preferred to do so. @TomToad indeed gave the correct and right solution
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with Logical error processing List of strings dmc8300 3 1,083 Nov-27-2022, 04:10 PM
Last Post: Larz60+
  Greedy algorithms on logical problems Opensourcehacker 0 1,533 Nov-22-2020, 05:12 PM
Last Post: Opensourcehacker
  Python greater than equal to comparison operator Ilangos 4 2,419 Sep-26-2020, 03:53 AM
Last Post: buran
  Unable to bit shift and logical OR bytes and ints? MysticLord 7 6,984 Sep-01-2020, 03:31 PM
Last Post: deanhystad
  Basic logical errors cpopescu 3 2,052 Jun-03-2020, 11:30 AM
Last Post: menator01
  Use of & operator, groupby in python abhi1693r 0 1,587 Mar-11-2020, 02:25 PM
Last Post: abhi1693r
  Comparison Operator "is" idle vs python command spisatus 3 2,774 Oct-29-2019, 10:00 PM
Last Post: DeaD_EyE
  parsing logical expression with pyparsing palo173 2 5,473 May-13-2019, 09:22 AM
Last Post: palo173
  Is there any <> operator in python 3.6 rajeev1729 1 3,119 Sep-11-2017, 07:46 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020