3) c. Lists Examples




You are an evil and powerful alien leader and you want to destroy all planets on the Egyke planetary system. Create a list that contains the name of each planet and display it to the user along with its indexes. Then get the index number from the user, remove the corresponding planet and returns its name.

1
2
3
4
5
6
7
8
9
10
11
12
13
Output:
Ready to attack:
1 Vogsphere
2 Caprica
3 Coruscant
4 Worlorn
5 Gallifrey
6 Cybertron
7 Krypton
8 Terra Prime
9 Arrakiss
Enter the index of the planet that you want to attack 7
Readying warhead to attack  Krypton . Standby for attack.




Dang Spader stores his name as Python string, named “coolest_name_ever”. He wonders if he can create a list, called name_list, that contains all the letters of his name, in the same order, as elements. What should he do?

1
2
3
Output:
current list =  ['D', 'a', 'n', 'g', ' ', 'S', 'p', 'i', 'd', 'e', 'r', ' ', 'D', '.', 'S', '.']
sorted list =  [' ', ' ', '.', '.', 'D', 'D', 'S', 'S', 'a', 'd', 'e', 'g', 'i', 'n', 'p', 'r']




Dang likes signing his contracts with his initials: D.S. How can he retrieve these two letters by slicing the list name_list?

1
2
3
4
Output:
['D', '.', 'S', '.']
['D', '.', 'S', '.']
['D', '.', 'S', '.']




Oh no! Dang mistyped his last name in the list! How can he correct that to Dang Spadert?

1
2
Output:
Corrected =  ['D', 'a', 'n', 'g', ' ', 'S', 'p', 'i', 'd', 'e', 'r', ' ', 'D', '.', 'S', '.']




Problems continue: Lang told Dang that he needs his middle name “Gee” in his super-fancy list, exactly as is in his passport. How can you help Dang insert his middle name in the list in a one-liner pythonic way?

1
2
3
Output:
Current location of surname =  5
Corrected ['D', 'a', 'n', 'g', ' ', 'G', 'e', 'e', ' ', 'S', 'p', 'i', 'd', 'e', 'r', ' ', 'D', '.', 'S', '.']




Lang claims that his full name contains more ‘a’ characters than Dang’s ‘e’ characters. But Dang is sure that Lang is wrong… Lang’s full name is Lang Wuaha, Ringo Chan. Who is right?

1
2
3
Output:
Count of 'e's in Dang's name =  3
Count of '
a's in Lang's name =  4




Dang lost. He is upset. As a repercussion, he is trying to remove all ‘a’ characters from Lang’s list. How many times does he have to remove ‘a’ before Python raises an error?

1
2
Output:
The character 'a' can be removed = 4 times