Python Program to Sort a List in Ascending Order

Introduction

Sorting a list in ascending order is a common task in programming, often required when dealing with datasets or organizing data. This tutorial will guide you through creating a Python program that sorts a given list of numbers in ascending order.

Example:

  • Input: [3, 5, 7, 2, 8]

  • Output: [2, 3, 5, 7, 8]

  • Input: [15, 22, 9, 3, 6, 27]

  • Output: [3, 6, 9, 15, 22, 27]

Problem Statement

Create a Python program that:

  • Takes a list of numbers as input.
  • Sorts the list in ascending order.
  • Displays the sorted list.

Solution Steps

  1. Take Input from the User: Use the input() function to get a list of numbers from the user.
  2. Convert Input to a List of Integers: Convert the input string to a list of integers.
  3. Sort the List: Use the sort() method or the sorted() function to sort the list in ascending order.
  4. Display the Sorted List: Use the print() function to display the sorted list.

Python Program

# Python Program to Sort a List in Ascending Order # Author: https://www.rameshfadatare.com/ # Step 1: Take input from the user input_list = input("Enter a list of numbers separated by spaces: ") # Step 2: Convert the input string to a list of integers numbers = list(map(int, input_list.split())) # Step 3: Sort the list in ascending order numbers.sort() # Or you can use: numbers = sorted(numbers) # Step 4: Display the sorted list print(f"The sorted list in ascending order is: {numbers}") 

Explanation

Step 1: Take Input from the User

  • The input() function prompts the user to enter a list of numbers, separated by spaces. The input is stored as a string in the variable input_list.

Step 2: Convert Input to a List of Integers

  • The split() method is used to split the input string into individual components, and map(int, ...) is used to convert these components into integers. The list() function then creates a list of these integers.

Step 3: Sort the List in Ascending Order

  • The sort() method is used to sort the list in place, modifying the original list to be in ascending order. Alternatively, the sorted() function can be used, which returns a new sorted list without modifying the original list.

Step 4: Display the Sorted List

  • The print() function is used to display the sorted list.

Output Example

Example 1:

Enter a list of numbers separated by spaces: 3 5 7 2 8 The sorted list in ascending order is: [2, 3, 5, 7, 8] 

Example 2:

Enter a list of numbers separated by spaces: 15 22 9 3 6 27 The sorted list in ascending order is: [3, 6, 9, 15, 22, 27] 

Example 3:

Enter a list of numbers separated by spaces: 1 100 50 23 The sorted list in ascending order is: [1, 23, 50, 100] 

Conclusion

This Python program demonstrates how to sort a list of numbers in ascending order using the sort() method or the sorted() function. This is a fundamental operation in Python programming, and understanding it is essential for working with lists and data structures effectively.

Leave a Comment

Scroll to Top