In: Computer Science
"PYTHON"
Declare a list that is initialized with six of your favorite
sports teams. Print out the number of teams to the shell. Use a for
loop to print out the teams to the shell.
2. a. Declare a sporting goods list, initially with no
elements.
b. Use a while loop to prompt the user for a sporting goods item
and append the item to the list.
c. Break out of the loop when the user enters exit; do NOT append
the word exit to the list.
d. Write a for s in sport_list loop to print out the elements in
the list to the shell.
(BONUS) Add code to handle a ctrl-c exit.
3. a. Create a pressure list which initially contains no elements.
b. Using the SenseHat, take 25 pressure measurements and store each
measurement as a floating-point number in the list. Be sure to have
the program sleep for a second or two between each
measurement.
c. After recording the measurements, calculate the average of the
pressure measurements- use the sum function and display the average
on the SenseHat.
4. Allow the user to enter new sporting goods items at a specific
index in the sporting goods list. Prompt the user for the index and
the new item’s name.
5. Sort the sporting goods list in ascending order. Use a while
loop to print out each item in the sporting goods list.
---Allow the user to delete an item from the sporting goods list.
Prompt the user for the item’s name.
-- Sort the sporting goods list in descending order. Use a while
loop to print out each item in the sporting goods list.
---a. Create a week_tuple and assign the days of the week as
strings to the week_tuple.
b. Print out the elements in the week_tuple.
---a. The following list has been declared as follows:
credit_list = [24,3,15]
b. Convert the credit_list to a tuple named credit_tuple.
c. Print out the elements in the credit_tuple.
---a. Write an initialize_list_values function that takes in a
number for the number of elements in a list and an initial value
for each element. The function creates a new list and appends the
elements with the initial value to the new list.
b. Pass in 5 and 3.4 as arguments to the intital_list_values and
store the returned list in data_list.
c. Use a loop to print the elements of the data_list.
---a. Create a tuple named inventory_items_tuple which has
Raspberry Pi 3, Raspberry Pi 2, and Raspberry Pi Camera
Module.
b. Create a tuple named out_of_stock_items_tuple which has
Raspberry Pi Zero. (Hint: add a , after “Raspberry Pi Zero”.)
c. Append these two tuples together into the all_items_tuple. Print
out each element in the tuple using a for loop and use the len
function to determine the number of elements in the tuple.
--a. Create a list of lists consisting of three rows and four
columns, which were initially set to 0.
b. Prompt the user to initialize each element in the list of
lists.
c. Compute the sum of all of the elements in the list. You may use
the sum function.
d. Prompt the user to enter a row number and use a loop to
calculate the sum of the elements in the row and print out the
sum.
Solution:
Given data:
1)
teams = ["Denver Broncos","Atlanta Braves","Baltimore
Ravens","Los Angeles Lakers","Dallas Cowboys","Atlanta
Falcons"];
print('The list of sports team is:');
for i in range(0,6): #since we know that there are 6 teams, we can
have a range from 0 to 6, where the indices will go from 0 to
6-1
print(teams[i]); # print each team name
CODE:
OUTPUT:
2)
sport_list = []; #Declare a sporting goods list, initially with
no elements
item = input('Enter a sporting good: '); #Read the first sporting
item
while item.lower()!="exit": # until exit is entered, we need to
continue with the loop
#also item name is converted to lowercase so
that exit written in any case can be handled
sport_list.append(item); #append the item to
list
item = input('Enter a sporting good: '); #list
the next item
print('The sporting goods list is: ');
for s in sport_list: #using for loop
print(s); #print each item in the
sport_list
CODE:
OUTPUT:
3)
from sense_hat import SenseHat
from time import sleep
pressure = []; #Create a pressure list which initially contains
no elements.
i = 0; # no of pressure values read is initialized to 0
sense = SenseHat(); #create a senseHat variable
while i < 25: # the loop goes on for 25 iterations
p = sense.get_pressure(); #get the
pressure
pressure.append(sense); #append pressure to the
list
sleep(1); # sleep for 1 second
i = i+1; #increment the count of pressure values
read
sumval = sum(pressure); #sum up the pressures using the sum
function
avg = sumval/25; #find the average
print('Average pressure = ', avg); #display average
CODE:
4)
sport_list = ["Bottles","Shoes","Protein"]; #Declare a sporting
goods list
item = input('Enter a sporting good: '); #Read the sporting
item
pos = int(input('Enter the index for new item: ')); #read the
position for new item
sport_list.insert(pos,item); #insert item at given index
print(sport_list); #display the sport_list
CODE:
OUTPUT:
5)
sport_list = ["Bottles","Shoes","Protein","Belt","First Aid
Kit","Cap"]; #Declare a sporting goods list
print('Original List: ',sport_list); #display original list
sport_list.sort()#sort the list by default in acending order
print('Sorted List (Ascending Order):');
i = 0; #iterator
while i < len(sport_list): #wehile i is less than length of
list
print(sport_list[i]); #print the list
value
i = i+1; #increment the iterator
CODE:
OUTPUT:
6)
sport_list = ["Bottles","Shoes","Protein","Belt","First Aid
Kit","Cap"]; #Declare a sporting goods list
print('Original List: ',sport_list); #display original list
item = input('Enter the item to be deleted: ');
if item in sport_list: #if item is present in the list
sport_list.remove(item); #remove the item
print(item,'deleted successfully');
print(sport_list); #display the updated
list
else:
print(item, 'not present in list'); #else, print
item is not in the list
CODE:
OUTPUT:
7)
sport_list = ["Bottles","Shoes","Protein","Belt","First Aid
Kit","Cap"]; #Declare a sporting goods list
print('Original List: ',sport_list); #display original list
sport_list.sort(reverse=True)#sort the list in descending
order
print('Sorted List (Descending Order):');
i = 0; #iterator
while i < len(sport_list): #wehile i is less than length of
list
print(sport_list[i]); #print the list
value
i = i+1; #increment the iterator
CODE:
OUTPUT:
PLEASE GIVEME THUMB UP.........