Grade 8_Python
Grade 8_Python
e. To add an element to the end of a list, you use the append() method.
f. The return statement is used to send back a value from a function to the calling
program.
Ans- 1. Function heips in dividing the program into smaller parts. As a program
grows bigger and bigger, functions helps to make it more organized and easy to
manage.
2. It helps to avoid repetition of same code and also makes the code reusable. A
function needs to be defined only once, and can be called by its name anywhere
in any program.
Ans- remove() method is used to delete a particular element from the list by
specifying the element. It searches for the element and removes it from the list. If
there are multiple occurrences, only the first one will be removed.
Pop() method removes an element from the list using its index. If the index is not
specified, it will remove the last element from the list.
Ans- Modules break down the program into smaller, manageable pieces, making
your code more organized and readable.
e. The following program gives the TypeError when it is executed. Why does this
error occur? What change should be made in the program to avoid this error?
Ans- The given program gives TypeError because the index specified in the list is a
float value which is not allowed. We should make following changes to avoid this
error:
Num=[-23,22,20.23,110,-10.98]
print(num[1:3])
print(num[:2])
print(num[2:])
Ans- Output:
Sample list: [‘red’,’yellow’,’blue’,’black’]
Updated list: [‘red’,’black’]
Ans- Output:
Ans- Output:
[ 7, 5 , 4 ,2 ,1 ]
Ans- Output:
20