Program which takes list of numbers as input and finds:
a) The largest number in the list
b) The smallest number in the list
c) Product of all the items in the list
tl = int(input('number of element:')) l = [] for i in range(tl): e = eval(input('element:')) l.append(e) large_num = max(l) small_num = min(l) p = 1 for item in l: p = p * item print('largest number in list:', large_num) print('smallest number in list:', small_num) print('product of all items in list:', p)
Output :
number of element: 4
element: 2
element: 5
element: 1
element: 3
largest number in list: 5
smallest number in list: 1
product of all item in list: 30
Explanation
- Prompt for Total Number of Elements (
tl=int(input('number of element:'))
):- This line asks the user to enter the total number of elements they want in the list. The input is converted to an integer and stored in the variable
tl
.
- This line asks the user to enter the total number of elements they want in the list. The input is converted to an integer and stored in the variable
- Initialize an Empty List (
l=[]
):- An empty list
l
is created to store the elements that the user will input.
- An empty list
- Input Elements and Append to List:
- The
for
loop iteratestl
times. In each iteration:- The user is prompted to input an element (
e=eval(input('element:'))
). - The input element
e
is appended to the listl
usingl.append(e)
.
- The user is prompted to input an element (
- The
- Find the Largest and Smallest Numbers:
large_num = max(l)
finds the largest number in the listl
.small_num = min(l)
finds the smallest number in the listl
.
- Calculate the Product of All Elements:
p
is initialized to 1 (p=1
).- The
for
loop iterates through each item in the listl
and multiplies it top
(p = p * item
). This computes the product of all the elements.
- Print the Results:
- The largest number is printed using
print('largest number in list:', large_num)
. - The smallest number is printed using
print("smallest number in list:", small_num)
. - The product of all elements is printed using
print("product of all item in list:", p)
.
- The largest number is printed using