Maximum of three numbers

EasyFunction Programs · 4 of 39

Write a Python function to find the maximum of three numbers.

Advertisement
Input
17, 21, -9
Output
21

Approach

  1. Reuse a two-number max function twice.

Solution

Python
def max_of_two(a, b):    return a if a > b else b def max_of_three(a, b, c):    return max_of_two(a, max_of_two(b, c)) print(max_of_three(17, 21, -9))
Output
21

Line by line

Lines 4-5

Building on a smaller function keeps each one simple.

Learn the concept

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

More function programs

← All 39 Function Programs

Leave a Comment