Exercise 3-8 Solution Python Crash Course Chapter 3: Introducing Lists

 

πŸ“˜ 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:

  1. 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.

  2. Print the original list:

    The print() function is used to display the list in its original order.

  3. 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 variable sorted_location.

  4. Show the original list:

    The original list is printed again to confirm that it has not been modified.

  5. Reverse the list:

    The reverse() method is used to reverse the order of the list. The modified list is printed to show the change.

  6. 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.

  7. Sort the list in ascending order:

    The sort() method is used to permanently sort the list in alphabetical order. The sorted list is printed.

  8. 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:


πŸ”— 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

“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