0% found this document useful (0 votes)
18 views

Class Exercisesmust

The document outlines various programming tasks related to managing lists, including adding and modifying orders, creating waitlists, handling test scores, and implementing loops and functions. It provides examples of using lists, indices, and loops in Python to achieve specific goals such as summing values and generating trip instructions. The tasks are structured to help users practice their coding skills in a practical context.

Uploaded by

kamarakemoh17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Class Exercisesmust

The document outlines various programming tasks related to managing lists, including adding and modifying orders, creating waitlists, handling test scores, and implementing loops and functions. It provides examples of using lists, indices, and loops in Python to achieve specific goals such as summing values and generating trip instructions. The tasks are structured to help users practice their coding skills in a practical context.

Uploaded by

kamarakemoh17
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1. Jiho works for a gardening store called Petal Power.

Jiho keeps a record of orders in a list


called orders.
2. Use print to inspect the orders he has received today.
3. Jiho just received a new order for "tulips". Use append to add this string to orders.
4. Another order has come in! Use append to add "roses" to orders.
5. Use print to inspect the orders Jiho has received today.
orders = ["daisies", "periwinkle"]

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"]

employees = ["Michael", "Dwight", "Jim", "Pam", "Ryan", "Andy", "Robert"]


1. Use square brackets ([ ]) to select the 4th employee from the list employees. Save it to the
variable employee_four.
2. Paste the following code:
print(employees[8])

shopping_list = ["eggs", "butter", "milk", "cucumbers", "juice", "cereal"]


1. Create a variable called last_element.
Assign the last element in shopping_list to the variable last_element using a negative
index.
2. Now select the element with index 5 and save it to the variable index5_element.
3. Use print to display both index5_element and last_element.
Note that they are equal to "cereal"!
1. We have decided to start selling some of our garden produce. Word around our town
has spread and people are interested in getting some of our delicious vegetables and
fruit.
We decided to create a waitlist to make sure we can sell to all of our new customers!
Define a list called garden_waitlist and set the value to contain our customers (in
order): "Jiho", "Adam", "Sonny", and "Alisha".

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

2. Create a range called range_diff_five that:


• Starts at 0
• Has a difference of 5 between each item
• Ends before 40

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"]

#Your code below:


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"]
#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.

Create a variable called index and initialize the value to be 0.

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. A condition for our while loop


2. A statement in the loop body to print from our condition
3. A statement in the loop body to increment our index forward.

The end result should output:


I am learning about variables
I am learning about control flow
I am learning about loops
I am learning about modules
I am learning about classes

1.

You have a list of dog breeds you can adopt, dog_breeds_available_for_adoption.

Using a for loop, iterate through the dog_breeds_available_for_adoption list and print() out each dog breed.

Use the <temporary variable> name of dog_breed in your loop declaration.


dog_breeds_available_for_adoption = ["french_bulldog", "dalmatian", "shihtzu", "poodle",
"collie"]
dog_breed_I_want = "dalmatian"

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]]

2.Loop through the sales_data list using the following guidelines:

1. For our temporary variable of the for loop, call it location.


2. print() out each location list.

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.

4.Print out the value of scoops_sold outside of the nested loop.


1. We want to create a program that allows our users to generate the directions for their upcoming trip!

Create a function called generate_trip_instructions() that defines one parameter called location.

2. generate_trip_instructions() should print out the following:

Looks like you are planning a trip to visit <location>


Where <location> will represent the location parameter.

3. generate_trip_instructions() should also let our users know they can reach their location using public transit.

Let’s have generate_trip_instructions()also print out the following on a new line:

You can use the public subway system to get to <location>


Where <location> will represent the location parameter.

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.

You might also like