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.

Python sort function sort the List items 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)

Sorting Python List in ascending order output

```
Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : 56
Please enter the Value of 2 Element : 76
Please enter the Value of 3 Element : 44
Please enter the Value of 4 Element : 2
Element After Sorting List in Ascending Order is : [2, 44, 56, 76]
```

## 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)

Sorting Python List in ascending order output

```
Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : 67
Please enter the Value of 2 Element : 86
Please enter the Value of 3 Element : 34
Please enter the Value of 4 Element : 55
Element After Sorting List in Ascending Order is : [34, 55, 67, 86]
```

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 Python 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)