need help with python.. please paste ur working and indented code here Write one line of code for each of the following: 1) Create a list named fruits with the following items: "apple", "cherry", "pear" 2) Add "orange" at the end of the fruits list. 3) Change the value from "cherry" to "banana", in the fruits list. 4) Use the insert method to add "lemon" as the third item in the fruits list. 5) Use slicing to print the third, fourth, and fifth items in the current fruits list. 6) Use the del function to remove "banana" from the current fruits list. 7) Use negative indexing to print "pear" in the current fruits list.
need help with python.. please paste ur working and indented code here
Write one line of code for each of the following:
1) Create a list named fruits with the following items:
"apple", "cherry", "pear"
2) Add "orange" at the end of the fruits list.
3) Change the value from "cherry" to "banana", in the fruits list.
4) Use the insert method to add "lemon" as the third item in the fruits list.
5) Use slicing to print the third, fourth, and fifth items in the current fruits list.
6) Use the del function to remove "banana" from the current fruits list.
7) Use negative indexing to print "pear" in the current fruits list.
List: To storing multiple items in a single variable we can use a list, we can also say it is one type of array but the list has no particular data type so we can store any mixed data into it.
Synatx:
varibale_name = [element1, element2, ...] |
Adding Item to list:
We can use multiple ways to add items to the list.
- By using append() method:
append() method: append method is used to add an item or element to the end of the list.
Syntax:
list_name.append(element) |
- By using insert() method:
insert() method: insert method is used to add an item or element to the specific index of the list.
Syntax:
list_name.insert(index, element) |
Slicing list:
we can slice the list by using the slice() method.
Syntax:
list_name[initial: End: number_of_index_jump] |
deleting element from the list:
we can delete elements at a particular index from the list using the del keyword.
Syntax of del for list:
del list_name[index] |
we can also remove elements from a list by the value of the element itself using the remove() method.
Syntax:
list_name.remove(element_value) |
Step by step
Solved in 4 steps with 1 images