xxxxxxxxxx
from scipy import sparse
# Creating a sparse diagonal matrix. The code will work for any type of
# sparse matrix.
A = sparse.diags(np.arange(5))
# Adding a diagonal filled with 2s in the first diagonal above the main
# diagonal
A.setdiag(2, k=1)
# Adding a diagonal with values 3 and 4 in the second diagonal below the
# main diagonal
A.setdiag([3, 4], k=-2)
# To visualize the diagonal you can use
print(A.toarray())
# or
print(A.todense())