Python Lab Ex 4 Ex 5
Python Lab Ex 4 Ex 5
#1
my_list = []
#2
count = int(input("How many numbers you want to add : "))
#3
for i in range(1,count+1):
my_list.append(int(input("Enter number {} : ".format(i))))
#4
print("Input Numbers : ")
print(my_list)
#5
min = my_list[0]
max = my_list[0]
#6
for no in my_list:
if no < min : min = no
elif no > max :
max = no
#7
print("Minimum number : {}, Maximum number : {}".format(min,max))
1. Create one empty list mylist. We are using one _empty square bracket to
create the empty list. This is a list without any items.
2. Get the total number of elements the user is going to enter and save it
in the count variable. This is required because we will ask the user to
enter each number one by one. If the value of count is 4, the user will
have to enter four numbers to add to the list.
3. Using a_ for loop, get the numbers and append it to the list _mylist. For
appending a number, _append _method is used. This method takes the
value we are adding as the parameter. For reading the value, we are using
the _input _method. This method will read the value from the user. The
return value of this method is of _string _type. Wrapping it as int()_ will
convert the entered value to an integer.
4. Print the list to the user.
5. Create two variables to hold the minimum and maximum number. We
are assigning the first element of the list to both of these variables first.
We will update these variables on the next step. On this step, we are
assuming that the minimum and maximum value of the list is equal to
the first element of the list. We will compare this value with all other
elements of the list one by one and update them if required.
6. Run one for the loop on the list again. For each number, check if it is less
than the minimum number. If yes, assign the minimum value holding
variable to this number. Similarly, update the maximum value if the
number is more than the current maximum.
7. After the list is completed reading, print out both maximum and
minimum numbers
PYTHON PROGRAM TO FIND LARGEST OF N NUMBERS USING MAX FUNCTION
NOTE: THIS PROGRAM SHOULD BE RUN THROUGH ONLINE COMPILER
Take input number for the length of the list using python
input() function.
Initialize an empty list lst = [].
Read each number in your python program using a for loop.
In the for loop append each number to the list.
Use built-in python function max() to find the largest
element in a list.
End of the program print the largest number from list.
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
arr = ['t','u','t','o','r','i','a','l']
x = 'a'