nerdexam
Python_Institute

PCEP-30-02 · Question #30

What snippet would you insert in the line indicated below to print. The highest number is 10 and the lowest number is 1. to the monitor? ``python data = [10, 2, 1, 7, 5, 6, 4, 3, 9, 8] insert your…

The correct answer is D. def find_high_low(nums): nums.sort() return nums[-1], nums[0] high, low = find_high_low(data). Option D correctly returns nums[-1] (highest) and nums[0] (lowest) after sorting - because .sort() arranges the list in ascending order, making the last element (nums[-1]) the maximum (10) and the first (nums[0]) the minimum (1), which correctly populates high and low in that…

Question

What snippet would you insert in the line indicated below to print. The highest number is 10 and the lowest number is 1. to the monitor?
data = [10, 2, 1, 7, 5, 6, 4, 3, 9, 8]
# insert your code here
print(
 'The highest number is {} ' +
 'and the lowest number is {}'.format(high, low)
)

Options

  • ANone of the above.
  • Bdef find_high_low(nums): nums.sort() return nums[0], nums[-1] high, low = find_high_low(data)
  • Cdef find_high_low(nums): nums.sort() return nums[len(nums)], nums[0] high, low = find_high_low(data)
  • Ddef find_high_low(nums): nums.sort() return nums[-1], nums[0] high, low = find_high_low(data)

How the community answered

(39 responses)
  • A
    15% (6)
  • B
    8% (3)
  • C
    5% (2)
  • D
    72% (28)

Explanation

Option D correctly returns nums[-1] (highest) and nums[0] (lowest) after sorting - because .sort() arranges the list in ascending order, making the last element (nums[-1]) the maximum (10) and the first (nums[0]) the minimum (1), which correctly populates high and low in that order.

Option B returns nums[0], nums[-1] - the values are swapped relative to D, so high gets 1 and low gets 10, producing the opposite of the desired output.

Option C uses nums[len(nums)] as the first index, which is always one position past the last valid index (for a 10-element list, index 10 doesn't exist), causing an IndexError at runtime.

Option A is eliminated because D works correctly.

Memory tip: After an ascending .sort(), picture the list climbing uphill - the last index (-1) is at the peak (highest), and index 0 is at the bottom (lowest). Return them in the same order your variables expect: high first, low second.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice