Welcome to our tutorial on finding the minimum and maximum numbers in Python using a user-defined function!
First, let’s understand the importance of using a user-defined function to find the minimum and maximum numbers. When working with a large dataset, it’s often necessary to find the smallest (minimum) and largest (maximum) numbers in the dataset. Instead of writing the code to find the minimum and maximum numbers every time, we can create a function that can be reused multiple times.
To begin, 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.
Python Program to Find Min and Max’s Numbers Using a User-defined Function
# Min and Max Numbers in Python using a user-defined function # we are taking a number from user as input # entered value will be converted to int from string 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 three 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 three numbers is : ", smallest_num) largest(number1, number2, number3) smallest(number1, number2, number3)
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 (maximum) and smallest (minimum) 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 25, 50, and 75.
Output:
The output is 75 for Largest (maximum) number and 25 for Smallest (minimum) number when using a user-defined function.
Enter First number : 25
Enter Second number : 50
Enter Third number : 75
The largest of the three numbers is : 75
The smallest of the three numbers is : 25
In conclusion, finding the minimum and maximum numbers in Python using a user-defined function is a great way to make your code more efficient and reusable. With the example of 25, 50, and 75, we saw how easy it is to find the minimum and maximum numbers using a user-defined function in Python, and also the importance of creating reusable functions to make our code more efficient. By understanding this concept, you’ll be well on your way to using Python for more advanced data manipulation and analysis tasks.
For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions