#π what does and keyword do not in a bool
19 messages Β· Page 1 of 1 (latest)
@dire basin
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.
!e print(None and 5)
print(6 and 5)
@dire basin :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | None
002 | 5
x and y is effectively y if x else x
or makes more sense to me
for and:
- if
xis False, regardless of the value ofy, the result will be False, so it returnsx - if you know that
xis True, then the result will be equal toywhenever it is true or false, so it returnsy
wait what even is the original question
I assume they meant "What does the and and or keywords do when you are not working with booleans"
!e print(False and 6)
@dire basin :white_check_mark: Your 3.12 eval job has completed with return code 0.
False
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
i know useful examples with βorβlike optional parameters but can you give me an example with βandβ
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
!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
@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
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.