Lab 7
Python Strings and Text files (Part I)
1. Create a list of words by asking the user, then generate a dictionary where the keys are the first
letter of the word.
2. Reverse the words in a sentence while keeping the words themselves in the original order.
3. WAP to ask the user to input a sentence, then create a dictionary where each word is a value,
and its length is the key.
4. Given a list of sentences, create a dictionary where the keys are the lengths of the sentences,
and the values are sets of words in those sentences.
Example: sentences = ["The quick brown fox", "jumps over the lazy dog", "Hello world"]
Output : {19: {'quick', 'fox', 'The', 'brown'}, 24: {'jumps', 'lazy', 'dog', 'the', 'over'}, 11: {'Hello',
'world'}}
5. Create a dictionary where the keys are unique words in a list, and the values are lists of indices
where each word appears.
Example: words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
Output: {'apple': [0, 2, 5], 'banana': [1, 4], 'orange': [3]}
Lab 8
Python Strings and Text files (Part I)
6. Count the frequency of each word in a text file and store it in a dictionary.
7. Write a Python program that takes a list of dictionaries (where each dictionary represents a row
of data) and writes it to a CSV file. Ensure the CSV file has a header row.
Example: Given the list data = [{'Name': 'Alice', 'Age': 25}, {'Name': 'Bob', 'Age': 30}], the CSV file
should look like:
Name,Age
Alice,25
Bob,30
8. Write a Python program that reads a sentence from the user, splits it into words, and writes the
unique words (no duplicates) to a file in alphabetical order.
9. Read data from a CSV file where each row represents a person with fields like Name, Age, City.
Create a dictionary where the keys are cities, and the values are lists of names of people who live
in those cities.
Output: {'New York': ['Alice', 'Bob'], 'Los Angeles': ['Charlie'], ...}
10. You have a dictionary that maps employee IDs to their names. You also have a CSV file that
contains EmployeeID, Department, and Salary. Update the dictionary so that each employee ID
maps to another dictionary containing their Name, Department, and Salary.
Output: Output: {'E001': {'Name': 'Alice', 'Department': 'HR', 'Salary': '70000'}, ...}