Exercise 4-10 Solution Python Crash Course Chapter 4: Working With Lists

 

πŸ“˜ Python Crash Course – Chapter 4: Working With Lists – Exercise 4-10 Solution

Welcome to another solution from Python Crash Course by Eric Matthes.


πŸ“ Exercise 4-10

Task:
Using one of the programs you wrote in this chapter, add several lines to the end of the program that do the following:
• Print the message, The first three items in the list are: . Then use a slice to print the first three items from that program’s list .
• Print the message, Three items from the middle of the list are: . Use a slice to print three items from the middle of the list .
• Print the message, The last three items in the list are: . Use a slice to print the last three items in the list

✅ Solution Code:


num_list = list(range(1,11))

print("The first three items in the list are:")
print(num_list[0:3])

print("-----------------------------------------------------------")

print("Three items from the middle of the list are:")
print(num_list[4:7])

print("-----------------------------------------------------------")


print("The last three items in the list are:")
print(num_list[-3:])

print("-----------------------------------------------------------")

🧠 Code Explanation:

This Python code demonstrates how to use slicing to extract specific portions of a list. Here's how it works:

  1. Create a list of numbers:

    The variable num_list is assigned a list of numbers generated using range(1, 11), which creates a sequence of numbers from 1 to 10.

  2. Extract the first three items:

    The slice num_list[0:3] is used to extract the first three items from the list. The slice starts at index 0 and ends before index 3.

  3. Extract three items from the middle:

    The slice num_list[4:7] is used to extract three items from the middle of the list. The slice starts at index 4 and ends before index 7.

  4. Extract the last three items:

    The slice num_list[-3:] is used to extract the last three items from the list. The negative index -3 starts the slice three items from the end of the list, and the slice continues to the end.

This code demonstrates how to use slicing to access specific portions of a list in Python.

πŸ” Output:


The first three items in the list are:
[1, 2, 3]
-----------------------------------------------------------
Three items from the middle of the list are:
[5, 6, 7]
-----------------------------------------------------------
The last three items in the list are:
[8, 9, 10]
-----------------------------------------------------------

πŸ“š Related Exercises from Chapter 4:


πŸ”— Connect With Us:

“The only way to learn a new programming language is by writing programs in it.”
– Dennis Ritchie

πŸ“Œ Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips

“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python

πŸ“Œ Follow Us And Stay Updated For Daily Updates

Comments