Python nhỏ nhất đến lớn nhất

Xin chào mọi người, đây là chương trình Python tìm ra số nhỏ nhất và lớn nhất trong danh sách

Số nhỏ nhất và lớn nhất trong danh sách — lập trình trong python. com

Ở đây chúng tôi sử dụng 2 hàm được xác định trước min() và max() để kiểm tra số nhỏ nhất và lớn nhất trong danh sách tương ứng

Vì, không giống như các ngôn ngữ lập trình khác, Python không có mảng, thay vào đó, nó có danh sách. Sử dụng danh sách dễ dàng và thoải mái hơn khi làm việc so với mảng. Hơn nữa, các chức năng sẵn có rộng lớn của Python, giúp công việc trở nên dễ dàng hơn. Vì vậy, bằng cách sử dụng các kỹ thuật này, hãy thử tìm các phạm vi khác nhau của số trong một danh sách nhất định.  

ví dụ

Đầu vào. danh sách = [12, 45, 2, 41, 31, 10, 8, 6, 4]
đầu ra.  
phần tử lớn nhất là. 45
phần tử nhỏ nhất là. 2
Phần tử lớn thứ hai là. 41
Phần tử nhỏ thứ hai là. 4

Đầu vào. danh sách = [22, 85, 62, 40, 55, 12, 39, 2, 43]
đầu ra
Phần tử lớn nhất là. 85
phần tử nhỏ nhất là. 2
Phần tử lớn thứ hai là. 62
Phần tử nhỏ thứ hai là. 12

Khuyến khích. Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp

Cách tiếp cận số 1. Cách tiếp cận đơn giản. Python cho phép chúng ta sắp xếp danh sách bằng hàm list(). Sử dụng điều này, chúng ta có thể tìm thấy các dãy số khác nhau trong một danh sách, từ vị trí của chúng, sau khi được sắp xếp. Giống như vị trí đầu tiên phải chứa phần tử nhỏ nhất và phần tử cuối cùng phải là phần tử lớn nhất.  

Python3




# Python prog to illustrate the following in a list

def find_len(list1):

    length=

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
0
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
1

    

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
3

    

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5_______0_______6
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
7
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
9
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
0_______0_______1

    

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5_______0_______6
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
7
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
1

    

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5_______0_______6
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
2
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
9
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
1

    0_______5_______0_______6# Python prog to illustrate the following in a list0

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
0_______0_______1

 

# Python prog to illustrate the following in a list4

# Python prog to illustrate the following in a list5=___# Python prog to illustrate the following in a list7# Python prog to illustrate the following in a list8# Python prog to illustrate the following in a list9def0# Python prog to illustrate the following in a list9

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5# Python prog to illustrate the following in a list9def4# Python prog to illustrate the following in a list9def6# Python prog to illustrate the following in a list9def8# Python prog to illustrate the following in a list9find_len(list1):0_______29_______9_______31_______2_______39_______5