Python Basic: Exercise-148 with Solution
Write a Python function to find the maximum and minimum numbers from a sequence of numbers.
Note: Do not use built-in functions.
Sample Solution :-
Python Code :
def max_min[data]:
l = data[0]
s = data[0]
for num in data:
if num> l:
l = num
elif num< s:
s = num
return l, s
print[max_min[[0, 10, 15, 40, -5, 42, 17, 28, 75]]]
Sample Output:
[75, -5]
Pictorial Presentation:
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor :
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a Python function to check whether a number is divisible by another number. Accept two integers values form the user.
Next: Write a Python function that takes a positive integer and returns the sum of the cube of all the positive integers smaller than the specified number.
We are given a list of numbers and our task is to write a Python program to find the smallest number in given list. For the following program we can use various methods including the built-in min method, sorting the array and returning the last element, etc.
Example:
Input : list1 = [10, 20, 4] Output : 4 Input : list2 = [20, 10, 20, 1, 100] Output : 1
Sorting the list to find smallest number in a list
In Ascending order
Here writing a Python program where we are sorting the entire list and then returning the first element as it’ll be the smallest element present in the list.
Python3
list1
=
[
10
,
20
,
4
,
45
,
99
]
list1.sort[]
print
[
"Smallest element is:"
, list1[
0
]]
Output:
smallest element is: 4
In Descending order
Here we are sorting using the sort[] function the entire list and then returning the last element as it’ll be the smallest element present in the list.
Python3
list1
=
[
10
,
20
,
4
,
45
,
99
]
list1.sort[reverse
=
True
]
print
[
"Smallest element is:"
, list1[
-
1
]]
Output:
smallest element is: 4
Using min[] Method to find smallest number in a list
Here we are using the min Method and then returning the smallest element present in the list.
Python3
list1
=
[
10
,
20
,
1
,
45
,
99
]
print
[
"Smallest element is:"
,
min
[list1]]
Output:
Smallest element is: 1
Find minimum list element for a user defined list
Python3
list1
=
[]
num
=
int
[
input
[
"Enter number of elements in list: "
]]
for
i
in
range
[
1
, num
+
1
]:
ele
=
int
[
input
[
"Enter elements: "
]]
list1.append[ele]
print
[
"Smallest element is:"
,
min
[list1]]
Output:
Enter number of elements in list: 4 Enter elements: 12 Enter elements: 19 Enter elements: 11 Enter elements: 99 Smallest element is: 11
Find the smallest element in list comparing every element
Python3
l
=
[
int
[l]
for
l
in
input
[
"List:"
].split[
","
]]
print
[
"The list is "
,l]
min1
=
l[
0
]
for
i
in
range
[
len
[l]]:
if
l[i] < min1:
min1
=
l[i]
print
[
"The smallest element in the list is "
,min1]
Input:
List: 23,-1,45,22.6,78,100,-5
Output:
The list is ['23', '-1', '45', '22.6', '78', '100','-5'] The smallest element in the list is -5
Using the lambda function to find smallest number in a list
Here we are using the lambda function to print the smallest number present in the list.
Python3
lst
=
[
20
,
10
,
20
,
1
,
100
]
print
[
min
[lst, key
=
lambda
value:
int
[value]] ]
Output:
1
Using the enumerate function to find smallest number in a list
Here we are iterating over the list using the enumerate[] function and returning the last element.
Python3
lst
=
[
20
,
10
,
20
,
1
,
100
]
a,i
=
min
[[a,i]
for
[i,a]
in
enumerate
[lst]]
print
[a]
Output:
1