Programming Assignment Unit 6
(a). Employee List Operations and Salary Management
Assignment Description
This section addresses the requirements for a Data Analyst role, involving operations
on an existing list of 10 employee names and a corresponding salary list. The tasks
include splitting lists, adding and removing elements, merging lists, updating salaries
with a 4% rise, and sorting to identify top earners.
Python Code
# Part (a): Employee List Operations and Salary Management
# 1. Initial list of 10 employee names
employee_names = [
"Alice Smith", "Bob Johnson", "Charlie Brown", "Diana Prince", "Eve Adams",
"Frank White", "Grace Kelly", "Harry Potter", "Ivy Green", "Jack Black"
]
print(f"Initial Employee Names: {employee_names}")
# 2. Split the list into two sub-lists, each containing 5 names
subList1 = employee_names[0:5]
subList2 = employee_names[5:10]
print(f"\nsubList1: {subList1}")
print(f"subList2: {subList2}")
# Technical Explanation:
# List slicing is used to create sub-lists. `employee_names[start:end]` creates
a new list
# containing elements from the `start` index up to (but not including) the
`end` index.
# Here, `employee_names[0:5]` gets the first five elements (indices 0-4) for
subList1,
# and `employee_names[5:10]` gets the next five elements (indices 5-9) for
subList2.
# 3. Add a new employee "Kriti Brown" to subList2
new_employee = "Kriti Brown"
[Link](new_employee)
print(f"\nsubList2 after adding new employee: {subList2}")
# Technical Explanation:
# The `append()` method is used to add a single element to the end of a list.
# This modifies the list in-place, extending it by one element.
# 4. Remove the second employee\'s name from subList1
# Python lists are 0-indexed, so the second employee is at index 1.
del subList1[1]
print(f"\nsubList1 after removing second employee: {subList1}")
# Technical Explanation:
# The `del` statement is used to remove an item from a list given its index.
# `del subList1[1]` removes the element at index 1 from `subList1`.
# Alternatively, `[Link](1)` could be used, which also returns the
removed item.
# 5. Merge both the lists
merged_list = subList1 + subList2
print(f"\nMerged List of Employees: {merged_list}")
# Technical Explanation:
# The `+` operator is used to concatenate two lists. It creates a new list
# containing all elements from the first list followed by all elements from the
second list.
# This operation does not modify the original lists.
# 6. Assume a salaryList that stores salary of these employees (initial 10
employees)
# The salaries correspond to the original `employee_names` list.
salaryList = [
60000, 75000, 50000, 80000, 65000,
70000, 90000, 55000, 82000, 72000
]
print(f"\nInitial Salary List: {salaryList}")
# 7. Give a rise of 4% to every employee and update the salaryList
# Using a list comprehension for efficient update
salaryList = [salary * 1.04 for salary in salaryList]
print(f"Updated Salary List (after 4% rise): {salaryList}")
# Technical Explanation:
# A list comprehension `[expression for item in iterable]` is used to create a
new list.
# Here, for each `salary` in the original `salaryList`, it calculates `salary *
1.04`
# and creates a new list with these updated values. This is a concise and
efficient way
# to apply an operation to all elements of a list and create a new one.
# 8. Sort the SalaryList and show top 3 salaries
[Link](reverse=True)
print(f"\nSorted Salary List (descending): {salaryList}")
print(f"Top 3 Salaries: {salaryList[0:3]}")
# Technical Explanation:
# The `sort()` method sorts the list in-place. By setting `reverse=True`,
# the list is sorted in descending order (highest to lowest). After sorting,
# list slicing `salaryList[0:3]` is used again to retrieve the first three
elements,
# which represent the top 3 salaries.
Output
Initial Employee Names: ['Alice Smith', 'Bob Johnson', 'Charlie Brown', 'Diana
Prince', 'Eve Adams', 'Frank White', 'Grace Kelly', 'Harry Potter', 'Ivy
Green', 'Jack Black']
subList1: ['Alice Smith', 'Bob Johnson', 'Charlie Brown', 'Diana Prince', 'Eve
Adams']
subList2: ['Frank White', 'Grace Kelly', 'Harry Potter', 'Ivy Green', 'Jack
Black']
subList2 after adding new employee: ['Frank White', 'Grace Kelly', 'Harry
Potter', 'Ivy Green', 'Jack Black', 'Kriti Brown']
subList1 after removing second employee: ['Alice Smith', 'Charlie Brown',
'Diana Prince', 'Eve Adams']
Merged List of Employees: ['Alice Smith', 'Charlie Brown', 'Diana Prince', 'Eve
Adams', 'Frank White', 'Grace Kelly', 'Harry Potter', 'Ivy Green', 'Jack
Black', 'Kriti Brown']
Initial Salary List: [60000, 75000, 50000, 80000, 65000, 70000, 90000, 55000,
82000, 72000]
Updated Salary List (after 4% rise): [62400.0, 78000.0, 52000.0, 83200.0,
67600.0, 72800.0, 93600.0, 57200.0, 85280.0, 74880.0]
Sorted Salary List (descending): [93600.0, 85280.0, 83200.0, 78000.0, 74880.0,
72800.0, 67600.0, 62400.0, 57200.0, 52000.0]
Top 3 Salaries: [93600.0, 85280.0, 83200.0]
(b). Sentence to Wordlist Conversion and Reversal
Assignment Description
This section focuses on designing a program to convert a given sentence into a list of
words (wordlist) and then reverse the order of words in that list.
Python Code
# Part (b): Sentence to Wordlist Conversion and Reversal
def convert_and_reverse_sentence(sentence):
"""
Converts a given sentence into a wordlist, then reverses the wordlist.
"""
print(f"Original Sentence: \"{sentence}\"")
# 1. Convert a sentence into a wordlist
# The `split()` method without arguments splits the string by any
whitespace
# and handles multiple spaces between words correctly, returning a list of
words.
wordlist = [Link]()
print(f"\nWordlist: {wordlist}")
# Technical Explanation:
# The `split()` method is a string method that breaks a string into a list
of substrings
# based on a delimiter. When no delimiter is specified, it splits by
whitespace
# (spaces, tabs, newlines) and discards empty strings, effectively giving a
list of words.
# 2. Reverse the wordlist
# The `reverse()` method reverses the elements of the list in-place.
[Link]()
print(f"\nReversed Wordlist: {wordlist}")
# Technical Explanation:
# The `reverse()` method is a list method that reverses the order of the
items in the list.
# It modifies the list directly and does not return a new list. If a new
reversed list
# was needed without modifying the original, `reversed_list =
wordlist[::-1]` or
# `reversed_list = list(reversed(wordlist))` could be used.
# Example Usage:
sentence1 = "This is a sample sentence for the assignment."
convert_and_reverse_sentence(sentence1)
sentence2 = "Python programming is fun and powerful."
convert_and_reverse_sentence(sentence2)
Output
Original Sentence: "This is a sample sentence for the assignment."
Wordlist: ['This', 'is', 'a', 'sample', 'sentence', 'for', 'the',
'assignment.']
Reversed Wordlist: ['assignment.', 'the', 'for', 'sentence', 'sample', 'a',
'is', 'This']
Original Sentence: "Python programming is fun and powerful."
Wordlist: ['Python', 'programming', 'is', 'fun', 'and', 'powerful.']
Reversed Wordlist: ['powerful.', 'and', 'fun', 'is', 'programming', 'Python']
References
The technical explanations provided are based on fundamental Python list and string
operations, which are extensively covered in various Python programming resources.
For further reading and in-depth understanding, the following books are
recommended:
1. Lutz, M. (2013). Learning Python (5th ed.). O'Reilly Media.
2. Sweigart, A. (2019). Automate the Boring Stuff with Python (2nd ed.). No Starch
Press.
3. Matthes, E. (2019). Python Crash Course (2nd ed.). No Starch Press.