#πŸ”’ what does and keyword do not in a bool

19 messages Β· Page 1 of 1 (latest)

white glenBOT
#

@dire basin

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dire basin
#

!e print(None and 5)
print(6 and 5)

white glenBOT
#

@dire basin :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | None
002 | 5
twilit tinsel
#

x and y is effectively y if x else x

dire basin
#

or makes more sense to me

twilit tinsel
#

for and:

  • if x is False, regardless of the value of y, the result will be False, so it returns x
  • if you know that x is True, then the result will be equal to y whenever it is true or false, so it returns y
green acorn
#

wait what even is the original question

twilit tinsel
dire basin
#

!e print(False and 6)

white glenBOT
#

@dire basin :white_check_mark: Your 3.12 eval job has completed with return code 0.

False
copper copper
#

Both and and or return first element that definitely shows the logical value.

So in case of or it means first "truthy" value or last value
And in case of and it's first "falsy" value or last value

Because or "becomes" True with only one thing being True/truthy. While and needs all truthy to be True - so any False/falsy before that defines the whole thing will "be" false

dire basin
#

i know useful examples with β€œor”like optional parameters but can you give me an example with β€œand”

copper copper
#

I don't think it's used much in practice, it just exists here for compatibility reasons - we have or, so we also have and

twilit tinsel
#

!e sometimes you might see it being used for things like py for value in ('hello', None, 'world'): print(value and value.upper()) for value in ('hello', None, 'world'): print(value or "missing") but I'd recommend just using explicit if statements instead

white glenBOT
#

@twilit tinsel :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | HELLO
002 | None
003 | WORLD
004 | hello
005 | missing
006 | world
dire basin
#

thanks

#

!close

white glenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.