In this Python program, we will learn how to sort a list in ascending order. In this program, for sorting a list we used the python built-in function such as

, **sort()**** sorted()** and without using the sorting function.

Here is the source code of the program to sort a list in ascending order.

In this program, we used the python built-in function

to sort the list in ascending order by default. **sort()**

The ** sort() **function takes two parameters:

**reverse** = the first parameter is optional.if

then it returns a sorted list in descending order.**reverse = True**

**Key** = second Parameter. It is also optional and this function used to specify the sorting criteria(s)

```
# Python Program to Sort a List in an Ascending Order Using sort() Method
NumList = [12,45,87,49,56,46]
# Print the List
print("\nElement Before Sorting is: ", NumList)
NumList.sort()
print("\nElement After Sorting List in Ascending Order is: \n", NumList)
```

Element Before Sorting is: [12, 45, 87, 49, 56, 46]

Element After Sorting List in Ascending Order is:

[12, 45, 46, 49, 56, 87]

In this program, we used the python built-in

function to sort the list in ascending order. Python lists have a built-in **sorted()**

function that builds a new sorted list from an iterable. **sorted()**

have a **sorted()***key* parameter to specify a function to be called on each list element prior to making comparisons.

```
# Python Program to Sort a List in an Ascending Order Using sorted() Method
NumList = [12,45,87,49,56,46]
# Print the List
print("\nElement Before Sorting is: ", NumList)
new_NumList=sorted(NumList)
print("\nElement After Sorting List in Ascending Order is: \n", new_NumList)
```

Element Before Sorting is: [12, 45, 87, 49, 56, 46]

Element After Sorting List in Ascending Order is:

[12, 45, 46, 49, 56, 87]

```
# Python Program to Sort a List in an Ascending Order Using For Loop
NumList = [12,45,87,49,56,46]
# Print the List
print("\nElement Before Sorting is: ", NumList)
Number=len(NumList)
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("\nElement After Sorting List in Ascending Order is: \n", NumList)
```

Element Before Sorting is: [12, 45, 87, 49, 56, 46]

Element After Sorting List in Ascending Order is:

[12, 45, 46, 49, 56, 87]

```
# Python Program to Sort a List in Ascending Order Using While Loop
NumList = [12,45,87,49,56,46]
# Print the List
print("\nElement Before Sorting is: ", NumList)
Number=len(NumList)
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("\nElement After Sorting List in Ascending Order is: \n", NumList)
```

Element Before Sorting is: [12, 45, 87, 49, 56, 46]

Element After Sorting List in Ascending Order is:

[12, 45, 46, 49, 56, 87]

## Comments