code
Python exercise : Find and print max and min elements from given list
Python exercise
Episode: 1
Level: Beginner
Find and print max and min elements from list L=[56, 12, 78, 14, 44, 85, 9] without using max() and min() functions. Use python 3 print command.
One obvious way to find min and max elements from any given list of integers is to use built in Python functions max() and min().
L=[56, 12, 78, 14, 44, 85, 9]
print(max(L))
print(min(L))
This gives 85 and 9.
But how can you solve this problem without using those functions?
Comment your solution.
Expand solution
You can simply sort the list and print first element to get min. To get max you reverse the list and again print the first element.
# print min value
L.sort()
print(L[0])
# print max value
L.reverse()
print(L[0])
I hope this exercise is useful for you if you are just starting with learning Python. Please share this post and come back again for many more exercises. If you have any questions feel free to ask.
Thank you.

Post a Comment
0 Comments
Thanks for sharing your thoughts !