0% found this document useful (0 votes)
5 views4 pages

Python List and String Assignment

The document outlines Python list operations and string manipulations in an HR context, demonstrating tasks such as slicing, adding, deleting, and sorting employee data. It details the process of managing employee names and salaries, including a 4% salary increase and identifying the top three earners. Additionally, it showcases string manipulation by converting a sentence into a list of words and reversing that list.

Uploaded by

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

Python List and String Assignment

The document outlines Python list operations and string manipulations in an HR context, demonstrating tasks such as slicing, adding, deleting, and sorting employee data. It details the process of managing employee names and salaries, including a 4% salary increase and identifying the top three earners. Additionally, it showcases string manipulation by converting a sentence into a list of words and reversing that list.

Uploaded by

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

Python List Operations and String Manipulations

(a) Employee List Operations

This program demonstrates various list operations such as slicing, adding, deleting,

joining, updating, and sorting. The context is an HR scenario where an analyst manages

employee data. Each step is explained and implemented using Python’s list methods and

operators. (Downey, 2015).

# Initial list of employees and salaries


employees = [
"Alice Johnson", "Brian Odoi", "Chloe Mwangi", "Daniel Okello",
"Evelyn Kim",
"Frank Namutebi", "Grace Achieng", "Henry Ssemanda", "Irene Kato",
"John Bosco"
]

salaryList = [
48000.00, 52000.00, 47000.00, 60000.00, 55000.00,
43000.00, 51000.00, 49500.00, 58000.00, 45000.00
]

# Split the list into two sub-lists


subList1 = employees[:5]
subList2 = employees[5:]

# Add a new employee


[Link]("Kriti Brown")
[Link]("Kriti Brown")
[Link](52000.00)

# Remove the second employee from subList1


removed_employee = [Link](1)
if removed_employee in employees:
idx = [Link](removed_employee)
[Link](idx)
[Link](idx)

# Merge both sub-lists


merged_list = subList1 + subList2

# Give a 4% salary increase


for i in range(len(salaryList)):
salaryList[i] = round(salaryList[i] * 1.04, 2)

# Sort the list and show top 3 salaries


sorted_salaries_desc = sorted(salaryList, reverse=True)
top_3_salaries = sorted_salaries_desc[:3]

# Pair employees with their salaries


pairs = list(zip(employees, salaryList))
pairs_sorted_by_salary = sorted(pairs, key=lambda x: x[1], reverse=True)
top_3_pairs = pairs_sorted_by_salary[:3]

Output:

Final Employees: ['Alice Johnson', 'Chloe Mwangi', 'Daniel Okello',


'Evelyn Kim', 'Frank Namutebi', 'Grace Achieng', 'Henry Ssemanda',
'Irene Kato', 'John Bosco', 'Kriti Brown']
Final Salaries: [49920.0, 48880.0, 62400.0, 57200.0, 44720.0, 53040.0,
51480.0, 60320.0, 46800.0, 54080.0]
Top 3 Salaries: [62400.0, 60320.0, 57200.0]
Top 3 Employees: [('Daniel Okello', 62400.0), ('Irene Kato', 60320.0),
('Evelyn Kim', 57200.0)]

Explanation:

The code begins by creating two lists: one for employee names and another for their

salaries. List slicing (employees[:5] and employees[5:]) divides the list into two equal

halves. The append() method adds 'Kriti Brown' to the second list, while pop() removes
the second employee from the first list. These operations demonstrate mutability of lists

since the changes directly affect the list in memory.

The two sub-lists are then merged using the + operator. Next, a 4% raise is applied to

each salary through a loop that updates elements in place. The sorted() function orders

salaries in descending order, and slicing [:3] extracts the top three salaries. Pairing

employees with their updated salaries via the zip() function provides a clear HR-ready

summary of high earners.

(b) String to List Conversion and Reversal

sentence = "Python makes working with lists straightforward and


powerful"
wordlist = [Link]()
reversed_wordlist = list(reversed(wordlist))

Original Sentence: Python makes working with lists straightforward and


powerful
Word List: ['Python', 'makes', 'working', 'with', 'lists',
'straightforward', 'and', 'powerful']
Reversed Word List: ['powerful', 'and', 'straightforward', 'lists',
'with', 'working', 'makes', 'Python']
Reversed Sentence: powerful and straightforward lists with working makes
Python

Explanation:

The program uses the split() method to convert a sentence into a list of words, where each

space marks a separator. The reversed() function is then applied to generate a new list

with elements in reverse order. This operation demonstrates how Python’s list

manipulation tools can handle text data efficiently. The final output confirms that both
the original and reversed sequences of words are correctly handled without altering the

original sentence.

References

Downey, A. (2015). Think Python: How to think like a computer scientist (2nd ed.).

Green Tea Press. [Link]

You might also like