AmitDiwan has Published 10740 Articles

Python Program to Calculate the Number of Words and the Number of Characters Present in a String

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:24:38

1K+ Views

When it is required to calculate the number of words and characters present in a string, Below is the demonstration of the sameExample Live Demomy_string = "Hi there, how are you Will ? " print("The string is :") print(my_string) my_chars=0 my_words=1 for i in my_string:    my_chars=my_chars+1    if(i==' '):   ... Read More

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

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:22:23

695 Views

When it is required to remove characters from odd indices of a string, a method is defined that takes the string as parameter.Below is the demonstration of the same −Example Live Demodef remove_odd_index_characters(my_str):    new_string = ""    i = 0    while i < len(my_str):       if (i ... Read More

Python Program for Depth First Binary Tree Search using Recursion

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:21:22

432 Views

When it is required to perform depth first search on a tree using recursion, a class is defined, and methods are defined on it that help perform breadth first search.Below is a demonstration for the same −Example Live Democlass BinaryTree_struct:    def __init__(self, key=None):       self.key = key   ... Read More

Python Program to Sort using a Binary Search Tree

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:19:41

823 Views

When it is required to sort a binary search tree, a class is created, and methods are defined inside it that perform operations like inserting an element, and performing inorder traversal.Below is a demonstration of the same −Exampleclass BinSearchTreeNode:    def __init__(self, key):       self.key = key   ... Read More

Vertical Concatenation in Matrix in Python

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:15:42

463 Views

When it is required to concatenate a matrix vertically, the list comprehension can be used.Below is a demonstration of the same −Example Live Demofrom itertools import zip_longest my_list = [["Hi", "Rob"], ["how", "are"], ["you"]] print("The list is : ") print(my_list) my_result = ["".join(elem) for elem in zip_longest(*my_list, fillvalue ... Read More

Get Nth Column of Matrix in Python

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:14:57

438 Views

When it is required to get the ‘n’th column of a matrix, the ‘any’ method can be used.Below is a demonstration of the same −Example Live Demomy_list = [[34, 67, 89], [16, 27, 86], [48, 30, 0]] print("The list is : ") print(my_list) N = 1 print("The value of ... Read More

Matrix creation of n*n in Python

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:12:23

696 Views

When it is required to create a matrix of dimension n * n, a list comprehension is used.Below is a demonstration of the same −Example Live DemoN = 4 print("The value of N is ") print(N) my_result = [list(range(1 + N * i, 1 + N * (i + 1))) ... Read More

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

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:11:44

4K+ Views

When it is required to read the height in ‘cm’ and convert it into ‘feet’ and ‘inches’, the ‘round’ method can be used.Below is a demonstration of the same −Example Live Demoin_cm=int(input("Enter the height in centimeters...")) in_inches=0.394*in_cm in_feet=0.0328*in_cm print("The length in inches is ") print(round(in_inches, 2)) print("The length in feet is") ... Read More

Python Program to Compute Simple Interest Given all the Required Values

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:11:26

360 Views

When it is required to compute a simple interest when the amount, rate and interest are given, a simple formula can be defined, and the elements can be plugged into the formula.Below is a demonstration of the same −Example Live Demoprinciple_amt = float(input("Enter the principle amount...")) my_time = int(input("Enter the time ... Read More

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

AmitDiwan

AmitDiwan

Updated on 16-Apr-2021 12:11:08

617 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.Below is a demonstration of the same −Example Live Demomy_date = input("Enter a date : ") dd, mm, yy = my_date.split('/') dd=int(dd) mm=int(mm) ... Read More

Advertisements