Sequences of booleans

Two other very useful built-ins which facilitate elegant programs are any() and all(). They're equivalent to the logical operators and and or but for iterable series of bool values:

>>> any([False, False, True])
True
>>> all([False, False, True])
False

Here we'll use any() together with a generator expression to answer the question of whether there are any prime numbers in the range 1328 to 1360 inclusive:

>>> any(is_prime(x) for x in range(1328, 1361))
False

For a completely different type of problem we can check that all of these city names are proper nouns with initial upper-case letters:

>>> all(name == name.title() for name in ['London','Paris','Tokyo'])
True
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset