Write a Python Program to Sort List in Ascending Order with practical example. Before you start, please refer List article to understand everything about Lists.

## Python Program to Sort List in Ascending Order

This python program will allow a user to enter any integer value. This is a length of a List. Next, we used For Loop to add numbers to the list.

**TIP: **Python Sort function will sort the List in Ascending Order

# Python Program to Sort List in Ascending Order NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) NumList.sort() print("Element After Sorting List in Ascending Order is : ", NumList)

**OUTPUT**

## Python Program to Sort List in Ascending Order without using Sort

In this program, we are using Nested For Loop to iterate each and every number in a List, and sort them in ascending order.

# Python Program to Sort List in Ascending Order NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) for i in range (Number): for j in range(i + 1, Number): if(NumList[i] > NumList[j]): temp = NumList[i] NumList[i] = NumList[j] NumList[j] = temp print("Element After Sorting List in Ascending Order is : ", NumList)

**OUTPUT**

**ANALYSIS**

**First For Loop – First Iteration: **for 0 in range(0, 4)

The condition is True so, it will enter into second for loop

**Nested For Loop – First Iteration:** for 1 in range(0 + 1, 4)

Condition is True so, compiler will enter into the If Statement

if(NumList[0] > NumList[1]) = if(67 > 86)

It means, Condition is False. So, it will exit from If block, and j value will be incremented by 1.

**Nested For Loop – Second Iteration:** for 2 in range(1, 4) – Condition is True

if(67 > 34) – Condition is True

temp = 67

NumList[i] = 34

NumList[j] = 67

Now the List will be **34 86 67 55**. Next, j will be increment by 1.

**Nested For Loop – Third Iteration:** for 3 in range(1, 4) – Condition is True

if(34 > 55) – Condition is False. So, compiler exit from the If block, and j value will be 4.

**Nested For Loop – Fourth Iteration:** for 4 in range(1, 4) – Condition is False

Next, i value will be incremented by 1.

**First For Loop – Second Iteration: **for 1 in range(0, 4)

The condition is True so, it will enter into second for loop

Do the same for the remaining Iterations

## Python Program to Sort List in Ascending Order using While Loop

This program is same as above. However, we replaced For Loop with While loop.

# Python Program to Sort List in Ascending Order NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) i = 0 while(i < Number): j = i + 1 while(j < Number): if(NumList[i] > NumList[j]): temp = NumList[i] NumList[i] = NumList[j] NumList[j] = temp j = j + 1 i = i + 1 print("Element After Sorting List in Ascending Order is : ", NumList)

**OUTPUT**