List Assignment 2
Instructions: Write answers to these questions in jupyter notebook and submit the
assignment in this drive upload_link by naming it as
“python_list_assignment_batch_name_your_name.ipynb”
# append() method
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)
# Question 1: Append the string 'a' to my_list and print the list.
# Question 2: Append the list [5, 6] to my_list and print the list.
# Question 3: Create an empty list and append the number 1 to it. Print the list.
# Question 4: Append the same element twice to my_list and print the list.
# Question 5: Append a tuple (7, 8) to my_list and print the list.
# clear() method
my_list = [1, 2, 3, 4]
my_list.clear()
print(my_list)
# Question 1: Create a list with elements and use clear() method. Print the list after clearing
it.
# Question 2: Create a list, clear it, and then append a new element. Print the list.
# Question 3: Clear a list and check its length.
# Question 4: Create a list, clear it, and then try to access the first element. What happens?
# Question 5: Create a list, clear it, and then append multiple elements to it. Print the list.
# copy() method
original_list = [1, 2, 3]
copied_list = original_list.copy()
copied_list.append(4)
print("Original List:", original_list)
print("Copied List:", copied_list)
# Question 1: Create a list and make a copy of it. Print both lists.
# Question 2: Copy a list and modify the copy. Print both lists to show they are different.
# Question 3: Create a nested list, make a copy, and modify the nested list in the copy. Print
both lists.
# Question 4: Copy an empty list and append an element to the copy. Print both lists.
# Question 5: Copy a list with mixed data types and modify the copy. Print both lists.
# count() method
my_list = [1, 2, 3, 2, 2, 4, 5]
count_of_twos = my_list.count(2)
print(count_of_twos)
# Question 1: Create a list with repeated elements and count the occurrences of a specific
element.
# Question 2: Create a list with strings and count the occurrences of a specific string.
# Question 3: Create a list with mixed data types and count the occurrences of a specific
type.
# Question 4: Count the occurrences of an element that is not in the list.
# Question 5: Count the occurrences of an element in an empty list.
# extend() method
my_list = [1, 2, 3]
another_list = [4, 5, 6]
my_list.extend(another_list)
print(my_list)
# Question 1: Create two lists and use extend() to combine them. Print the result.
# Question 2: Extend a list with a string and print the result.
# Question 3: Extend a list with a tuple and print the result.
# Question 4: Extend a list with another list containing different data types and print the
result.
# Question 5: Extend an empty list with a non-empty list and print the result.
# index() method
my_list = [10, 20, 30, 40, 50]
index_of_30 = my_list.index(30)
print(index_of_30)
# Question 1: Find the index of the element 40 in my_list and print it.
# Question 2: Find the index of the first occurrence of the element 20 in a list with repeated
elements.
# Question 3: Create a list and find the index of an element not present in the list. What
happens?
# Question 4: Create a list with strings and find the index of a specific string.
# Question 5: Create a mixed data type list and find the index of a specific element.
# insert() method
my_list = [1, 2, 3]
my_list.insert(1, 'a')
print(my_list)
# Question 1: Insert the number 10 at the beginning of my_list and print the list.
# Question 2: Insert the string 'b' at the end of my_list and print the list.
# Question 3: Insert the list [4, 5] at index 2 and print the list.
# Question 4: Insert an element at a negative index in the list.
# Question 5: Insert an element at an index greater than the length of the list and print the
list.
# pop() method
my_list = [1, 2, 3, 4, 5]
popped_element = my_list.pop(2)
print(popped_element)
print(my_list)
# Question 1: Pop the last element from my_list and print it.
# Question 2: Pop an element from the beginning of my_list and print the list.
# Question 3: Create a list, pop an element from the middle, and print both the popped
element and the list.
# Question 4: Pop an element using a negative index and print the list.
# Question 5: Try to pop an element from an empty list. What happens?
# remove() method
my_list = [1, 2, 3, 2, 4, 2]
my_list.remove(2)
print(my_list)
# Question 1: Remove the first occurrence of the element 3 from my_list and print the list.
# Question 2: Remove an element not present in the list. What happens?
# Question 3: Create a list with repeated elements, remove one occurrence, and print the
list.
# Question 4: Remove a string element from a list of strings and print the list.
# Question 5: Create a mixed data type list, remove an element, and print the list.
# reverse() method
my_list = [1, 2, 3, 4, 5]
my_list.reverse()
print(my_list)
# Question 1: Create a list, reverse it, and print the reversed list.
# Question 2: Reverse a list with strings and print the result.
# Question 3: Reverse an empty list and print the result.
# Question 4: Reverse a list with mixed data types and print the result.
# Question 5: Reverse a list twice and print the result.
# sort() method
my_list = [3, 1, 4, 2, 5]
my_list.sort()
print(my_list)
# Question 1: Create a list and sort it in ascending order. Print the sorted list.
# Question 2: Sort a list of strings and print the result.
# Question 3: Sort a list in descending order and print the result.
# Question 4: Create a list with negative and positive numbers, sort it, and print the result.
# Question 5: Sort a list with repeated elements and print the result.