Skip to main content

Exercises 5.10 Drill and Practice

This section is designed to reinforce your understanding and help you gain confidence in writing code. Whether youโ€™re a beginner or looking to brush up on the basics, these exercises will cover basic topics such as variables, data types, statements and expressions.
Take your time with each exercise, and donโ€™t hesitate to experiment with different approaches. Programming is all about solving problems creatively, so feel free to explore and make mistakesโ€”itโ€™s all part of the learning process.

1.

Declare a list called my_lst that includes at least 4 elements (values).

2.

Display the length of the list my_stuff.

3.

Display the last element in the list my_stuff.

4.

Change the value of the first element in the list my_stuff to a value other than "computer".

5.

Update the fourth element of the list my_stuff so that its value is "game consoles".

6.

Complete this program so that it first asks the user to enter a number value between 1 and the length of the list drinks then it asks the user to enter a number that represents the desired quantity. Using that input, the program retrieves and displays the corresponding drink information and the total price. For example, if the user enters 2 and 3 the output would include "chai latte" and 13.68
Solution.
## question 5 solution ##
drinks = ["tea","chai latte", "latte", "coffee", "soda", "berry lemonade"]
prices = [2.49, 4.56, 3.75, 2.99, 1.99, 3.49]

order = input("Enter a number value between 1 and " + str(len(drinks)) + ": ")
quantity = input("How many " + drinks[int(order) - 1] + "s would you like? ")

total = prices[int(order) - 1] * int(quantity)

print("Your total is","$"+str(total)+".","Your",drinks[int(order) - 1],"will be ready shortly.")

7.

Write a program that extracts the last three items in the list sports and assigns it to the variable last. Make sure to write your code so that it works no matter how many items are in the list.

8.

Write code that combines the following variables so that the sentence โ€œYou are doing a great job, keep it up!โ€ is assigned to the variable message. Do not edit the values assigned to by, az, io, or qy.

9.

Assign the last element of lst to the variable end_elem. Do this so that it works no matter how long lst is.

10.

Assign the number of elements in lst to the variable num_lst.

11.

What will the output be for the following code?
let = "z"
let_two = "p"
c = let_two + let
m = c*5
print(m)
  • zpzpzpzpzp
  • The order of concatenation matters.
  • zzzzzppppp
  • Think about the order that the program is executed in, what occurs first?
  • pzpzpzpzpz
  • Yes, because let_two was put before let, c has "pz" and then that is repeated five times.
  • pppppzzzzz
  • Think about the order that the program is executed in, what occurs first?
  • None of the above, an error will occur.
  • This is correct syntax and no errors will occur.

12.

What will the output be for the following code?
ls = ['run', 'world', 'travel', 'lights', 'moon', 'baseball', 'sea']
new = ls[2:4]
print(new)
  • [โ€™travelโ€™, โ€™lightsโ€™, โ€™moonโ€™]
  • When we take a slice of something, it includes the item at the first index and excludes the item at the second index.
  • [โ€™worldโ€™, โ€™travelโ€™, โ€™lightsโ€™]
  • When we take a slice of something, it includes the item at the first index and excludes the item at the second index. Additionally, Python is a zero-index based language.
  • [โ€™travelโ€™, โ€™lightsโ€™]
  • Yes, python is a zero-index based language and slices are inclusive of the first index and exclusive of the second.
  • [โ€™worldโ€™, โ€™travelโ€™]
  • Python is a zero-index based language.

13.

What is the type of m?
l = ['w', '7', 0, 9]
m = l[1:2]
  • string
  • Not quite, is it slicing or accessing an element?
  • integer
  • What is happening in the assignment statement for m?
  • float
  • What is happening in the assignment statement for m?
  • list
  • Yes, a slice returns a list no matter how large the slice.

14.

What is the type of m?
l = ['w', '7', 0, 9]
m = l[1]
  • string
  • Yes, the quotes around the number mean that this is a string.
  • integer
  • Not quite, look again at what is being extracted.
  • float
  • Not quite, look again at what is being extracted.
  • list
  • Not quite, is it slicing or accessing an element?
    Which of the following best describes your experience with these problems?
  • 1.
    I found the problems very confusing and didnโ€™t know where to start.
  • 2.
    I started solving the problems but encountered significant difficulties.
  • 3.
    I managed to solve the problems, but it was very challenging.
  • 4.
    I solved the problems with some minor difficulties.
  • 5.
    I found the problems straightforward and solved them easily.
You have attempted of activities on this page.