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: 1234, 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?


hatList = [1, 2, 3, 4, 5] # This is an existing list of numbers hidden in the hat.
# Step 1: write a line of code that prompts the user
# to replace the middle number with an integer number entered by the user.
# Step 2: write a line of code here that removes the last element from the list.
# Step 3: write a line of code here that prints the length of the existing list.
print(hatList)
  • Console 

Comments

  1. hat_list = [1, 2, 3, 4, 5] # This is an existing list of numbers hidden in the hat.

    # 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")

    ReplyDelete

Post a Comment

Popular Posts