21.12.2022 Views

python_para_desenvolvedores_2ed

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

132 Sobrecarga de operadores

float(self.__getitem__((x, y))))

r += '\n'

return r

if __name__ == '__main__':

mat = Mat()

print '2 itens preenchidos:'

mat[1, 2] = 3.14

mat[3, 4] = 4.5

print mat

print 'Troca e remoção:'

del mat[3, 4]

mat[1, 2] = 5.4

print mat

print 'Preenchendo a 3ª coluna:'

for i in xrange(1, 4):

mat[i + 1, 3] = i

print mat

print '3ª coluna:', mat.col(3)

print 'Fatia com 2ª a 3ª linha', mat[2:3]

print 'Somatório:', mat.sum(), 'Média', mat.avg()

Saída:

2 itens preenchidos:

Dim: (3, 4)

(1, 1) = 0.0 (1, 2) = 3.1 (1, 3) = 0.0 (1, 4) = 0.0

(2, 1) = 0.0 (2, 2) = 0.0 (2, 3) = 0.0 (2, 4) = 0.0

(3, 1) = 0.0 (3, 2) = 0.0 (3, 3) = 0.0 (3, 4) = 4.5

Troca e remoção:

Dim: (1, 2)

(1, 1) = 0.0 (1, 2) = 5.4

Preenchendo a 3ª coluna:

Dim: (4, 3)

(1, 1) = 0.0 (1, 2) = 5.4 (1, 3) = 0.0

(2, 1) = 0.0 (2, 2) = 0.0 (2, 3) = 1.0

(3, 1) = 0.0 (3, 2) = 0.0 (3, 3) = 2.0

(4, 1) = 0.0 (4, 2) = 0.0 (4, 3) = 3.0

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!