Part 1
1. What is a list in Python and what are some common use cases?
2. What are some common use cases for dictionaries?
3. What is a nested data structure, and how can you access its elements?
4. What are the key differences between a list and a tuple in Python, and when would you
use one over the other?
5. How can you use Python's built-in functions and methods to manipulate and sort lists,
tuples, dictionaries, and sets, and what should you watch out for when working with
these data structures?
Part 2
1. Write a Python program to create a list of integers and then append a new integer to the
end of the list.
2. Write a Python program to create a nested list of strings and then print the first element
of the second list.
3. Write a Python program to create a tuple of integers and then print the length of the
tuple.
4. Write a Python program to create a set of integers and then add a new integer to the set.
5. Write a Python program to create a dictionary of student names and their corresponding
ages, and then print the age of a specific student.
6. Write a Python program that prompts the user for a list of integers and stores them in a
list. For all values that are greater than 100, the string 'over' should be stored instead.
The program should display the resulting list.
7. Write a Python script to concatenate the following dictionaries to create a new one.
Sample Dictionary:
dict1={1:10, 2:20}
dict2={3:30, 4:40}
dict3={5:50, 6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
8. Write a Python program to check if a specific key and a value exist in a dictionary.
students=[
{"student_id": 1, "name": "Jean Castro", "class": "V"},
{'student_id': 2, 'name': 'Lula Powell', 'class': 'V'},
{'student_id': 3, 'name': 'Brian Howell', 'class': 'VI'},
{'student_id': 4, 'name': 'Lynne Foster', 'class': 'VI'},
{'student_id': 5, 'name': 'Zachary Simon', 'class': 'VII'}
]
Your output should as follows:
Key: 'name' and Value: 'Jean Castro' do exist
Key: 'address' and Value: 'New York' do not exist
Part 3
1. Write a Python program that takes in a list of strings and returns a new list with only the
strings that contain the letter 'a'.
2. Write a Python program that takes in two sets of integers and returns a new set with only
the common elements in both sets.
3. Write a Python program that takes in a list of dictionaries representing people with their
age, and returns a new list of dictionaries with only the people over the age of 18.
4. Write a program to input your friend’ names and their Phone Numbers and store them in
the dictionary as the key-value pair. Perform the following operations on the dictionary:
a)Display the name and phone number of all your friends
b)Add a new k:v in this dictionary and display the modified dictionary
c)Delete the particular friend from the dictionary
d)Modify the phone number of an existing friend
e)Check if a friend is present in the dictionary or not and display it in sorted form.
5. Write a Python program to get the top three items in a shop.
Sample data:
{'item1': 45.50, 'item2':35, 'item3': 41.30, 'item4':55, 'item5': 24}
Expected Output:
item4: 55
item1: 45.5
Item3: 41.3
6. You are a teacher and you have a list of dictionaries containing information about your
students. Each dictionary represents a single student and contains the following keys:
"name", "age", "gender", and "grades". The "grades" key points to a list of grades the
student has received in different subjects. Your task is to write a Python program that
calculates the average grade for each student and prints out their name and average
grade.
Here's an example of list of dictionaries to get you started:
students =
[
{"name": "Alice", "age": 17, "gender": "female", "grades": [90, 85, 95]},
{"name": "Bob", "age": 16, "gender": "male", "grades": [80, 75, 70]},
{"name": "Charlie", "age": 16, "gender": "male", "grades": [100, 90, 95]},
{"name": "Diana", "age": 17, "gender": "female", "grades": [85, 80, 90]},
{"name": "Emily", "age": 16, "gender": "female", "grades": [95, 90, 100]}
]
Your program should produce the following output:
Alice: 90.0
Bob: 75.0
Charlie: 95.0
Diana: 85.0
Emily: 95.0
Hint: You'll need to use nested loops to iterate over the list of dictionaries and the list of
grades for each student. You can use the len() function to get the number of grades for
each student.
7. Write a Python program that takes in a list of tuples representing book titles and their
corresponding authors, and returns a new list of tuples sorted by author name in
alphabetical order.
8. Write a Python program to compute the difference between two lists.
Sample data:
["red", "orange", "green", "blue", "white"], ["black", "yellow", "green", "blue"]
Expected Output:
Color1-Color2: ['white', 'orange', 'red']
Color2-Color1: ['black', 'yellow']
Part 4
1. You are developing a simple game where the player needs to select a cell in a 3x3
matrix and reveal its content. Initially, all cells in the matrix are hidden and have a value
of 1. When the player chooses a cell, the value in that cell is revealed and replaced with
an 'X' to indicate that it has been selected. The game continues until all cells have been
selected.
As the developer, you need to implement the game logic in Python. Write a function
play_game() that takes no parameters and returns nothing. The function should create a
3x3 matrix of integers, initialize all cells with a value of 1, and then repeatedly prompt the
player to choose a cell until all cells have been selected. Your 3x3 matrix should initially
should look as shown below:
matrix = [[1, 1, 1],
[1, 1, 1],
[1, 1, 1] ]
When the player chooses a cell, the function should print the updated matrix with the
selected cell marked with an 'X'. If the player chooses a cell that has already been
selected, the function should print an error message and prompt the player to choose
again.
Once all cells have been selected, the function should print a message indicating that
the game is over and the total number of moves made by the player.
Hint: Create a 3x3 matrix of integers, prompt the player to choose a cell and reveal its
content, and keep track of the number of moves until all cells have been selected.
2. Imagine that you are working on a project to assist a nearby library in managing
their book inventory. They want you to design a program that enables them to
add new books, remove old books, and perform advanced book searches. You
make the decision to use a list to store all of the library's books. Moreover, your
book list will be as shown below:
books = [
{"title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "year": 1925},
{"title": "The Hobbit", "author": "J.R.R. Tolkien", "year": 1937},
{"title": "The Lord of the Rings", "author": "J.R.R. Tolkien", "year": 1954},
{"title": "The Da Vinci Code", "author": "Dan Brown", "year": 2003}
]
Task: Create a Python program that prompts user with a dashboard menu as follows:
1. Add a new book
2. Remove a book
3. Search for a book by title
4. Search for a book by author (optional)
5. List all the books
6. Quit
Hint: You may want to use loops in your program and define a function for each task.