Class Exercisesmust
Class Exercisesmust
1. Jiho is updating a list of orders. He just received orders for "lilac" and "iris".
2. Create a list called new_orders that contains our new orders.
3. Use + to create a new list called orders_combined that combines orderswith new_orders
orders = ["daisy", "buttercup", "snapdragon", "gardenia", "lily"]
2. "Adam" decided his fridge is too full at the moment and asked us to remove him from
the waitlist and make space for one of our other townsfolk.
Replace "Adam" with our other interested customer "Calla" using the index method we
used in the narrative.
Print garden_waitlist to see the change!
3. Alisha realized she was already stocked with all the items we are selling. She asked us
to replace her with her friend Alex who just ran out.
Replace Alisha with Alex using a negative index.
Print garden_waitlist again to see the change!
1. We want to have a way to store all of our classroom test score data.
Using the provided table, create a two-dimensional list called class_name_test to
represent the data. Each sublist in class_name_test should have one student’s name and
their associated score.
Name Test Score
"Jenny" 90
"Alexus" 85.5
Name Test Score
"Sam" 83
"Ellie" 101.5
Print class_name_test to see the result.
2. Use double square brackets ([][]) to select Sam‘s test score from the
list class_name_test.
Save it to the variable sams_score.
Print the variable sams_score to see the result.
3. Use double square brackets ([][]) to select Ellies test score from the
list class_name_test. This time only use negative indices!
Save it to the variable ellies_score.
Print the variable ellies_score to see the result.
1. Modify the range() function that created the range range_five_three such that it:
• Starts at 5
• Has a difference of 3 between each item
• Ends before 15
1.
suitcase = ["shirt", "shirt", "pants", "pants", "pajamas", "books"]
Create a new list called middle that contains the middle two items ( ["pants", "pants"] )
from suitcase.
Print middle to see the slice!
suitcase = ["shirt", "shirt", "pants", "pants", "pajamas", "books"]
1. Create a new list called last_two_elements containing the final two elements
of suitcase.
Print last_two_elements to see your result.
2. Create a new list called slice_off_last_three containing all but the last three elements.
Print slice_off_last_three to see your result.
1.
We are going to write a while loop to iterate over the provided list python_topics.
First, we will need a variable to represent the length of the list. This will help us know how many times
our while loop needs to iterate.
Create a variable length and set its value to be the length of the list of python_topics.
python_topics = ["variables", "control flow", "loops", "modules", "classes"]
First, we will need a variable to represent the length of the list. This will help us know how many times our while loop
needs to iterate.
Create a variable length and set its value to be the length of the list of python_topics.
python_topics = ["variables", "control flow", "loops", "modules", "classes"]
#Your code below:
2. Next, we will require a variable to compare to our length variable to make sure we are able to implement a condition
that eventually allows the loop to stop.
3. Let’s now build our loop. We want our loop to iterate over the list of python_topics and on each iteration print "I am
learning about <element from python_topics>". For this loop we will need the following components:
1.
Using a for loop, iterate through the dog_breeds_available_for_adoption list and print() out each dog breed.
2.
Inside your for loop, after you print each dog breed, check if the current element inside dog_breed is equal
to dog_breed_I_want. If so, print "They have the dog I want!"
3.
Add a break statement when your loop has found dog_breed_I_want so that the rest of the list does not need to be
checked once we have found our breed.
1.
Your computer is the doorman at a bar in a country where the drinking age is 21.
Loop through the ages list. If an entry is less than 21, skip it and move to the next entry. Otherwise, print() the age.
ages = [12, 38, 34, 26, 21, 19, 67, 41, 17]
1.We have provided the list sales_data that shows the number of scoops sold for different flavors of ice cream at three different
locations: Scoopcademy, Gilberts Creamery, and Manny’s Scoop Shop.
We want to sum up the total number of scoops sold across all three locations. Start by defining a variable scoops_sold and set it to zero.
sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
3.Within our sales_data loop, nest a secondary loop to go through each location sublist element and add the element value
to scoops_sold.
By the end, you should have the sum of every number in the sales_data nested list.
Create a function called generate_trip_instructions() that defines one parameter called location.
3. generate_trip_instructions() should also let our users know they can reach their location using public transit.
4. Time for some greenery! Let’s see what happens when we call the function and input the argument "Central Park", a
backyard wonder in the heart of New York City.
5. The day trip is over and we need to get back to the train station to head home. Change the argument to "Grand Central
Station" and call the function again.