Introduction to Programming Using Python — Question 31
You develop a Python application for your company.
A list named employees contains 200 employee names, the last five being company management. You need to slice the list to display all employees excluding management.
Which two code segments should you use? Each correct answer presents a complete solution. (Choose two.)
Answer options
- A. employees [1:-4]
- B. employees [:-5]
- C. employees [1:-5]
- D. employees [0:-4]
- E. employees [0:-5]
Correct answer: B, E
Explanation
Option B, employees[:-5], correctly slices the list to exclude the last five management names. Option E, employees[0:-5], also excludes the last five names while starting from the first employee. The other options either start slicing from the wrong index or do not exclude all management employees.