Python program to find the greatest of three numbers
In this tutorial you will learn Python program to find the greatest of three numbers using three approaches like if elif, using list and built-in function max(). Let’s learn these approaches with examples.
Python program to find the greatest of three numbers
Given three numbers a,b and c. The task is to find the greatest of three numbers using Python programming language.
Method 1: Using if elif else
In the below program, the three numbers are stored in a,b and c variables. And we are going to use if elif else logic to find the greatest among three numbers and print the output.
For example, the following are the given numbers.
a=5 b=10 c=7
Below is the Python program to find the largest of three given numbers.
>>> def maximum(a,b,c): ... if (a>=b) and (a>=c): ... greatest = a ... elif (b>=a) and (b>=c): ... greatest = b ... else: ... greatest = c ... return greatest
Let’s try to call maximum method and print the output.
>>> print(maximum(a,b,c))
Output
10
Using List and max() built-in function
The below example, initializes three variables x,y and z with values 5,10 and 7. Then add those three numbers in to a list. Finally, you can find the largest number using built-in python function max() as shown below.
>>> x=5
>>> y=10
>>> z=7 >>> def maximum(x,y,z): ... list=[x,y,z] ... return max(list) >>> maximum(x,y,z) 10
Using max() built-in function
In this approach, you will be using only the max() built-in function. And you need to pass numbers as arguments and it return the largest item of two or more arguments as shown below.
>>> max (x,y,z) 10
That’s it. You have learnt how to find the greatest of three given numbers using Python programming using different approaches.
Hope it helped 🙂
You’ll also like:
- Convert floating point number to fixed point in Python
- What is %matplotlib inline and how to use ?
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Reset jupyter notebook theme to default theme
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- Run a Jupyter Notebook .ipynb file from terminal or cmd prompt
- Amazon Linux AMI : apt-get command not found
- Linux: sudo: apt-get: command not found
- Check if a file or folder exists without getting exceptions – Python
- Putty Fatal Error No supported authentication methods available
- Find which users belongs to a specific group in linux
- Embed HTML within IPython Notebook
- Check if Python Object is a Number ?
- Error: helm install unknown flag: –name
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- TypeError: a bytes-like object is required, not ‘str’ – Python3
- Extract numbers from a string in python