Problem 190259 · easy · Phase 01 Prerequisites & Setup

Largest in a List

lists · for loop · comparison

Write a function largest(nums) that returns the largest number in the list nums.

Python has a built-in max() that does this, but the point of this exercise is to see how it works inside. Solve it with a loop: look at each number in turn and remember the biggest one you have seen so far.

Examples

Input:  nums = [3, 7, 2]
Output: 7
Input:  nums = [-5, -2, -9]
Output: -2
Explanation: All numbers are negative; -2 is the largest.

Constraints

  • 1 <= len(nums) <= 100 (the list is never empty)
  • -1000 <= nums[i] <= 1000

Goals

  • Loop over every element of a list with for
  • Track a best-so-far value and update it when something better appears
  • Access the first element of a list with index 0
Starting Python…