Write the Python statement for each of the following tasks using BUILTIN functions/methods only:
(i) To insert an element 200 at the third position, in the list L1.
Answer
Answer by student
The Python statement for inserting an element 200 at the third position in the list L1 is:
L1.insert(2,200)
Detailed answer by teachoo
To write the Python statement for inserting an element 200 at the third position in the list L1, we need to understand the following concepts:
- Lists are collections of values that can be of different types. For example, L1=[10,20,30,40] is a list of four integers. We can also access the elements of a list by using indexing, similar to strings. For example, L1[0] is 10, L1[1] is 20, and so on.
- insert() is a method that inserts an element at a specified position in a list. The syntax of insert() is list.insert(index,element) ,
- where index is the position where the element will be inserted
- element is the value to be inserted.
- The index starts from 0, so the first position is 0, the second position is 1, and so on.
Therefore, the Python statement for inserting an element 200 at the third position in the list L1 is:
L1.insert(2,200)