Python Practice › Loop Programs › Program 88
Maximum number in a list
Write a Python program to find the largest number in a list using a loop.
Input
[12, 14, 45, 88, 63, 97, 88]
Output
Maximum number: 97
Approach
- Assume the first item is the largest and replace it whenever a bigger one appears.
Solution
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.