Python Lab Manual Answers(22616) Practical 6

 Python Lab Manual Answers



Practical 6: Write Python program to perform following operations on 
Lists: Create list, Access list, Update list (Add item, 
                                    Remove item), Delete list

*Practical Related Question 
1. When to used list
Ans=> 1)Lists are used in python to store data when we need to access them sequentially. 
            2) When we have to perform many operations on elements and access them sequentially.  Then we can use list
 
2. Describe various list functions.
Ans=>
append)It append or additem x to
the end of a list.
insert()Inserts the specified item x at
index position i
extend()It adds the new to the end of an
 existing one.
delThis Python list method removes the
 value at a specified index.
pop()Remove an item at a specified index and
display the removed item.
After removing, the remaining items moved
forward to fill the index gap.
remove()It removes the user-specified item.
It is very useful if we know the item.
copy()This Python List function shallow copies
 the items into a new one.
clear()It clears the existing elements.
count()Counts the number of times the
value is repeated inside it.
reverse()This method reverses the items
2. Write syntax for a method to sort a list
Ans=> 1)Sort Method sort the elements of list in ascending or descending order.
            2) Syntax list.sort(key=, reverse=)
       3)Example:
ist1=[12,8,0,8,34,67,8,10,5]
list1.sort()


3. Write syntax for a method to count occurrences of a list item in Python
Ans=>1)The python count () method counts the number of occurrences of an element in the list and returns it.
           2) Syntax : listname.count(element)
           3) Example :
list1=[12,8,0,8,34,67,8,10,5]
list1.count(8)

#output : 3

4. How to concatenate list
Ans=> 1) we can add two list by concatenation .
             2)concatenation is done using '+ ' operator
             3) Example :
list1=[12,34,67]
list1+[5,6,7]
print(list1)
#output : [12,34,67,5,6,7]


5. Justify the statement “Lists are mutable”
Ans=>1)we can change an item in a list by accessing it directly as part of the assignment statement. Using the indexing operator (square brackets) on the left side of an assignment, we can update one of the list items.
          2) We can add new items in list  and delete any item from list anytime therefore list are mutable

6. Describe the use pop operator in list
Ans=> 1) pop is inbuilt function in python that removes last value from from list 
            2) Syntax : listname.pop(index)
            3) if index is not provided then it removes the last item of list.

*Exercise.

1. Write a Python program to sum all the items in a list.
Ans=>
list=[63,78,90,46,19,20,35,73]
sum=0
for num in range(0,len(list)):
    sum=sum+list[num]
print("Sum of numbers in a list",sum)


2. Write a Python program to multiplies all the items in a list.
Ans=>

list=[10,20]
multiplication=1
for num in range(0,len(list)):
    multiplication=multiplication*list[num]
print("Sum of numbers in a list",multiplication)


3. Write a Python program to get the largest number from a list.
Ans=>

list=[10,34,57,69,27,49]
print("The largest number in a list : ",max(list))


4. Write a Python program to get the smallest number from a list.
Ans=>

list=[67,38,98,26,79,90,16]
print("The smallest number in a list : ",min(list))




5. Write a Python program to reverse a list.
Ans=>

list=[10,20,30,40,50,60,70]
list.reverse()
print("The reverse number in a list : ",list)


6. Write a Python program to find common items from two lists.
Ans=>

list1=[10,35,78,45]
list2=[34,10,78,24]
for i in range(0,len(list1)):
    for j in range(0,len(list2)):
        if(list1[i]==list2[j]):
            print(list[i])



7. Write a Python program to select the even items of a list.
Ans=>

list=[67,54,23,90,14]
for i in list :
    if(i%2==0):
        print("Even no in list",i)



Post a Comment

0 Comments