0% found this document useful (0 votes)
13 views3 pages

Python Data Structures Tutorial Tasks

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Python Data Structures Tutorial Tasks

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Dr.

Mai Cao Lan GE1027 - Data Science

GE1027 - Data Science


Semester: 251
Chapter 2 (Part B) – Tutorial: Built-in Data Structures
This tutorial provides 16 programming tasks focusing on the fundamental operations
of Python’s built-in data structures (lists, tuples, sets, and dictionaries). Each data
structure has 4 characteristic tasks that demonstrate its core features and typical use
cases.

LIST Tasks (4 problems)


1. Element Search and Position: Given a list of integers, write a function that
finds the first occurrence index of a target element. Return -1 if not found.

Test input: [3, 7, 1, 9, 4, 7, 2], target: 7 Output: 1

2. Element Insertion and Removal: Write a function that inserts a new element
at a specific position and removes all occurrences of a target element.

Input: [“apple”, “banana”, “cherry”, “banana”], insert: “orange”, pos: 2, remove: “banana”

Output: [“apple”, “orange”, “cherry”]

3. List Slicing and Reversal: Create a function that returns every second element
starting from index 1, but in reverse order.

Test input: [1, 2, 3, 4, 5, 6, 7, 8] Output: [8, 6, 4, 2]

4. List Sorting with Custom Criteria: Given a list of student tuples (name, score),
return the top 3 students with highest scores.

Input: [(“Alice”, 85), (“Bob”, 92), (“Charlie”, 78), (“Diana”, 96), (“Eve”, 89)]

Output: [(“Diana”, 96), (“Bob”, 92), (“Eve”, 89)]

TUPLE Tasks (4 problems)


5. Tuple Unpacking and Access: Given a tuple containing student information
(name, age, grade, subjects list), return a formatted string.

Input: (“John”, 16, “A”, [“Math”, “Science”, “English”])

Output: “John (age 16) has grade A in 3 subjects: Math, Science, English”

6. Tuple Concatenation and Repetition: Create a path by concatenating two


coordinate tuples, then repeat it a specified number of times.

Input: (1, 2), (3, 4), repeat: 2 Output: (1, 2, 3, 4, 1, 2, 3, 4)

Page 1 of 3
Dr. Mai Cao Lan GE1027 - Data Science

7. Tuple as Dictionary Key: Create a dictionary using coordinate tuples as keys


and find the value for a specific coordinate.

Coords: [(1, 2), (3, 4), (5, 6)], values: [10, 20, 30], lookup: (3, 4) Output: 20

8. Tuple Counting and Index Finding: Return both the count of an element and
all indices where it appears.

Input: (1, 2, 3, 2, 4, 2, 5), element: 2 Output: (3, [1, 3, 5])

SET Tasks (4 problems)


9. Set Union and Intersection: Find colors liked by both groups (intersection) and
all colors liked by either group (union).

Group1: {“red”, “blue”, “green”}, Group2: {“blue”, “yellow”, “red”}

Output: ({“red”, “blue”}, {“red”, “blue”, “green”, “yellow”})

10. Set Difference and Symmetric Difference: Compare two skill sets and return
unique skills in each set and symmetric difference.

Skills1: {“Python”, “Java”, “SQL”}, Skills2: {“Java”, “JavaScript”, “HTML”}

Output: ({“Python”, “SQL”}, {“JavaScript”, “HTML”}, {“Python”, “SQL”, “JavaScript”, “HTM

11. Set Membership and Subset Operations: Check if a developer has required
skills and identify missing ones.

Dev skills: {“Python”, “JavaScript”}, Required: {“Python”, “Java”, “SQL”}

Output: (False, {“Java”, “SQL”})

12. Set Creation and Deduplication: Extract unique email domains from a list and
count them.

Input: [“user1@[Link]”, “user2@[Link]”, “user3@[Link]”, “user4@[Link]”]

Output: ({“[Link]”, “[Link]”, “[Link]”}, 3)

DICTIONARY Tasks (4 problems)


13. Dictionary Access and Key Management: Safely access dictionary values with
default values for missing keys.

Dict: {“name” : “John”, “age” : 25, “city” : “NYC”}, Keys: [“name”, “salary”, “age”]

Default: “Unknown” Output: [“John”, “Unknown”, 25]

Page 2 of 3
Dr. Mai Cao Lan GE1027 - Data Science

14. Dictionary Update and Merging: Merge two inventory dictionaries, summing
quantities for common products.

Store1: {“apples” : 50, “bananas” : 30, “oranges” : 20}

Store2: {“bananas” : 25, “oranges” : 15, “grapes” : 40}


Output: {“apples” : 50, “bananas” : 55, “oranges” : 35, “grapes” : 40}

15. Dictionary Iteration and Filtering: Filter student grades, returning only pass-
ing students (grade ≥ 60) with letter grades.

Input: {“Alice” : 85, “Bob” : 55, “Charlie” : 92, “Diana” : 45, “Eve” : 78}

Output: {“Alice” : “B”, “Charlie” : “A”, “Eve” : “C”}


Grade scale: A(90+), B(80-89), C(70-79), D(60-69), F(¡60)

16. Dictionary Comprehension and Value Transformation: Convert prices to


different currency and apply discount.

Products: {“laptop” : 1000, “mouse” : 25, “keyboard” : 75}

USD to EUR: 0.85, Discount: 10%


Output: {“laptop” : 765.0, “mouse” : 19.125, “keyboard” : 57.375}

Page 3 of 3

You might also like