Press ESC to close

Python max number

0 1324

I propose solutions to solve the algorithm that finds in Python the max number among three numbers taken as input, without using lists. As mentioned in previous lessons, there is almost always more than one solution to solve a problem.

Python max number of 3 numbers

Given three integers a, b and c, find the maximum value.

First solution to find the max number of three numbers in Python.

In the first solution I first check:

  • a > b if it’s true I pass to the condition a > c (it is in fact taken for granted that b cannot be the greater):
    • a > c if true, print the value a; otherwise it prints the value c.
  • a > b if it’s false I pass to the condition b > c (it is in fact assumed that a cannot be the greater):
    • b > c if it is true, print the value b; otherwise it prints the value c.


a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))
c = int(input('Enter the third number: '))

if a > b:
  if a > c:
    print('The largest value is a: ', a)
  else:
    print('The largest value is c: ', c)
else:
  if b > c:
    print('The largest value is b: ', b)
  else:
    print('The largest value is c: ', c)

Let’s try it in the compiler:

Second solution max number of 3 numbers in Python

In the second solution I will use the logical operator and, in order to create a more streamlined structure. In fact, if a > b and a > c are both true then a is the greater; otherwise a is certainly not the largest and then just check if b is greater than c and based on this last condition, the largest is established.

So here is a possible solution:


a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))
c = int(input('Enter the third number: '))

if a > b and a > c:
    print('The largest value is a: ', a)
else:
  if b > c:
    print('The largest value is b: ', b)
  else:
    print('The largest value is c: ', c)

Also try this solution in the compiler above.

Third solution to find the max number of 3 numbers in Python, with the use of elif

This solution is similar to the second one but uses the elif statement that we remember stands for else if.


a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))
c = int(input('Enter the third number: '))

if a > b and a > c:
    print('The largest value is a: ', a)
elif b > c:
    print('The largest value is b: ', b)
else:
    print('The largest value is c: ', c)

Check of equality before finding the max

Finally I present a solution where I check if the numbers are all the same and thus display an appropriate message.


a = int(input('Enter the first number: '))
b = int(input('Enter the second number: '))
c = int(input('Enter the third number: '))

if a==b and b==c:
  print('The numbers are the same')
elif a > b and a > c:
    print('The largest value is a: ', a)
elif b > c:
    print('The largest value is b: ', b)
else:
    print('The largest value is c: ', c)

Fourth solution to find the max number of 3 numbers in Python, with the use of elif

Finally, here is a final solution that makes use of the max variable.


a, b, c = int(input('Insert the first number:')), int (input ('Insert the second number:')), int (input ('Insert the third number:'))

max = a

if b> max:
  max = b
if c> max:
  max = c

print('The largest value is:', max)

Useful links

Python tutorial

Python Compiler

Install Python

Variables

Assignment operators

Strings

Casting

How to find the maximum of N numbers

How to use the math module

Bubble sort

Matplotlib Plot

Other stories

Create dictionary in Python

Next Story

Python maximum

Previous Story