Start Your Journey with Linux Command Line
![]() |
The basics of lists |
If you're taking PCAP - Programming Essentials In Python , you may have encountered this question 3.4.1.6 LAB: The basics of lists.
Familiarize the student with:
using basic instructions related to lists;
creating and modifying lists.
There once was a hat. The hat contained no rabbit, but a list of five numbers: 1, 2, 3, 4, and 5.
Your task is to:
write a line of code that prompts the user to replace the middle number in the list with an integer number entered by the user (Step 1)
write a line of code that removes the last element from the list (Step 2)
write a line of code that prints the length of the existing list (Step 3).
Ready for this challenge?
=================================================================================
LAB 3.4.1.6 "The basics of lists" introduces fundamental list operations through a small story: a hat contains a list of five numbers, 1, 2, 3, 4, and 5, stored in a variable named hat_list. Lists in Python are ordered, mutable collections, meaning you can change their contents after creation. The lab walks through three separate steps that together exercise creating a list, indexing into it, deleting an element, and measuring its length.
Step 1 asks you to replace the middle number with an integer typed by the user. The middle value 3 sits at index 2, because list indexing starts at 0. The code reads the user's value with int(input("Please Enter an integer: ")) and assigns it directly with hat_list[2] = user_number. This shows that a list element can be overwritten simply by assigning to its index, in much the same way you would update a variable.
Step 2 removes the last element. The code uses del hat_list[-1], where the negative index -1 refers to the final item in the list. The del statement permanently removes that element and shifts the remaining ones, so the list shrinks by one. Because the user may have replaced the middle element in step 1, the last element is still the original 4 after indexing positions, and deleting it leaves four elements.
Step 3 prints the current length using len(hat_list). The len() function returns the number of elements currently in the list, which is now 4 after the deletion. The post wraps this inside a formatted string with print(f"The length of the list is: {len(hat_list)}") and then prints the updated list itself so you can verify the changes visually. Taken together, the three steps give a compact tour of the most common list manipulation techniques used throughout Python.
hat_list[2] = user_number.del to remove it.del statement permanently removes an element and shifts the remaining items.len(hat_list) returns the number of elements, which becomes 4 after the deletion.Python lists are zero-indexed, so the first element is at index 0. In a list of five numbers the middle value sits at position 2, which is exactly the third element.
Assigning to an existing index overwrites that element in place. The line hat_list[2] = user_number replaces whatever is currently at index 2 with the new integer.
The negative index -1 selects the last element of the list, and del removes it permanently. Afterward the list is one item shorter and the remaining elements shift to fill the gap.
The len() function returns the current number of elements in the list. After removing one element from the original five, it returns 4.
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.