Data Structures Practice Questions
1. Create a list nums = [3, 6, 1, 8, 2, 7]
- Use slicing to extract the middle 4 elements.
- Sort the list using sorted() without changing the original list.
- Reverse the list using slicing.
2. Given colors = ['red', 'green', 'blue'],
- Use insert() to add 'yellow' at index 1.
- Use pop() to remove the last element.
- Use extend() to add ['purple', 'orange'].
3. Create a list with 10 zeros using multiplication.
Example: [0] * 10
4. Use index(), count(), and remove() on a list that has repeating elements:
Example: nums = [2, 4, 4, 6, 2, 8, 2]
5. Given info = ('John', 'Doe', 30, 'Engineer')
- Extract the first and last elements using indexing.
- Use slicing to get the middle part of the tuple.
6. Convert this tuple to a list, modify the second element, and convert it back to a tuple.
7. Create a tuple by unpacking:
Example: a, b, c = (1, 2, 3)
Then print the result of a + c.
8. Create a nested tuple: ('Alice', (90, 85, 88), 'A')
- Access the value 85.
9. Repeat the tuple (1, 2) five times using * operator.
10. Create a dictionary person = {'name': 'Alice', 'age': 25, 'city': 'Delhi'}
- Access the value of 'age' using get().
- Use keys(), values(), and items() to print different views.
11. Add a new key 'email' and update the value of 'city' to 'Mumbai'.
12. Use update() to merge another dictionary: {'country': 'India', 'age': 30}
13. Delete a key 'email' using pop() and clear the dictionary using clear().
14. Create two sets:
set1 = {1, 2, 3, 4} and set2 = {3, 4, 5, 6}
- Perform union, intersection, and difference.
15. Add element 10 to a set and remove element 3.
16. Convert a list with duplicate elements into a set to get unique values:
nums = [1, 2, 2, 3, 3, 3, 4]
17. Create a set from the string 'mississippi' to extract unique characters.