-
-
Save cibofdevs/7597f8f8e790e062f55a6b298e92ac50 to your computer and use it in GitHub Desktop.
# 1. Write code to add ‘horseback riding’ to the third position (i.e., right before volleyball) in the list sports. | |
sports = ['cricket', 'football', 'volleyball', 'baseball', 'softball', 'track and field', 'curling', 'ping pong', 'hockey'] | |
sports.insert(2, 'horseback riding') | |
# 2. Write code to take ‘London’ out of the list trav_dest. | |
trav_dest = ['Beirut', 'Milan', 'Pittsburgh', 'Buenos Aires', 'Nairobi', 'Kathmandu', 'Osaka', 'London', 'Melbourne'] | |
trav_dest.pop(7) | |
# 3. Write code to add ‘Guadalajara’ to the end of the list trav_dest using a list method. | |
trav_dest = ['Beirut', 'Milan', 'Pittsburgh', 'Buenos Aires', 'Nairobi', 'Kathmandu', 'Osaka', 'Melbourne'] | |
trav_dest.append('Guadalajara') | |
# 4. Write code to rearrange the strings in the list winners so that they are in alphabetical order from A to Z. | |
winners = ['Kazuo Ishiguro', 'Rainer Weiss', 'Youyou Tu', 'Malala Yousafzai', 'Alice Munro', 'Alvin E. Roth'] | |
winners.sort() | |
# 5. For each character in the string saved in ael, append that character to a list that should be saved in a variable app. | |
ael = "python!" | |
app = [] | |
for w in ael: | |
app.append(w) | |
print(app) | |
# 6. For each string in wrds, add ‘ed’ to the end of the word (to make the word past tense). | |
# Save these past tense words to a list called past_wrds. | |
wrds = ["end", 'work', "play", "start", "walk", "look", "open", "rain", "learn", "clean"] | |
past_wrds = [word + "ed" for word in wrds] | |
print(past_wrds) |
ael = "python!"
app = []
for w in ael:
app.append(w)
print(app)
#Write code to rearrange the strings in the list winners so that they are in alphabetical order from A to Z.
winners = ['Alice Munro', 'Alvin E. Roth', 'Kazuo Ishiguro', 'Malala Yousafzai', 'Rainer Weiss', 'Youyou Tu']
z_winners = winners
z_winners.reverse()
print(z_winners) #Answer: ['Youyou Tu', 'Rainer Weiss', 'Malala Yousafzai', 'Kazuo Ishiguro', 'Alvin E. Roth', 'Alice Munro']
#Write code to create a list of word lengths for the words in original_str,
#using the accumulation pattern and assign the answer to a variable num_words_list.
#(You should use the len function).
original_str = "The quick brown rhino jumped over the extremely lazy fox"
original_list = list(original_str.split())
num_words = len(original_list)
num_words_list = []
for i in original_list:
num_words_list.append((len(i)))
print(num_words_list)
Write code to add ‘horseback riding’ to the third position (i.e., right before volleyball) in the list sports.
sports = ['cricket', 'football', 'volleyball', 'baseball', 'softball', 'track and field', 'curling', 'ping pong', 'hockey']
sports.insert(2, 'horseback riding')