Sorting numbers is a common task in programming and Python offers various methods to do it. In this tutorial, we will learn how to sort numbers in Python using multiple techniques, including the **sorted()** function, **sort()** method, and sorting with **lambda** functions.

### Step 1: Using the sorted() function

The **sorted()** function is a built-in Python function that allows you to sort a sequence of numbers in ascending or descending order. This function returns a sorted list and does not modify the original sequence.

Here’s an example:

1 2 3 4 |
numbers = [34, 67, 9, 1, 54, 32] sorted_numbers = sorted(numbers) print(sorted_numbers) |

**Output:**

[1, 9, 32, 34, 54, 67]

We can also sort the numbers in descending order by setting the **reverse** parameter to **True**.

1 2 |
descending_numbers = sorted(numbers, reverse=True) print(descending_numbers) |

**Output:**

[67, 54, 34, 32, 9, 1]

### Step 2: Using the sort() method

The **sort()** method is a list method that sorts the elements of a list in ascending or descending order. This method sorts the list in-place and does not return a new sorted list. To use this method, call the **sort()** method on the list object.

Here’s an example:

1 2 3 4 |
numbers = [34, 67, 9, 1, 54, 32] numbers.sort() print(numbers) |

**Output:**

[1, 9, 32, 34, 54, 67]

To sort the list in descending order, set the **reverse** parameter to **True**.

1 2 |
numbers.sort(reverse=True) print(numbers) |

**Output:**

[67, 54, 34, 32, 9, 1]

### Step 3: Sorting with lambda functions

In some cases, we may need to use custom sorting criteria. Python provides the **key** parameter in both **sorted()** function and **sort()** method, which allows you to specify a custom function to determine the sorting order.

Here’s an example of sorting a list of numbers based on their absolute values:

1 2 3 4 |
numbers = [34, -67, 9, -1, 54, -32] sorted_numbers = sorted(numbers, key=lambda x: abs(x)) print(sorted_numbers) |

**Output:**

[-1, 9, 34, -32, 54, -67]

In this example, the **key** parameter evaluates each number’s absolute value and sorts the list based on that.

## Full Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
numbers = [34, 67, 9, 1, 54, 32] sorted_numbers = sorted(numbers) print(sorted_numbers) descending_numbers = sorted(numbers, reverse=True) print(descending_numbers) numbers.sort() print(numbers) numbers.sort(reverse=True) print(numbers) numbers = [34, -67, 9, -1, 54, -32] sorted_numbers = sorted(numbers, key=lambda x: abs(x)) print(sorted_numbers) |

## Conclusion

In this tutorial, we have learned how to sort numbers in Python using the **sorted()** function, **sort()** method, and sorting with **lambda** functions. These methods give you flexibility in sorting sequences based on different criteria. It’s important to remember that the **sorted()** function returns a new sorted list, while the **sort()** method modifies the list in-place.