Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Gireesha Devara
Page 5 of 18
Python Program to Recursively Linearly Search an Element in an Array
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 MorePython Program To Find the Trace and Normal of a given Matrix
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 MorePython Program to Multiply to Matrix Using Multi-dimensional Arrays
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 MorePython Program to Add Two Matrix Using Multi-dimensional Array
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 MorePython Program to Interchange Elements of First and Last in a Matrix Across Columns
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 MorePython Program to Interchange Elements of First and Last in a Matrix Across Rows
A matrix is a set of numbers arranged in rows and columns format. In Python, a matrix can be created using nested lists or NumPy arrays. This tutorial demonstrates how to interchange the first and last rows of a matrix. Input Output Scenarios Let's look at examples of interchanging first and last rows in different matrices ? Input matrix: [1, 2, 3] [4, 5, 6] [7, 8, 9] Output matrix: [7, 8, 9] [4, 5, 6] [1, 2, 3] For matrices with unequal row lengths ? Input matrix: ...
Read MoreHow do I create a constant in Python?
In Python, there's no built-in data type for constants like other programming languages. However, Python follows naming conventions and provides several approaches to create constants that signal to other developers that a value shouldn't be changed. Built-in Constants in Python Python has six built-in constants: False, True, None, NotImplemented, Ellipsis (...), and __debug__. These cannot be reassigned ? # Trying to reassign a built-in constant raises SyntaxError try: False = 100 except SyntaxError as e: print(f"Error: {e}") File "", line 2 ...
Read MoreHow to flatten a shallow list in Python?
Flattening a shallow list means converting a nested list into a simple, single-dimensional list. In other words, converting a multidimensional list into a one-dimensional list. Flattening can be performed using different techniques like nested for loops, list comprehensions, list concatenation, and using built-in functions. In this article, we will discuss a few techniques to flatten a shallow Python list. Flattening a shallow list using a nested for loop By using a nested for loop and a list.append() method, we can flatten the shallow list. Let’s have a look and see how this can be done in a program. Example This simple example ...
Read MoreHow to create Python objects based on an XML file?
XML (Extensible Markup Language) is a markup language used to structure, store, and transfer data between systems. Python developers often need to read and process XML data in their applications. The untangle library provides a simple way to create Python objects based on an XML file. This small Python library converts an XML document into a Python object with an intuitive API. Syntax untangle.parse(filename, **parser_features) Parameters filename: Can be an XML string, XML filename, or URL parser_features: Extra arguments passed to parser.setFeature() Return Value Returns a Python object representing the parsed XML document. ...
Read MoreHow to prevent loops going into infinite mode in Python?
In Python, while loops can run infinitely if the loop condition never becomes False. To prevent infinite loops, you need to modify the condition variable inside the loop body or use control statements like break. Using a Counter Variable The most common approach is to use a counter that gets incremented in each iteration ? count = 0 while count < 5: print('Python!') count += 1 Python! Python! Python! Python! Python! Here, the count variable is incremented in each iteration, ensuring the condition ...
Read More