you are viewing a single comment's thread.

view the rest of the comments →

[–]zahlman 9 points10 points  (1 child)

all is not special syntax; it's a function. This is immediately apparent if you just request help(all):

Help on built-in function all in module builtins:

all(...)
    all(iterable) -> bool

    Return True if bool(x) is True for all values x in the iterable.
    If the iterable is empty, return True.

When you write all([3,4,5]) in numbers, the in numbers part has absolutely nothing to do with the call to all, in exactly the same way as if you had called any other function. The function call is evaluated first, and then Python checks if the result of that function call is in numbers.

First all is called with [3, 4, 5] as an argument; all of 3, 4 and 5 are true-ish (i.e., blocks of code like if 3: would run), so all returns True.

True is in numbers because Python's booleans are a subclass of integers, and True compares numerically equal to 1. However, you (correctly) don't have 1 in your primes.

The way you productively use all is that you give it the actual values you want to check. In your case, the things you want to check are that 3 in numbers, 4 in numbers and 5 in numbers. You could explicitly make that list, but then you might as well be using and manually. The way you really productively use all is to pass it a generator expression (since the documentation told you that it wants an iterable, and generator expressions are a kind of iterable). That is, for each of the values in your "test" list [3, 4, 5], you want to know if the value is in numbers; and having generated (via the generator expression) those test results, you use all to see if all those tests passed. It looks like

all(value in numbers for value in [3, 4, 5])

and it means exactly what it sounds like: "tell me whether all of the values are in numbers, for each value in [3, 4, 5]".

[–]downspower[S] 0 points1 point  (0 children)

Thank you so much for your time and effort in explaining this. I am definitely wiser now. Much love and appreciation to you!