Write a Python Program to Sort List in Ascending Order with a practical example.

## Python Program to Sort List in Ascending Order

This python program allows a user to enter any integer value, and we consider it is a length of a **List**. Next, we used For Loop to add numbers to the Python list.

**TIP: **Python sort function 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 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 Python For Loop – First Iteration: **for 0 in range(0, 4)

The condition is True. So, it enters into second for loop

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

Condition is True. So, it enters into the If Statement

if(NumList[0] > NumList[1]) = if(67 > 86) – It means the condition is False. So, it exits from If block, and j value 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 = **34 86 67 55**. Next, j increment by 1.

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

if(34 > 55) – Condition is False. So, it exits from the If block, and the j value is 4.

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

Next, i value incremented by 1.

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

The condition is True. So, it enters into second for loop

Do the same for the remaining Iterations

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

This Python program to sort list items in ascending is the 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**