Introducing lists - Exercise 2 (Try Yourself Challenge)
3-1. Seeing the World: Think of at least five places in the world you’d like to visit.
- Store the locations in a list. Make sure the list is not in alphabetical order.
- Print your list in its original order. Don’t worry about printing the list neatly, just print it as a raw Python list.
- Use sorted() to print your list in alphabetical order without modifying the actual list.
- Show that your list is still in its original order by printing it.
- Use sorted() to print your list in reverse alphabetical order without changing the order of the original list.
- Show that your list is still in its original order by printing it again.
- Use reverse() to change the order of your list. Print the list to show that its order has changed.
- Use reverse() to change the order of your list again. Print the list to show it’s back to its original order.
- Use sort() to change your list so it’s stored in alphabetical order.
- Print the list to show that its order has been changed.
- Use sort() to change your list so it’s stored in reverse alphabetical order.
- Print the list to show that its order has changed.
3-2. Dinner Guests: Working with one of the programs from Exercises through Introducing lists - Exercise 1, use len() to print a message indicating the number of people you are inviting to dinner.
Answer :
### Making a list
world = ['sigiriya','taj-mahal','everest','newyork','paris']
print(world)
### Using sorted() function
print(sorted(world))
print(world)
### Using sorted() reverse alphabetical order
print(sorted(world ,reverse = True))
print(world)
### Using a reverse() function
world.reverse()
print(world)
### Change it to original order
world.reverse()
print(world)
### Use sort() function
world.sort()
print(world)
### Use sort() reverse alphabet order
world.sort(reverse = True)
print(world)
Output :
['sigiriya', 'taj-mahal', 'everest', 'newyork', 'paris']
['everest', 'newyork', 'paris', 'sigiriya', 'taj-mahal']
['sigiriya', 'taj-mahal', 'everest', 'newyork', 'paris']
['taj-mahal', 'sigiriya', 'paris', 'newyork', 'everest']
['sigiriya', 'taj-mahal', 'everest', 'newyork', 'paris']
['paris', 'newyork', 'everest', 'taj-mahal', 'sigiriya']
['sigiriya', 'taj-mahal', 'everest', 'newyork', 'paris']
['everest', 'newyork', 'paris', 'sigiriya', 'taj-mahal']
['taj-mahal', 'sigiriya', 'paris', 'newyork', 'everest']
[Finished in 0.1s]