Python Practice › Loop Programs › Program 88

Maximum number in a list

EasyLoop Programs · 88 of 90

Write a Python program to find the largest number in a list using a loop.

Advertisement
Input
[12, 14, 45, 88, 63, 97, 88]
Output
Maximum number: 97

Approach

  1. Assume the first item is the largest and replace it whenever a bigger one appears.

Solution

Python
numbers = [12, 14, 45, 88, 63, 97, 88]largest = numbers[0]for n in numbers[1:]:    if n > largest:        largest = nprint("Maximum number:", largest)
Output
Maximum number: 97

Line by line

Line 2

Starting from the first item works for negative numbers too, unlike starting from 0.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Leave a Comment