Python Articles

Page 138 of 855

Python Program to get the subarray from an array using a specified range of indices

Gireesha Devara
Gireesha Devara
Updated on 27-Mar-2026 15K+ Views

An array is a homogeneous data structure used to store elements of the same data type. Each element is identified by a key or index value. Python provides several ways to extract subarrays from arrays using specified index ranges. What is a Subarray? A subarray is a continuous portion of elements from an array. For example, if we have an array: numbers = [9, 3, 1, 6, 9] print("Original array:", numbers) Original array: [9, 3, 1, 6, 9] The possible subarrays include: [9, 3] [9, 3, 1] [3, ...

Read More

Python Program to push an array into another array

Gireesha Devara
Gireesha Devara
Updated on 27-Mar-2026 3K+ Views

In programming, an array is a data structure used to store a collection of homogeneous data elements. Each element is identified by an index value. Python doesn't have a specific array data type, so we use lists as arrays. Arrays in Python Here's how we represent a list as an array ? numbers = [1, 4, 6, 5, 3] print(numbers) [1, 4, 6, 5, 3] Python indexing starts from 0, so elements are accessed using index values 0, 1, 2, 3, 4. Pushing an array into another array means inserting ...

Read More

Python Program to convert an array into a string and join elements with a specified character

Gireesha Devara
Gireesha Devara
Updated on 27-Mar-2026 417 Views

An array is a data structure consisting of a collection of elements of the same data type, where each element is identified by an index. In Python, we use lists to represent arrays and can convert them to strings with custom separators. Arrays in Python Python uses the list data structure to represent arrays. Here's an example of a list representing an array − numbers = [10, 4, 11, 76, 99] print(numbers) [10, 4, 11, 76, 99] Input Output Scenarios Let's see how array elements can be joined with specified ...

Read More

Python Program To Determine If a Given Matrix is a Sparse Matrix

Gireesha Devara
Gireesha Devara
Updated on 27-Mar-2026 733 Views

A matrix is a rectangular array of numbers arranged in rows and columns. A sparse matrix is one that contains significantly more zero elements than non-zero elements − typically when more than half of the elements are zero. What is a Sparse Matrix? Consider this example matrix ? [0, 0, 3, 0, 0] [0, 1, 0, 0, 6] [1, 0, 0, 9, 0] [0, 0, 2, 0, 0] This 4×5 matrix has 20 total elements but only 6 non-zero values. Since 14 out of 20 elements are zero (70%), this qualifies as a sparse ...

Read More

Python Program to Display Upper Triangular Matrix

Gireesha Devara
Gireesha Devara
Updated on 27-Mar-2026 3K+ Views

A matrix is a two-dimensional array of numbers arranged in rows and columns. A square matrix (whose rows and columns have the same number of elements) has two diagonals. One is the primary diagonal − located from the top left corner to the bottom right corner. The second is the secondary diagonal − located from the top right to the bottom left corner. For a square matrix, if all the elements below the primary diagonal are zero, then it is called the Upper Triangular Matrix. # Example of upper triangular matrix matrix = [[1, 3, 4], ...

Read More

Python Program to Recursively Linearly Search an Element in an Array

Gireesha Devara
Gireesha Devara
Updated on 27-Mar-2026 3K+ Views

Linear search is the simplest method of searching for an element in an array. It is a sequential searching algorithm that starts from one end and checks every element of the array until the desired element is found. Recursion means a function that calls itself. When using a recursive function, we don't need any loop to generate iterations. The below syntax demonstrates the working of a simple recursion function ? def recursiveFunction(): # Statements # ... recursiveFunction() # ...

Read More

Python Program To Find the Trace and Normal of a given Matrix

Gireesha Devara
Gireesha Devara
Updated on 27-Mar-2026 3K+ Views

A matrix is defined as a set of numbers arranged in rows and columns. A matrix with m rows and n columns is called an m X n matrix and m and n are called its dimensions. A matrix is a two-dimensional array, which is created by using lists or NumPy arrays in Python. The Trace of a Matrix The trace of a matrix is defined as the sum of its diagonal elements (i.e., elements from upper left to lower right). Calculating the trace of a matrix is possible only for a square matrix (i.e., the matrix whose ...

Read More

Python Program to Multiply to Matrix Using Multi-dimensional Arrays

Gireesha Devara
Gireesha Devara
Updated on 27-Mar-2026 606 Views

A matrix is a set of numbers arranged in rows and columns. A matrix with m rows and n columns is called an m × n matrix and m and n are called its dimensions. In Python, matrices can be created using lists or NumPy arrays. Matrix multiplication can be done in two ways: standard matrix multiplication (dot product) where the number of columns in the first matrix must equal the number of rows in the second matrix, and element-wise multiplication where matrices must have the same dimensions. Matrix Multiplication Types For standard matrix multiplication, if we ...

Read More

Python Program to Add Two Matrix Using Multi-dimensional Array

Gireesha Devara
Gireesha Devara
Updated on 27-Mar-2026 1K+ Views

A matrix is a two-dimensional array of numbers arranged in rows and columns. The addition of two matrices involves adding corresponding elements and placing the sum in the corresponding position of the resultant matrix. This operation is only possible when both matrices have equal dimensions. In Python, multidimensional arrays are created using lists or NumPy arrays. The list data structure can accept lists as elements, making it easy to create matrices. The NumPy module provides numerous methods to work efficiently with multidimensional arrays. Matrix Addition Formula For two matrices A and B, the addition follows this pattern ...

Read More

Python Program to Interchange Elements of First and Last in a Matrix Across Columns

Gireesha Devara
Gireesha Devara
Updated on 27-Mar-2026 354 Views

A matrix is a two-dimensional array of numbers arranged in rows and columns. Python doesn't have a built-in matrix data type, but we can use nested lists or NumPy arrays to represent matrices. In this tutorial, we'll learn how to interchange the first and last column elements in each row of a matrix using different approaches. Input Output Scenarios Let's understand the problem with examples. For a 3×3 matrix, we swap the first and last elements of each row ? # Example 1: Square matrix original = [ [1, 3, 4], ...

Read More
Showing 1371–1380 of 8,549 articles
« Prev 1 136 137 138 139 140 855 Next »
Advertisements