Write a Python program that does the following: a. Assigns the following list to
ID: 3666662 • Letter: W
Question
Write a Python program that does the following:
a. Assigns the following list to a variable called “sports”:
[‘cricket’, ‘basketball’, ‘ping pong’, ‘squash’]
b. Add ‘soccer’ to the end of the list.
c. Add ‘taekwondo’ to the list. Make it the first element of the list.
d. Display the list that you have so far.
e. Sort the list in descending order and display it.
f. Remove ‘basketball’ from the list and display the resulting list.
A sample run is shown below:
List after append and insert:
['taekwondo', 'cricket', 'basketball', 'ping pong', 'squash', 'soccer']
List after sorting in descending order:
['taekwondo', 'squash', 'soccer', 'ping pong', 'cricket', 'basketball']
List after removing "basketball":
['taekwondo', 'squash', 'soccer', 'ping pong', 'cricket']
Explanation / Answer
#! python program for the list of sports
# Assigning the list to a variable called “sports”
sports = [‘cricket’, ‘basketball’, ‘ping pong’, ‘squash’];
# Adding ‘soccer’ to the end of the list sports
sports.append('soccer');
# Adding ‘taekwondo’ as a first element of the list sports
sports[0] = 'taekwondo';
# Displaying the list sports
print "List after append and insert:"
print sports
# Sorting the list sports in descending order and displaying the list sports
sports.reverse();
print "List after sorting in descending order:"
print sports
# Removing ‘basketball’ from the list sports and displaying the resulting list sports
sports.remove('basketball') ;
print "List after removing "basketball":"
print sports
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.