-1

I saw somewhere a variable was stored with an and between two variables. Do you know the purpose of this? It seems to only store the second value as the variable, even when the second value is None.

y = 5 and 7
print(y)

7


h = None
y = 5 and h

None  

Is there any purpose for this? I'm wondering why it doesn't just throw an error since the first value seems to always be ignored. Not a duplicate question because no question is asked about storing variables this way and my phrasing is totally different than the supposed duplicate. The founder of Stack Overflow has said even if questions are duplicate if the phrasing is different it is not a duplicate because some questions have to be phrased differently in order for more people to find the answer they are searching for. Supposed duplicate does not ask or answer my questions at all.

grissler
  • 1
  • 2
  • 1
    Try `None and 5` – TheMaster Jan 09 '21 at 16:07
  • Why would it throw an error, `x and y` is a valid expression. `and` would be pretty useless if that threw an error – juanpa.arrivillaga Jan 09 '21 at 16:10
  • Why wouldn't it? That's a valid expression, and you can use the result in an assignment statement. – jonrsharpe Jan 09 '21 at 16:10
  • Someone marked this a duplicate but the question it is an alleged duplicate of barely resembles my question and I have no recourse I guess? This is a disservice to StackOverflow for anyone on the Q who answered and anyone in the future looking for the answers. I have head the SO CEO in interviews say a rephrased question is not a duplicate and my question is more than a rephrasing because it is specifically about variables. I can't even see who did this to my question. – grissler Jan 09 '21 at 22:44

2 Answers2

3

The right-and side of an assignment is an expression. It can be any legal expression.

5 and 7 is a legal expression everywhere in Python, why wouldn't it be a legal expression on the right-hand side of an assignment? It would be a weird exception to the rules, and exceptions to rules are bad, because they make the language harder to learn.

In other words:

This is legal:

print(5 and 7)

So, logically, this should also be legal:

y = 5 and 7
print(y)

Everything else would just be confusing.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
2

It seems to only store the second value as the variable, even when the second value is None.

Try False as the first operand:

False and True
# False

a and b is a regular Python expression that performs short-circuit conjunction of truthy values. It’s mostly useful in a boolean context (as above), but there’s no reason why Python should arbitrarily not allow it to be used in assignments for types other than booleans:

a = None and 'hello'
# a is None.
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Okay, so the first value's existence or nonexistence is a Bool? Makes sense. – grissler Jan 09 '21 at 16:14
  • @grissler "so the first value's existence or nonexistence is a Bool" no it isn't. [Read this relevant section of the docs](https://docs.python.org/3/library/stdtypes.html#truth-value-testing) – juanpa.arrivillaga Jan 09 '21 at 16:25
  • @juanpa.arrivillaga the docs say `x and y: if x is false, then x, else y` I don't understand how the variable can equal x if x is false. I understand how `and` works generally, just not in this case of defining variables. – grissler Jan 09 '21 at 22:11
  • @grissler defining variables is irrelevant. It works the same as `x and y`. **x is not false**. In truth-value testing, **all objects** have a "truth value", i.e. "truthiness". They *behave* as true or false, but they are not `True` or `False` – juanpa.arrivillaga Jan 09 '21 at 22:13
  • @juanpa.arrivillaga In this use when it is stored as a variable, are all but the last elements functioning as anything but bools? – grissler Jan 09 '21 at 22:32