Python Знайти максимальний та мінімальний елемент списку. Знайти номер максимального та
мінімальрого елемента списку. from random import randint l=[randint(20,100) for i in range(20)] print(l) print(max(l)) a=l[1] N=0 for i in range(20): if l[i]>a: a=l[i] N=i+1 print(a) print(N) print(min(l)) b=l[1] x=0 for i in range(20): if l[i] b=l[i] x=i+1 print(b) print(x)Ответы на вопрос
from random import *
l=[randint(20,100) for i in range(20)]
print(l)
print(max(l))
print(min(l))
print(l.index(max(l)))
print(l.index(min(l)))
To find the maximum and minimum elements of a list, as well as their corresponding indices, you can use the following Python code:
```python from random import randint
l = [randint(20, 100) for i in range(20)] print(l)
max_element = max(l) min_element = min(l)
max_index = l.index(max_element) min_index = l.index(min_element)
print("Maximum element:", max_element) print("Index of maximum element:", max_index)
print("Minimum element:", min_element) print("Index of minimum element:", min_index) ```
Let's break down the code:
1. We import the `randint` function from the `random` module to generate random integers. 2. We create a list `l` using a list comprehension. This list contains 20 random integers between 20 and 100. 3. We print the list `l` to see the generated elements. 4. We use the `max()` function to find the maximum element in the list and store it in the variable `max_element`. 5. We use the `min()` function to find the minimum element in the list and store it in the variable `min_element`. 6. We use the `index()` method to find the index of the maximum and minimum elements in the list and store them in the variables `max_index` and `min_index`, respectively. 7. Finally, we print the maximum element, its index, the minimum element, and its index.
Please note that the code you provided is not formatted correctly. I have corrected the formatting and added the necessary code to find the maximum and minimum elements and their indices.
Похожие вопросы
Топ вопросов за вчера в категории Информатика
Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili
