Python Articles

Page 140 of 855

Python Program to Sort using a Binary Search Tree

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 853 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 471 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 725 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

Python Program to Read a Number n And Print the Series "1+2+.....+n=

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 445 Views

When it is required to display the sum of all natural numbers within a given range and show the series format "1+2+...+n=sum", we can create a program that builds the series string and calculates the sum. Below is a demonstration of the same − Method 1: Using Loop to Build Series Example def print_series_sum(n): series = "" total = 0 for i in range(1, n + 1): total += i ...

Read More

Python Program to Print all Integers that are not Divisible by Either 2 or 3 and Lie between 1 and 50

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

When it is required to print all integers which are not divisible by either 2 or 3 and lie between 1 and 50, we can use a while loop with conditional statements to check each number. Numbers that satisfy both conditions (not divisible by 2 and not divisible by 3) will be printed. Example The following example demonstrates how to find integers between 1 and 50 that are not divisible by either 2 or 3 ? print("Integers not divisible by 2 and 3, that lie between 1 and 50 are:") n = 1 while n

Read More
Showing 1391–1400 of 8,549 articles
« Prev 1 138 139 140 141 142 855 Next »
Advertisements