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 listAns=> 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. del This 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 listAns=> 1)Sort Method sort the elements of list in ascending or descending order. 2) Syntax : list.sort(key=, reverse=) 3)Example:
3. Write syntax for a method to count occurrences of a list item in PythonAns=>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 :
4. How to concatenate listAns=> 1) we can add two list by concatenation . 2)concatenation is done using '+ ' operator 3) Example :
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 listAns=> 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=>sum=0for 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=>
3. Write a Python program to get the largest number from a list.Ans=>
4. Write a Python program to get the smallest number from a list.Ans=>
5. Write a Python program to reverse a list.Ans=>
6. Write a Python program to find common items from two lists.Ans=>
7. Write a Python program to select the even items of a list.Ans=>
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. |
del | This 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:
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 :
4. How to concatenate list
Ans=> 1) we can add two list by concatenation .
2)concatenation is done using '+ ' operator
3) Example :
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=>
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=>
3. Write a Python program to get the largest number from a list.
Ans=>
4. Write a Python program to get the smallest number from a list.
Ans=>
5. Write a Python program to reverse a list.
Ans=>
6. Write a Python program to find common items from two lists.
Ans=>
7. Write a Python program to select the even items of a list.
Ans=>
0 Comments