Python · Question 6 · Medium

What will be the output of the following Python code?

print([x * x for x in range(6) if x % 2])
  1. [0, 4, 16]
  2. [1, 9, 25]
  3. [1, 9, 25, 49]
  4. [0, 1, 4, 9, 16, 25]
Answer and explanation

Answer: B. [1, 9, 25]

Explanation: range(6) gives 0 to 5. x % 2 is truthy only for odd x (1, 3, 5), and their squares are 1, 9 and 25.

Previous: Which of the following Python built-in types is immutable? · Next: What will be the output of the following Python code?

Practise python

Try a fresh set of python questions with an explanation after every answer, free and without signing up: Python practice. Or sit a full placement mock test.

More python questions

More technical & programming practice

Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks · Software Engineering

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