Min and Max of Numbers in Python

Here we discuss a simple python program which finds the Min and Max number out of given numbers.Â The logic of this program is also very simple. In this blog, we will see different methods and types of getting the Min and Max of numbers.

Do you still face difficulties in understanding the logic? Thatâ€™s perfectly fine; we have something more for you. You can scroll down to learn more about this topic.

For all the practice Videos and Explanations on Python, Check out Python Programming Exercises and Solutions.

Program to check Min and Max Numbers in Python

There are various methods to Get the Min and Max of numbers in python. You can find the source code and an explanation of different methods over here. Letâ€™s start with our program here.

Method 1: Min and Max Numbers in Python using a user-defined function

Source Code and Output

```number1 = int(input('Enter First number : '))
number2 = int(input('Enter Second number : '))
number3 = int(input('Enter Third number : '))

def largest(num1, num2, num3):
if (num1 > num2) and (num1 > num3):
largest_num = num1
elif (num2 > num1) and (num2 > num3):
largest_num = num2
else:
largest_num = num3
print("The largest of the 3 numbers is : ", largest_num)

def smallest(num1, num2, num3):
if (num1 < num2) and (num1 < num3):
smallest_num = num1
elif (num2 < num1) and (num2 < num3):
smallest_num = num2
else:
smallest_num = num3
print("The smallest of the 3 numbers is : ", smallest_num)

largest(number1, number2, number3)
smallest(number1, number2, number3)```
Output:
``````Enter First number : 5
Enter Second number : 4
Enter Third number : 6
The largest of the 3 numbers is :  6
The smallest of the 3 numbers is :  4``````

You can find the video on YouTube as wellâ€“ Maximum and Minimum of 3 Numbers in Python

Code Explanation Method 1: Min and Max Numbers in Python using a user-defined function

Letâ€™s see how this program works.

Our Approach is:

• Check if the first element is smaller than the second and third. If yes, then print it.
• Else check if the second element is smaller than the first and third. If yes, then print it.
• Else third is the smallest element and print it.

Here at the first we have defined 3 variables and assigned values that we are taking from the user. We can also define static variables, but dynamic is useful in big projects. In this program, we have called two functions largest and smallest and pass the same variable which we have accepted from the user. Letâ€™s check the 1st function which is the largest(); this function accepts 3 parameters.

Inside this function, we have an if condition which checks if num1 is greater than number 2 and num1 is greater than num3 which means if both the conditions are true then num1 is greater than the other two.

If the above condition is not satisfied then we will check if num2 is greater than num1 and num3. If this condition is also false then num3 is the largest and this function will print the largest number with a message. Also in the case of the smallest number, this function also accepts three numbers and inside this function, we have a condition to check.

If num1 is less than the other two numbers. If this condition is true, then we will assign the value of variable num1 to a variable smallest_num. In case if this condition is false then the program will move to the elif condition and check further conditions and print the smallest number among all 3 with a message. Letâ€™s run this program and enter numbers 5, 4, and 6. See we have our output max number as 6 and minimum number as 4.

Method 2: Minimum and Maximum of a list of numbers in Python

Source Code and Output

```m = [4,12,43.3,19,2]
print("Maximum number is : ",max(m))
print("Minimum number is : ",min(m))```
Output
``````Maximum number is : 43.3
Minimum number is : 2``````

Code Explanation Method 2: Minimum and Maximum of a list of numbers in Python

This blog brings you a very interesting function of Python, namely max() and min().Â

• max() â€“ This function is used to compute the maximum of the values passed in its argument and lexicographically largest value if strings are passed as arguments.
• min() â€“ This function is used to compute the minimum of the values passed in its argument and lexicographically smallest value if strings are passed as arguments.

See the magic of predefined functions. If you compare our previous function with this function, you will get to know that we have reduced lines of code. Here we have defined a list with the name m and assigned value to the list. In the next line, we have a print statement with a message, and here, we have used the max function and passed the variable m, which returns the max number from the list.

Then in the next line, another print statement with the min function will return the minimum number from the list. Letâ€™s run this program and see if you got the output, Maximum number is 43.3, and the minimum number is 2.

I hope now you know how to get the minimum and maximum number with the help of user-defined functions as well as predefined functions.