Skip to the content

any, all, map and filter

Python · Unit 10: More on functions

any(items) gives True if at least one value is true; all(items) gives True only if they all are.

They're almost always used with a list of comparisons, which you build with a comprehension.

grades = [8, 5, 9]
low = [g < 6 for g in grades]
print(low)
print(any(low), all(low))

Prints

[False, True, False]
True False

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Predict the output

    What does this code print?

  2. 2. Complete the code

    Fill in the blank to find out whether everyone paid.

  3. 3. Find the case that fails

    passed_all(grades) should return True if every grade is 6 or more, and False if there are no grades at all. Which call breaks it?

  4. 4. Predict the output

    What does this code print?

  5. 5. Complete the code

    The ages came in as text. Fill in the blank to add them up.

  6. 6. Predict the output

    What does this code print?

  7. 7. Find the bug

    This program should print the positive balances. Which line has the bug?

  8. 8. Multiple choice

    You want the prices under 50. Which one reads most clearly?

  9. 9. Write the code

    Write to_ints(texts), which returns a list with each string turned into an integer. Use map.

  10. 10. Predict the output

    One piece of data isn't a number. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →