June 2017
Beginner
352 pages
8h 39m
English
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
Read now
Unlock full access