Hướng dẫn how do you print the largest and smallest number in python? - làm thế nào để bạn in số lớn nhất và nhỏ nhất trong python?

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

Số lượng nhỏ nhất và lớn nhất trong danh sách - Lập trìnhDInpython.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.

Nhiệm vụ :

Để tìm số lớn nhất và nhỏ nhất trong một danh sách.

Cách tiếp cận :

  • Đọc số đầu vào yêu cầu độ dài của danh sách bằng cách sử dụng input() hoặc raw_input().
  • Khởi tạo một danh sách trống lst = [].
  • Đọc từng số bằng cách sử dụng for loop.
  • Trong vòng lặp FOR, nối mỗi số vào danh sách.
  • Bây giờ chúng tôi sử dụng hàm được xác định trước
    Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
    Output : 
    Largest element is: 45
    Smallest element is: 2
    Second Largest element is: 41
    Second Smallest element is: 4
    0 để tìm phần tử lớn nhất trong danh sách.
  • Tương tự, chúng tôi sử dụng một chức năng được xác định trước khác
    Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
    Output : 
    Largest element is: 45
    Smallest element is: 2
    Second Largest element is: 41
    Second Smallest element is: 4
    1 để tìm phần tử nhỏ nhất trong danh sách.

Chương trình :

lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Maximum element in the list is :", max(lst), "\nMinimum element in the list is :", min(lst))

Đầu ra:

Số lượng nhỏ nhất và lớn nhất trong danh sách - Lập trìnhDInpython.com

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 hơn và thoải mái hơn để làm việc so với các mảng. Hơn nữa, các chức năng khổng lồ của Python, làm cho nhiệm vụ dễ dàng hơn. Vì vậy, sử dụng các kỹ thuật này, hãy để cố gắng tìm các phạm vi khác nhau của số trong một danh sách nhất định. & NBSP;

Examples:

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12

Cách tiếp cận số 1: Cách tiếp cận rất đơn giản. Python cho phép chúng tôi sắp xếp một danh sách bằng hàm Danh sách (). Sử dụng điều này, chúng ta có thể tìm thấy các phạm vi 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à lớn nhất. & NBSP;The approach is simple. Python allows us to sort a list using the list() function. Using this we can find various ranges of numbers in a list, from their position, after being sorted. Like the first position must contain the smallest and the last element must be the greatest. 

Python3

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
2
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
3

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
7
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
0

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
3
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
5
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
6
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
7
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
8

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
1
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
4
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
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
8

Các

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
4
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
3
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
7
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
8

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
1
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
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
3
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
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
5
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
5input()0
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input()2
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input()4
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input()6
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input()8
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5raw_input()0raw_input()1

raw_input()2

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6 raw_input()4

Output:

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

Cách tiếp cận số 2: Dưới đây là một phương pháp truyền thống khác để thực hiện tính toán sau. Thuật toán rất đơn giản, chúng tôi lấy một số và so sánh nó với tất cả các số khác có trong danh sách và nhận phần tử nhỏ nhất, nhỏ nhất, lớn nhất và thứ hai nhỏ nhất và thứ hai. & NBSP;Below is another traditional method to do the following calculation. The algorithm is simple, we take a number and compare it with all other numbers present in the list and get the largest, smallest, second largest, and second smallest element. 

Python3

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
2 raw_input()6raw_input()7

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4raw_input()9
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6 lst = []1
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4raw_input()1

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4lst = []5
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6 lst = []1
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4raw_input()1

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4for loop1
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6 for loop3

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4for loop5
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6 for loop3

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4for loop9
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
00
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
01 lst = []1
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
7
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
04

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
05
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
06
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
07

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
08for loop1
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
11

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
08raw_input()9
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
15

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
05
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
17 for loop1
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
4 for loop3
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
21
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
22

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
08for loop1
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
15

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
05
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
06
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
29

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
08for loop5
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
33

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
08lst = []55____16
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
15

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
05
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
17 for loop5
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
4 for loop3
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
21
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
44

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
08for loop5
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
15

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
3
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
4
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
53

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
1
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
4
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
58

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
4
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
63

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
4
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
68

Các

raw_input()6

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8

Không gian phụ trợ: O (1)

Output:

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

Cách tiếp cận số 3: Nhiệm vụ này có thể được thực hiện bằng các phương thức danh sách tối đa và pop. Chúng ta có thể tìm thấy phần tử lớn nhất và nhỏ nhất của danh sách bằng phương pháp tối đa và tối thiểu sau khi nhận được phần tử tối đa và tối đa vượt qua các phần tử từ danh sách và một lần nữa sử dụng phần tử tối đa và tối đa để có phần tử nhỏ thứ hai và nhỏ thứ hai. & NBSP;This task can be performed using max and pop methods of list. We can find largest and smallest element of list using max and min method after getting min and max element pop outs the elements from list and again use min and max element to get the second largest and second smallest element. 

Python3

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
2
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
93

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
95
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
97
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
98

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
00

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
02
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
97
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
8

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
07
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
09
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
98

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
12

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
14
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
09
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
98

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
22220
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
21
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
22

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
20
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
26
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
27

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
20
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
31
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
32

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
2
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
20
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
36
Input : list = [22, 85, 62, 40, 55, 12, 39, 2, 43]
Output :
Largest element is: 85
Smallest element is: 2
Second Largest element is: 62
Second Smallest element is: 12
37

Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
1
Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
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
3
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
4
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
5
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
5input()0
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input()2
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input()4
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input()6
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5input()8
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
5raw_input()0raw_input()1

raw_input()2

Input : list = [12, 45, 2, 41, 31, 10, 8, 6, 4]
Output : 
Largest element is: 45
Smallest element is: 2
Second Largest element is: 41
Second Smallest element is: 4
6 raw_input()4

Output:

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

Làm thế nào để bạn in số lớn nhất trong Python?

Hàm Max () in phần tử lớn nhất trong danh sách.max() function prints the largest element in the list.

Làm thế nào để tôi tìm thấy số lớn nhất và nhỏ nhất trong Python sử dụng cho Loop?

Trong vòng lặp FOR, nối mỗi số vào danh sách.Bây giờ chúng tôi sử dụng hàm được xác định trước Max () để tìm phần tử lớn nhất trong danh sách.Tương tự, chúng tôi sử dụng một hàm được xác định trước khác () để tìm phần tử nhỏ nhất trong danh sách.

Làm thế nào để bạn tìm thấy số lớn nhất và nhỏ nhất trong một mảng trong Python?

Thuật toán để tìm các số nhỏ nhất và lớn nhất trong một mảng..
Nhập các phần tử mảng ..
Khởi tạo nhỏ = lớn = mảng [0].
Lặp lại từ i = 2 đến n ..
if (mảng [i]> lớn).
lớn = mảng [i].
if (mảng [i]
nhỏ = mảng [i].
In nhỏ và lớn ..