3.1.4.6 LAB: The basics of lists
LAB
Estimated time
5 minutes
Level of difficulty
Very easy
Objectives
Familiarize the student with:
- using basic instructions related to lists;
- creating and modifying lists.
Scenario
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?
hat_list = [1, 2, 3, 4, 5] # This is an existing list of numbers hidden in the hat.
ReplyDelete# Step 1: write a line of code that prompts the user
# to replace the middle number with an integer number entered by the user.
hat_list[2] = int(input("Enter a integer number. "))
print(hat_list)
# Step 2: write a line of code that removes the last element from the list.
del hat_list[-1]
print(hat_list)
# Step 3: write a line of code that prints the length of the existing list.
print(hat_list, "this list have", len(hat_list), "numbers in it")