ข้ามไปยังเนื้อหาหลัก

Sorting in Python Tutorial

Discover what the sort method is and how to do it.
22 ก.ค. 2563  · 1 นาที อ่าน

สำรวจด้วย AI

ChatGPTClaudePerplexity

The sort() method is a built-in Python method that, by default, sorts the list in ascending order. However, you can modify the order from ascending to descending by specifying the sorting criteria.

sort() Method

Let's say you want to sort the element in prices in ascending order. You would type prices followed by a . (period) followed by the method name, i.e., sort including the parentheses.

prices = [238.11, 237.81, 238.91]
prices.sort()
print(prices)
[237.81, 238.11, 238.91]

Printing Sorted Lists

Again, using a similar example as above, we will use the .sort() method to sort and print the prices in the list named prices.

prices = [159.54, 37.13, 71.17]
prices.sort()
print(prices)
[37.13, 71.17, 159.54]

Try it for yourself.

To learn more about sorting in Python, please see this video from our course Introduction to Python for Finance.

This content was taken from DataCamp’s Introduction to Python for Finance course by Adina Howe.

หัวข้อ
Python
Data Science

Other Python Courses

Courses

Python เบื้องต้น

4 ชม.
7M
เชี่ยวชาญพื้นฐานการวิเคราะห์ข้อมูลด้วย Python ในเวลาเพียงสี่ชั่วโมง คอร์สออนไลน์นี้จะแนะนำอินเทอร์เฟซ Python และสำรวจแพ็กเกจยอดนิยม
ดูรายละเอียดRight Arrow
เริ่มหลักสูตร

Courses

Python เบื้องต้นสำหรับการเงิน

4 ชม.
106.7K
พัฒนาทักษะ Python เพื่อยกระดับอาชีพการเงินของคุณ เรียนรู้การทำงานกับรายการ อาร์เรย์ และการแสดงภาพข้อมูล เพื่อเชี่ยวชาญการวิเคราะห์ทางการเงิน
ดูเพิ่มเติมRight Arrow
ที่เกี่ยวข้อง

Tutorials

How to Sort a Dictionary by Value in Python

Learn efficient methods to sort a dictionary by values in Python. Discover ascending and descending order sorting and bonus tips for key sorting.
Neetika Khandelwal's photo

Neetika Khandelwal

5 นาที

Tutorials

Python Merge Sort Tutorial

Learn everything you need to know about the merge sort operation in Python and how to implement this critical algorithm for sorting large databases.
Javier Canales Luna's photo

Javier Canales Luna

8 นาที

Tutorials

Python Dictionary Tutorial

In this Python tutorial, you'll learn how to create a dictionary, load data in it, filter, get and sort the values, and perform other dictionary operations.
DataCamp Team's photo

DataCamp Team

14 นาที

Tutorials

Python Tutorial for Beginners

Get a step-by-step guide on how to install Python and use it for basic data science functions.
Matthew Przybyla's photo

Matthew Przybyla

12 นาที

Tutorials

How to Sort Data in R: A Complete Tutorial

Learn how to sort vectors, data frames, and more in R using order(), sort(), and dplyr's arrange() with practical examples.

Olivia Smith

6 นาที

Tutorials

Tuples in Python Tutorial

Learn to use and unpack Tuples in Python.
DataCamp Team's photo

DataCamp Team

3 นาที

ดูเพิ่มเติมดูเพิ่มเติม