O
O
overlord13372020-05-27 14:13:24
Python
overlord1337, 2020-05-27 14:13:24

4. Given a matrix D(4,4) of real numbers. How to replace all elements below the main diagonal with zeros?

wrote the code for this task, but the teacher said that this task can be completed without the if condition, immediately in those cycles that set the matrix. How to do it?

import random
n=4

a=[]
for i in range (n):
  b=[]
  for j in range (n):
    if i>j:
      b.append(0)
    else:
           b.append(random.randint(-100,100))
  a.append(b)
for i in a:
  for m in i:
    print(m, end=' ')
  print()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Petryaev, 2020-05-27
@Gremlin92

if(i<j)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question