Articles on Trending Technologies

Technical articles with clear explanations and examples

Python Program to Remove the Characters of Odd Index Values in a String

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 732 Views

When working with strings in Python, you might need to remove characters at odd index positions (1, 3, 5, etc.). This can be accomplished using various approaches including loops, string slicing, and list comprehensions. Understanding String Indexing In Python, string indexing starts from 0. So for a string "Hello": Index 0: 'H' (even) Index 1: 'e' (odd) Index 2: 'l' (even) Index 3: 'l' (odd) Index 4: 'o' (even) Method 1: Using While Loop This approach iterates through the string and skips characters at odd indices ? def remove_odd_index_characters(my_str): ...

Read More

Python Program for Depth First Binary Tree Search using Recursion

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 468 Views

Depth First Search (DFS) is a tree traversal algorithm that explores nodes by going as deep as possible before backtracking. In binary trees, DFS can be implemented recursively by visiting the current node, then traversing the left subtree, and finally the right subtree. Binary Tree Class Structure First, let's define a binary tree class with methods for insertion and DFS traversal ? class BinaryTree_struct: def __init__(self, key=None): self.key = key self.left = None ...

Read More

Python Program to Sort using a Binary Search Tree

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 852 Views

A Binary Search Tree (BST) is a tree data structure where the left subtree contains values less than the root, and the right subtree contains values greater than or equal to the root. The inorder traversal of a BST gives elements in sorted order, making it useful for sorting. How BST Sorting Works BST sorting involves two main steps: Insertion: Add elements to the BST maintaining the BST property Inorder Traversal: Visit nodes in left-root-right order to get sorted sequence Implementation We'll create two classes: BinSearchTreeNode for individual nodes and BinSearchTree for the ...

Read More

Vertical Concatenation in Matrix in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 490 Views

Vertical concatenation in Python matrix operations involves combining elements from corresponding positions across different rows. This creates new strings by joining elements column-wise rather than row-wise. Using zip_longest() for Vertical Concatenation The zip_longest() function from the itertools module handles matrices with unequal row lengths by filling missing values ? from itertools import zip_longest matrix = [["Hi", "Rob"], ["how", "are"], ["you"]] print("The matrix is:") print(matrix) result = ["".join(elem) for elem in zip_longest(*matrix, fillvalue="")] print("The matrix after vertical concatenation:") print(result) The matrix is: [['Hi', 'Rob'], ['how', 'are'], ['you']] The matrix after ...

Read More

Get Nth Column of Matrix in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 470 Views

When working with matrices in Python, you often need to extract a specific column. Python provides several methods to get the Nth column of a matrix, including list comprehension, the zip() function, and NumPy arrays. Using List Comprehension The most straightforward approach is using list comprehension to extract elements at a specific index ? matrix = [[34, 67, 89], [16, 27, 86], [48, 30, 0]] print("The matrix is:") print(matrix) N = 1 print(f"Getting column {N}:") # Extract Nth column using list comprehension nth_column = [row[N] for row in matrix] print(nth_column) ...

Read More

Matrix creation of n*n in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 723 Views

When it is required to create a matrix of dimension n * n, a list comprehension is used. This technique allows us to generate a square matrix with sequential numbers efficiently. Below is a demonstration of the same − Example N = 4 print("The value of N is") print(N) my_result = [list(range(1 + N * i, 1 + N * (i + 1))) for i in range(N)] print("The matrix of dimension N * N is:") print(my_result) Output ...

Read More

Python Program to Read Height in Centimeters and convert the Height to Feet and Inches

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 4K+ Views

Converting height from centimeters to feet and inches is a common unit conversion task. Python provides built-in functions like round() to handle decimal precision in calculations. Basic Conversion Method Here's a simple approach to convert centimeters to both feet and inches separately ? height_cm = int(input("Enter the height in centimeters: ")) # Convert to inches and feet height_inches = 0.394 * height_cm height_feet = 0.0328 * height_cm print("The height in inches is:", round(height_inches, 2)) print("The height in feet is:", round(height_feet, 2)) Enter the height in centimeters: 178 The height in inches ...

Read More

Python Program to Compute Simple Interest Given all the Required Values

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 391 Views

When it is required to compute simple interest when the principal amount, rate, and time are given, we can use the standard formula: Simple Interest = (Principal × Time × Rate) / 100. Below is a demonstration of the same − Simple Interest Formula The mathematical formula for calculating simple interest is: Simple Interest = (Principal × Time × Rate) / 100 Where: Principal − The initial amount of money Time − Duration in years Rate − Annual interest rate (percentage) ...

Read More

Python Program to Check if a Date is Valid and Print the Incremented Date if it is

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 644 Views

When it is required to check if a date is valid or not, and print the incremented date if it is a valid date, the 'if' condition is used along with date validation logic. Below is a demonstration of the same − Example my_date = input("Enter a date : ") dd, mm, yy = my_date.split('/') dd = int(dd) mm = int(mm) yy = int(yy) if(mm == 1 or mm == 3 or mm == 5 or mm == 7 or mm == 8 or mm == 10 or mm == 12): ...

Read More

Python Program to Read a Number n and Print the Natural Numbers Summation Pattern

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 475 Views

When it is required to read a number and print the pattern of summation of natural numbers, a simple for loop can be used. This pattern displays progressive sums like "1 = 1", "1 + 2 = 3", "1 + 2 + 3 = 6", and so on. Example my_num = int(input("Enter a number... ")) for j in range(1, my_num + 1): my_list = [] for i in range(1, j + 1): print(i, sep=" ", end=" ") ...

Read More
Showing 1–10 of 61,303 articles
« Prev 1 2 3 4 5 6131 Next »
Advertisements