Python Algorithm to find Minimum and Maximum Number from List

Python Algorithm to find Minimum and Maximum Number from List

In this article, we will be solving a very interesting question based on list in Python. According to the question we have to write a Python algorithm to find minimum and maximum number from list.

If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.

Question - Write a Python Algorithm to find Minimum and Maximum Number from List

Python Algorithm to find Minimum and Maximum Number from List


Logic and Explanation

Question - Write a Python Algorithm to find Minimum and Maximum Number from List

Python Algorithm to find Minimum and Maximum Number from List - In this program we have to find the minimum and maximum number from a list, so for this we can write down a simple algorithm in which we will be just iterating through the list. Consider a list L which contains some numbers in it.

L = [40,60,12,88,43,22,75,29]

Here's the algorithm for given question.

  1.  For this we will assume the first number i.e. the number at the index 0 to be the greatest number as well as the smallest number. I know this is not possible but we are just assuming. So, create two variable l_max and l_min and initially assign the value of the number at first index to them.

    l_max = L[0]
    l_min = L[0]

  2. Now, after this we will start iterating through the list from index 0 i.e. the first element to the last element of the list.

    for x in L:

  3. On each iteration, if the current value is less than the l_min, assign the current value to the l_min.

        if x < l_min:
            l_min = x

  4. Similarly, if the current value is greater than the l_max, assign the current value to l_max.

        if x > l_max:
            l_max = x

  5. After iterating through all the elements of the list, the l_min contains the minimum number present in the list L and l_max contains the max element from the list L.

    print("Minimum Number", l_min)
    print("Maximum Number", l_max)

Here's the complete source code for the above program.

L = [40,60,12,88,43,22,75,29]

l_max = L[0]
l_min = L[0]

for x in L:
    if x < l_min:
        l_min = x

    if x > l_max:
        l_max = x

print("Minimum Number", l_min)
print("Maximum Number", l_max)


So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

For More Such Python Programs - Click Here

Enjoy Coding!!!


Also Read :-

Extract Phone Number Details Using Python





For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post