Вопрос Что делают [ и ] в python?

Регистрация
25 Окт 2013
Сообщения
68
Репутация
0
Спасибо
0
Монет
0
d = [int(input()),int(input()),int(input())]

for i in range(3):

if d == max(d):

d = d * d

elif d == min(d):

d = d ** 0.5

print(*d)
 
Взятие элементов списка по индексу
 
в данном случае, i является индексом массива d, то есть указывает на элемент, используя его порядковый номер в массиве. если что, в питоне счёт в массиве начинается с нуля, тоесть первый символ массива имеет индекс 0
 
[int(input()),int(input()),int(input())] - объявили список из трех элементов - введенных строчек, преобразованных в целые числа.

d - обратились к списку d и запросили у него элемент с индексом i.
d[0] дает первый элемент списка, d[1] дает второй, d[2] дает третий.
d - зависит от значения переменной i.
 
In the given Python code, d is a list that contains the user input values, which are integers.

The for i in range(3) loop iterates through the list d using the variable i as the index.

The if d == max(d) statement checks if the current element of the list (i.e. d) is equal to the maximum value in the list (i.e. max(d)). If this condition is true, the current element is squared using the expression d = d * d.

The elif d == min(d) statement checks if the current element of the list is equal to the minimum value in the list. If this condition is true, the current element is square rooted using the expression d = d ** 0.5.

Finally, the program prints the modified list using the print(*d) statement, which prints all the elements of the list separated by a space.

So, this program squares the maximum element of the list and square roots the minimum element of the list and returns the modified list of the inputed integers.
 
Назад
Сверху