I Learned Something New About Boolean Checks In Python

preview_player
Показать описание
I learned something new about boolean checks in Python. #python #code #shorts
Рекомендации по теме
Комментарии
Автор

For completeness, it's worth mentioning the any() method that verifies the existence of at least one True item in an iterable.

EdmondKachale
Автор

You can also do wife = input(“yes/no:? “)

Reubenh
Автор

I used it in a similar pattern in a project once, it's a streamlit app deployed for an ML solution

So I used the 'all' keyword to check whether every input box has been filled, otherwise it returns an error message (specified). I used it in a similar way, by making every input a variable and putting them all in an array, it wasn't a waste since I will use the array to take predictions from the ML model

glad to see it's a good practice

abdulsomadabiolajimoh
Автор

all() can be used for and checks
any() can be used for or checks
not all() can be used for none checks

jc_
Автор

Neat. Just coded up some test cases, and it works great.

viatori
Автор

Honestly since match causeway added to Python I've been acting a fool with that function.

Kakashi
Автор

I’m more impressed you found a way to have wife and money True, i can only have 1 true at a time 😂

Moist_elbows
Автор

why don't you do some in java too

naturexmusic
Автор

Warning! There is a major syntactic difference, as logical operators are short-circuited, while any and all NOT:
So:
>>>4 and 5 # short circuit
5
>>>all([4, 5]) # logical eval
True

Moreover, in IPython, all and any maybe shadowed by np.all and np.any (which has bugged me more than once), and then the output is flaky:

>>>5 and np # short-circuited
<module 'numpy' from

>>>np.all([5, np])
<module ...blah blah>

Thus the following (hideous) code works:
>>>np.all([5, np]).nanmean([5, 6, float('nan')])
5.5

works.

DrDeuteron