Write algorithm to find minimum and maximum number from list in python

Introduction

Python is one of the most widely used programming languages, and for good reason. With its simple syntax, rich set of libraries, and a vibrant community, Python is an ideal choice for a wide range of programming tasks, including data analysis and manipulation. In this blog post, we will explore how to write an algorithm in Python to find the minimum and maximum number from a list.

Table of Contents

1. What is a list in Python?
2. How to create a list in Python?
3. What is the minimum and maximum number in Python?
4. Algorithm to find the minimum and maximum number from a list in Python
5. Python code to find the minimum and maximum number from a list
6. Conclusion

What is a list in Python?

In Python, a list is a collection of items that are ordered and changeable. Lists can contain any type of object, including numbers, strings, and even other lists. Lists are created using square brackets [ ] and each item is separated by a comma.

How to create a list in Python?

To create a list in Python, you simply need to enclose the items in square brackets and separate them by commas. Here's an example:

my_list = [1, 2, 3, 4, 5]

What is the minimum and maximum number in Python?

The minimum and maximum number in Python can be found using the built-in min() and max() functions. The min() function returns the smallest item in an iterable, while the max() function returns the largest item in an iterable.

Algorithm to find the minimum and maximum number from a list in Python:

1. Initialize two variables min_val and max_val with the first element of the list
2. Loop through the remaining elements of the list
3. If an element is smaller than the current value of min_val, update min_val
4. If an element is larger than the current value of max_val, update max_val
5. After the loop completes, return min_val and max_val

Python code to find the minimum and maximum number from a list:

Here's the Python code that implements the above algorithm:

def find_min_max(numbers):
    min_val = numbers[0]
    max_val = numbers[0]
    for num in numbers[1:]:
        if num < min_val:
            min_val = num
        if num > max_val:
            max_val = num
    return min_val, max_val

numbers = [5, 10, 15, 20, 25, 30]
min_val, max_val = find_min_max(numbers)
print("Minimum value:", min_val)
print("Maximum value:", max_val)

Output

Minimum value: 5
Maximum value: 30

Explanation

The program first takes input from the user for the number of elements they want to enter in the list. Then, the program takes the input for each element of the list. Once the list is created, the program uses the built-in min() and max() functions in Python to find the minimum and maximum numbers in the list respectively.

The program then prints out the minimum and maximum numbers in the list as output.

Conclusion

In this blog post, we learned how to write an algorithm to find the minimum and maximum number from a list in Python. We also saw how to implement the algorithm in Python using a simple function. With this knowledge, you can now easily find the minimum and maximum values in any list of numbers using Python.

Advertisements

 

Comments