π Python Crash Course – Chapter 3: Introducing Lists – Exercise 3-8 Solution
Welcome to another solution from Python Crash Course by Eric Matthes.
π Exercise 3-8
Task:
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 chang
ing 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
✅ Solution Code:
location = ['india','america','japan','china','russia','finland']
print(location)
print("----------------------------------------------------------------------")
sorted_location =sorted(location)
print(f'Sorted Location : {sorted_location}')
print("----------------------------------------------------------------------")
print(f'Location list is still in Its Original order: {location}')
print("----------------------------------------------------------------------")
location.reverse() # list is reversed
print(f"Now the Location List is Reversed : {location}")
print("----------------------------------------------------------------------")
location.reverse() # list is reversed again
print(f"Now the Location List is Reversed again : {location}")
print("----------------------------------------------------------------------")
location.sort()
print(f"Now the Location List is sorted in ascending order : {location}")
print("----------------------------------------------------------------------")
location.sort(reverse=True)
print(f"Now the Location List is sorted in descending order : {location}")
print("----------------------------------------------------------------------")
π§ Code Explanation:
This Python code demonstrates how to manipulate a list of locations using various list methods and functions. Here's how it works:
-
Create a list:
The variable
location
is assigned a list of places:['india', 'america', 'japan', 'china', 'russia', 'finland']
. The list is not in alphabetical order. -
Print the original list:
The
print()
function is used to display the list in its original order. -
Sort the list temporarily:
The
sorted()
function is used to display the list in alphabetical order without modifying the original list. The sorted list is stored in the variablesorted_location
. -
Show the original list:
The original list is printed again to confirm that it has not been modified.
-
Reverse the list:
The
reverse()
method is used to reverse the order of the list. The modified list is printed to show the change. -
Reverse the list again:
The
reverse()
method is used again to restore the list to its original order. The modified list is printed to confirm the change. -
Sort the list in ascending order:
The
sort()
method is used to permanently sort the list in alphabetical order. The sorted list is printed. -
Sort the list in descending order:
The
sort(reverse=True)
method is used to permanently sort the list in reverse alphabetical order. The sorted list is printed.
π Output:
['india', 'america', 'japan', 'china', 'russia', 'finland']
----------------------------------------------------------------------
Sorted Location : ['america', 'china', 'finland', 'india', 'japan', 'russia']
----------------------------------------------------------------------
Location list is still in Its Original order: ['india', 'america', 'japan', 'china', 'russia', 'finland']
----------------------------------------------------------------------
Now the Location List is Reversed : ['finland', 'russia', 'china', 'japan', 'america', 'india']
----------------------------------------------------------------------
Now the Location List is Reversed again : ['india', 'america', 'japan', 'china', 'russia', 'finland']
----------------------------------------------------------------------
Now the Location List is sorted in ascending order : ['america', 'china', 'finland', 'india', 'japan', 'russia']
----------------------------------------------------------------------
Now the Location List is sorted in descending order : ['russia', 'japan', 'india', 'finland', 'china', 'america']
----------------------------------------------------------------------
π Related Exercises from Chapter 3:
- ➤ Exercise 3-1 -> Names
- ➤ Exercise 3-2 -> Greetings
- ➤ Exercise 3-3 -> Your Own List
- ➤ Exercise 3-4 -> Guest List
- ➤ Exercise 3-5 -> Changing Guest List
- ➤ Exercise 3-6 -> More Guests
- ➤ Exercise 3-7 -> Shrinking Guest List
- ➤ Exercise 3-8 -> Seeing the World
- ➤ Exercise 3-9 -> Dinner Guests
- ➤ Exercise 3-10 -> Every Function
π Connect With Us:
“The only way to learn a new programming language is by writing programs in it.”
– Dennis Ritchie
π Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips #introducingLists
πTrending Topics
π Connect With Us:
“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment