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

Programming Assignment Unit 6

Uploaded by

kuyembehj05
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)
15 views4 pages

Programming Assignment Unit 6

Uploaded by

kuyembehj05
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

Programming Assignment Solution

Part (a): List Operations for Employee Data

Explanation

This section demonstrates various list operations, including splitting a list,


adding/removing elements, merging lists, updating salaries, and sorting. The
tasks are performed on a list of employee names and their corresponding
salaries.

Python Code
# Initial list of 10 employees
employeeList = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace",
"Hannah", "Ivy", "Jack"]

# Split into two sublists


subList1 = employeeList[:5] # First 5 names
subList2 = employeeList[5:] # Last 5 names

# Add new employee "Kriti Brown" to subList2


[Link]("Kriti Brown")

# Remove the second employee from subList1 (index 1)


removed_employee = [Link](1)

# Merge both lists


mergedList = subList1 + subList2

# Salary list (assuming corresponding salaries for 10 employees + Kriti Brown)

salaryList = [50000, 60000, 70000, 80000, 90000, 55000, 65000, 75000, 85000,
95000, 62000]

# Give a 4% raise to each employee


salaryList = [salary * 1.04 for salary in salaryList]

# Sort salaryList in descending order and get top 3 salaries


[Link](reverse=True)
top3Salaries = salaryList[:3]

# Output results
print("SubList1 after removal:", subList1)
print("SubList2 after addition:", subList2)
print("Merged List:", mergedList)
print("Updated Salaries:", salaryList)
print("Top 3 Salaries:", top3Salaries)
Output

Technical Explanation

1. Splitting the List: The original list is divided into two sublists using
slicing (list[:5] and list[5:]).

2. Adding an Employee: [Link]("Kriti Brown") adds a new


name to the second sublist.

3. Removing an Employee: [Link](1) removes the second


employee (index 1) from subList1.

4. Merging Lists: The + operator combines both sublists


into mergedList.

5. Updating Salaries: A list comprehension applies a 4% raise to each


salary.

6. Sorting and Top 3 Salaries: [Link](reverse=True) sorts in


descending order, and slicing ([:3]) extracts the top 3 salaries.

Part (b): Sentence to Word List and Reversal


Explanation

This program takes a sentence, splits it into individual words, stores them in
a list, reverses the list, and displays the result.

Python Code
# Input sentence
sentence = "Python programming is fun and powerful"

# Convert sentence into word list


wordList = [Link]()

# Reverse the word list


reversedWordList = wordList[::-1]

# Output results
print("Original Word List:", wordList)
print("Reversed Word List:", reversedWordList)

Out put

Technical Explanation

1. Splitting the Sentence: The split() method breaks the sentence into
words based on spaces.

2. Reversing the List: Slicing with [::-1] reverses the list order.

References
 Downey, A. (2015). Think Python: How to think like a computer
scientist (2nd ed.). Green Tea
Press. [Link]

 Khan Academy. (2011, June 30). Python lists [Video].


YouTube. [Link]

 Khan Academy. (2011, June 30). For loops in Python [Video].


YouTube. [Link]

 Khan Academy. (2011, June 30). While loops in Python [Video].


YouTube. [Link]

You might also like