Variables, Types & Operators · Question 10 · Medium

How many values in the list vals are treated as true by the if test?

vals = [0, "", "0", [], [0], None, 0.0, -1]
print(sum(1 for v in vals if v))
  1. 4
  2. 3
  3. 5
  4. 2
Answer and explanation

Answer: B. 3

Explanation: 0, the empty string, the empty list, None and 0.0 are all false. The non-empty string "0", the non-empty list [0] and the non-zero number -1 are true, so the count is 3.

Previous: Unary minus meets the power operator, with and without brackets. What does… · Next: A voter-age check uses chained comparisons. What does it print for age 21?

Practise variables, types & operators

Try a fresh set of variables, types & operators questions with an explanation after every answer, free and without signing up: Variables, Types & Operators practice. Or sit a full placement mock test.

More variables, types & operators questions

More python practice

Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge