Python Articles

Page 48 of 855

Python Program to Calculate the Length of a String Without Using a Library Function

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

When it is required to calculate the length of a string without using library methods, a counter is used to increment every time an element of the string is encountered.Below is the demonstration of the same −Examplemy_string = "Hi Will" print("The string is :") print(my_string) my_counter=0 for i in my_string:    my_counter=my_counter+1 print("The length of the string is ") print(my_counter)OutputThe string is : Hi Will The length of the string is 7ExplanationA string is defined, and is displayed on the console.A counter is initialized to 0.The string is iterated over, and after every element is iterated over, the counter is ...

Read More

Python Program for Depth First Binary Tree Search using Recursion

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 461 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 −Exampleclass BinaryTree_struct:    def __init__(self, key=None):       self.key = key       self.left = None       self.right = None    def set_root(self, key):       self.key = key    def insert_at_left(self, new_node):       self.left = new_node    def insert_at_right(self, new_node):       self.right = new_node    def search_elem(self, key):     ...

Read More

Python Program to Find the Largest value in a Tree using Inorder Traversal

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 250 Views

When it is required to find the largest value in a tree using in order traversal, a binary tree class is created with methods to set the root element, perform in order traversal using recursion and so on.An instance of the class is created, and it can be used to access the methods.Below is the demonstration of the same −Exampleclass BinaryTree_Struct:    def __init__(self, key=None):       self.key = key       self.left = None       self.right = None    def set_root(self, key):       self.key = key    def inorder_traversal_largest(self):     ...

Read More

Python Program to Print only Nodes in Left SubTree

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 324 Views

When it is required to print the nodes in the left subtree, a class can be created that consists of methods can be defined to set the root node, perform in order traversal, insert elements to the right of the root node, to the left of the root node, and so on. An instance of the class is created, and the methods can be used to perform the required operations.Below is a demonstration of the same −Exampleclass BinaryTree_struct:    def __init__(self, data=None):       self.key = data       self.left = None       self.right = None ...

Read More

Python Program to Display the Nodes of a Tree using BFS Traversal

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 962 Views

When it is required to display the nodes of a tree using the breadth first search traversal, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, perform ‘bfs’ (breadth first search) and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Exampleclass Tree_struct:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data):       self.key = data    def ...

Read More

Python Program to Find All Connected Components using DFS in an Undirected Graph

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ Views

When it is required to find all the connected components using depth first search in an undirected graph, a class is defined that contains methods to initialize values, perform depth first search traversal, find the connected components, add nodes to the graph and so on. The instance of the class can be created and the methods can be accessed and operations and be performed on it.Below is a demonstration of the same −Exampleclass Graph_struct:    def __init__(self, V):       self.V = V       self.adj = [[] for i in range(V)]    def DFS_Utililty(self, temp, v, ...

Read More

Python Program to Find the Sum of All Nodes in a Binary Tree

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 291 Views

When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Exampleclass Tree_struct:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data):       self.key = data ...

Read More

Take in Two Strings and Display the Larger String without Using Built-in Functions in Python Program

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 897 Views

When it is required to take two strings and display the larger string without using any built-in function, the counter can be used to get the length of the strings, and ‘if’ condition can be used to compare their lengths.Below is the demonstration of the same −Examplestring_1= "Hi there" string_2= "Hi how are ya" print("The first string is :") print(string_1) print("The second string is :") print(string_2) count_1 = 0 count_2 = 0 for i in string_1:    count_1=count_1+1 for j in string_2:    count_2=count_2+1 if(count_1

Read More

Python Program to Find All Connected Components using BFS in an Undirected Graph

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 768 Views

When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Exampleclass Graph_structure:    def __init__(self, V):       self.V = V       self.adj = [[] for i in range(V)]    def DFS_Utility(self, temp, v, ...

Read More

Count Number of Lowercase Characters in a String in Python Program

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ Views

When it is required to count the number of lower case characters in a string, the ‘islower’ method and a simple ‘for’ loop can be used.Below is the demonstration of the same −Examplemy_string = "Hi there how are you" print("The string is ") print(my_string) my_counter=0 for i in my_string:    if(i.islower()):       my_counter=my_counter+1 print("The number of lowercase characters in the string are :") print(my_counter)OutputThe string is Hi there how are you The number of lowercase characters in the string are : 15ExplanationA string is defined and is displayed on the console.A counter value is initialized to 0.The ...

Read More
Showing 471–480 of 8,547 articles
« Prev 1 46 47 48 49 50 855 Next »
Advertisements