- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Create an array with ones at and below the given diagonal and zeros elsewhere with a different output type in Numpy

To create an array with ones at and below the given diagonal and zeros elsewhere, use the **numpy.tri()** method in Python Numpy

- The 1st parameter is the number of rows in the array
- The 2nd parameter is the number of columns in the array
- The "type" parameter is used to set the type of the returned array

The tri() function returns an array with its lower triangle filled with ones and zero elsewhere; in other words T[i,j] == 1 for j <= i + k, 0 otherwise.

## Steps

At first, import the required library −

import numpy as np

Now, create an array with ones at and below the given diagonal and zeros elsewhere, using the numpy.tri() method −

arr = np.tri(3, 5, dtype = int)

Displaying our array −

print("Array...\n",arr)

Get the datatype −

print("\nArray datatype...\n",arr.dtype)

Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim)

Get the shape of the Array −

print("\nOur Array Shape...\n",arr.shape)

Get the number of elements of the Array −

print("\nElements in the Array...\n",arr.size)

## Example

import numpy as np # To create an array with ones at and below the given diagonal and zeros elsewhere, use the numpy.tri() method in Python Numpy # The 1st parameter is the number of rows in the array # The 2nd parameter is the number of columns in the array # The "type" parameter is used to set the type of the returned array arr = np.tri(3, 5, dtype = int) # Displaying our array print("Array...\n",arr) # Get the datatype print("\nArray datatype...\n",arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n",arr.ndim) # Get the shape of the Array print("\nOur Array Shape...\n",arr.shape) # Get the number of elements of the Array print("\nElements in the Array...\n",arr.size)

## Output

Array... [[1 0 0 0 0] [1 1 0 0 0] [1 1 1 0 0]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (3, 5) Elements in the Array... 15

- Related Questions & Answers
- Create an array with ones at and below the given diagonal and zeros elsewhere in Numpy
- Create an array with ones below the main diagonal and zeros elsewhere in Numpy
- Create an array with ones above the main diagonal and zeros elsewhere in Numpy
- Return a 2-D array with ones on the diagonal and zeros elsewhere in Numpy
- Return a 2-D array with ones on the upper diagonal and zeros elsewhere in Numpy
- Return a 2-D array with ones on the lower diagonal and zeros elsewhere in Numpy
- Return a 2-D array with ones on the diagonal and zeros elsewhere but set a different datatype in Numpy
- Return a 2-D array with ones on the diagonal and zeros elsewhere and also set the number of columns in Numpy
- Return an array of ones with the same shape and type as a given array in Numpy
- Return an array of zeros with the same shape and type as a given array in Numpy
- Return a new array of given shape filled with a fill value and a different output type in Numpy
- Create an array class with possibly masked values and set a different dtype of output in Numpy
- Return a new array of given shape filled with zeros and also set the desired output in Numpy
- Power array elements of an array with a given value and display the result in a different type in Numpy
- Return a new array of given shape filled with ones and also set the desired data-type in Numpy

Advertisements