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 Min and Max of numbers.

We want you to know about the Min and Max numbers, hence we are come up with the best video explanation you have ever watched on the internet, where you not only understand the logic but also code to get the desired output.

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 Explanation on Python please click over here. Python Practice Series.

Video Explanation to Print Min and Max numbers in Python

Code: Min and Max Numbers in Python

There are various methods to Get the Min and Max of numbers in python. You can find 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

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 which 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 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 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 list.

In the next line, we have a print statement with a message and here we have used the max function and passed 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 you got output, Maximum number is 43.3 and 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.

Facebook Comments
(Visited 1 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *