Sunday, November 23, 2025
HomeLanguagesnumpy.tri() in Python

numpy.tri() in Python

numpy.tri(R, C = None, k = 0, dtype = ‘float’) : Creates an array with 1’s at and below the given diagonal(about k) and 0’s elsewhere.
Parameters :

R     : Number of rows
C     : [optional] Number of columns; By default R = C
k     : [int, optional, 0 by default]
               Diagonal we require; k>0 means diagonal above main diagonal or vice versa.
dtype : [optional, float(byDefault)] Data type of returned array.  




# Python Program illustrating
# numpy.tri method
  
import numpy as geek
  
print("tri with k = 1 : \n",geek.tri(2, 3, 1, dtype = float), "\n")
  
print("tri with main diagonal : \n",geek.tri(3, 5, 0), "\n")
  
print("tri with k = -1 : \n",geek.tri(3, 5, -1), "\n")


Output :

tri with k = 1 : 
 [[ 1.  1.  0.]
 [ 1.  1.  1.]] 

tri with main diagonal : 
 [[ 1.  0.  0.  0.  0.]
 [ 1.  1.  0.  0.  0.]
 [ 1.  1.  1.  0.  0.]] 

tri with k = -1 : 
 [[ 0.  0.  0.  0.  0.]
 [ 1.  0.  0.  0.  0.]
 [ 1.  1.  0.  0.  0.]]  

References :
https://docs.scipy.org/doc/numpy/reference/generated/numpy.tri.html
Note :
These NumPy-Python programs won’t run on online IDE’s, so run them on your systems to explore them
.
This article is contributed by Mohit Gupta_OMG 😀. If you likeneveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on theneveropen main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32409 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6908 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS