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
Python Articles
Page 25 of 854
Python Program to Print only Nodes in Left SubTree
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 MorePython Program to Display the Nodes of a Tree using BFS Traversal
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 MorePython Program to Find All Connected Components using DFS in an Undirected Graph
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 MorePython Program to Find the Sum of All Nodes in a Binary Tree
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 MoreTake in Two Strings and Display the Larger String without Using Built-in Functions in Python Program
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 MorePython Program to Find All Connected Components using BFS in an Undirected Graph
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 MoreCount Number of Lowercase Characters in a String in Python Program
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 MoreExtract Unique dictionary values in Python Program
When it is required to extract unique values from a dictionary, a dictionary is created, and the ‘sorted’ method and dictionary comprehension is used.Below is a demonstration for the same −Examplemy_dict = {'hi' : [5, 3, 8, 0], 'there' : [22, 51, 63, 77], 'how' : [7, 0, 22], 'are' : [12, 11, 45], 'you' : [56, 31, 89, 90]} print("The dictionary is : ") print(my_dict) my_result = list(sorted({elem for val in my_dict.values() for elem in val})) print("The unique values are : ") print(my_result)OutputThe dictionary is : {'hi': [5, 3, 8, 0], 'there': ...
Read MorePython Program to Find if Undirected Graph contains Cycle using BFS
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 −Examplefrom collections import deque def add_edge(adj: list, u, v): adj[u].append(v) adj[v].append(u) def detect_cycle(adj: list, s, V, visited: list): parent = [-1] * V ...
Read MoreInsertion at the beginning in OrderedDict using Python
When it is required to insert the elements at the beginning of an ordered dictionary, the ‘update’ method can be used.Below is the demonstration of the same −Examplefrom collections import OrderedDict my_ordered_dict = OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')]) print("The dictionary is :") print(my_ordered_dict) my_ordered_dict.update({'Mark':'7'}) my_ordered_dict.move_to_end('Mark', last = False) print("The resultant dictionary is : ") print(my_ordered_dict)OutputThe dictionary is : OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')]) The resultant dictionary is : OrderedDict([('Mark', '7'), ('Will', '1'), ('James', '2'), ('Rob', '4')])ExplanationThe required packages are imported.An ordered dictionary is created using OrderedDict’.It is displayed on the console.The ‘update’ method is used to ...
Read More