Exercise questions on data structures
Exercise questions on data structures
Lists
1. Create a list with the numbers 1 to 10 and print it.
2. Append the number 11 to the list and print the updated list.
3. Insert the number 100 at the second position in the list.
4. Remove the number 5 from the list.
5. Reverse the list and print the result.
6. Sort the list in descending order.
7. Find the maximum and minimum values in the list.
8. Write a program to remove duplicate elements from a list.
9. Create a list of 5 names and print them in uppercase.
10.Slice the list to get the first three elements.
11.Find the index of number 7 in the list.
12.Use a list comprehension to generate a list of squares of numbers from 1 to
10.
13.Use a list comprehension to filter out even numbers from a list.
14.Write a program to find the second-largest number in a list.
15.Convert a list into a tuple.
16.Merge two lists without using the + operator.
17.Write a program to count the frequency of each element in a list.
18.Write a function that returns the cumulative sum of a list.
19.Write a program to find the common elements between two lists.
20.Find the difference between two lists.
Dictionaries
1. Create a dictionary with keys as names and values as ages.
2. Add a new key-value pair: "city": "New York".
3. Update the age of a specific person in the dictionary.
4. Remove an entry from the dictionary.
5. Write a program to check if a key exists in a dictionary.
6. Write a program to print all keys and values separately.
7. Write a program to merge two dictionaries.
8. Convert a dictionary into a list of (key, value) tuples.
9. Write a function that returns the key with the highest value in a dictionary.
10.Find the sum of all values in a dictionary.
11.Write a program to sort a dictionary by keys.
12.Write a program to sort a dictionary by values.
13.Convert a list of tuples into a dictionary.
14.Write a program to invert a dictionary (swap keys and values).
15.Remove duplicates from a dictionary.
16.Write a function to find common keys between two dictionaries.
17.Write a program to count the occurrences of each word in a sentence.
18.Create a nested dictionary and retrieve values from it.
19.Convert two lists into a dictionary.
20.Write a program to create a dictionary where keys are numbers from 1 to
5 and values are their squares.
Sets
1. Create a set of the first 5 natural numbers.
2. Add a number to the set and print the result.
3. Remove an element from the set.
4. Find the union of two sets.
5. Find the intersection of two sets.
6. Find the difference between two sets.
7. Find the symmetric difference of two sets.
8. Check if a set is a subset of another.
9. Check if a set is a superset of another.
10.Convert a list with duplicate elements into a set.
11.Find the number of elements in a set.
12.Remove an element from a set using discard().
13.Write a program to check if two sets are disjoint.
14.Find the smallest and largest elements in a set.
15.Create a frozen set and attempt to modify it.
16.Write a function that returns common elements in multiple sets.
17.Write a program to check if a value exists in a set.
18.Convert a dictionary’s keys into a set.
19.Convert a string into a set of unique characters.
20.Write a program to remove all even numbers from a set.
Tuples
1. Create a tuple with 5 elements.
2. Try to modify an element in a tuple (observe the error).
3. Access the third element of a tuple.
4. Find the length of a tuple.
5. Convert a tuple into a list.
6. Convert a list into a tuple.
7. Check if an element exists in a tuple.
8. Find the index of an element in a tuple.
9. Count how many times an element appears in a tuple.
10.Write a program to unpack a tuple into variables.
11.Create a tuple of only one element.
12.Concatenate two tuples.
13.Multiply a tuple by a number.
14.Convert a tuple into a dictionary.
15.Write a program to iterate through a tuple using a loop.
16.Find the maximum and minimum values in a tuple.
17.Write a program to merge two tuples without using +.
18.Write a program to find the common elements between two tuples.
19.Write a program to find the difference between two tuples.
20.Convert a tuple into a string.