Python Articles

Page 44 of 855

Python Program to Implement Queues using Stacks

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 448 Views

When it is required to implement a queue using a stack, a queue class can be defined, where two stack instances can be defined. Different operations can be performed on the queue that are defined as methods in this class.Below is a demonstration of the same −Exampleclass Queue_structure:    def __init__(self):       self.in_val = Stack_structure()       self.out_val = Stack_structure()    def check_empty(self):       return (self.in_val.check_empty() and self.out_val.check_empty())    def enqueue_operation(self, data):       self.in_val.push_operation(data)    def dequeue_operation(self):       if self.out_val.check_empty():          while not self.in_val.check_empty():   ...

Read More

Python Program to Display Fibonacci Sequence Using Recursion

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 445 Views

When it is required to print the fibonacci sequence using the method of recursion, a method can be declared that calls the same method again and again until a base value is reached.Below is a demonstration of the same −Exampledef fibonacci_recursion(my_val):    if my_val

Read More

Find yesterday's, today's and tomorrow's date in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 535 Views

When it is required to find yesterday, today and tomorrow’s date with respect to current date, the current time is determined, and a method is used (in built) that helps find previous day and next day’s dates.Below is a demonstration of the same −Examplefrom datetime import datetime, timedelta present = datetime.now() yesterday = present - timedelta(1) tomorrow = present + timedelta(1) print("Yesterday was :") print(yesterday.strftime('%d-%m-%Y')) print("Today is :") print(present.strftime('%d-%m-%Y')) print("Tomorrow would be :") print(tomorrow.strftime('%d-%m-%Y'))OutputYesterday was : 05-04-2021 Today is : 06-04-2021 Tomorrow would be : 07-04-2021ExplanationThe required packages are imported into the environment.The current date is determined using ...

Read More

Python program to find difference between current time and given time

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 448 Views

When it is required to find the difference between the current time and a given time, a method can be defined, that takes the hours, minutes, and seconds as parameter. It then calculates the difference between two given times.Below is a demonstration of the same −Exampledef difference_time(h_1, m_1, h_2, m_2):    t_1 = h_1 * 60 + m_1    t_2 = h_2 * 60 + m_2    if (t_1 == t_2):       print("The times are the same")       return    else:       diff = t_2-t_1    hours = (int(diff / 60)) ...

Read More

Python Program to Count Number of Leaf Node in a Tree

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 709 Views

When it is required to count the number of leaf nodes in a Tree, a ‘Tree_structure’ class is created, methods to add root value, and other children values are defined. Various options are given that the user can select. Based on the user’s choice, the operation is performed on the Tree elements.Below is a demonstration of the same −Exampleclass Tree_structure:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root_node(self, data):       self.key = data    def add_vals(self, node):       self.children.append(node)    def search_val(self, ...

Read More

Python Program to Count Number of Non Leaf Nodes of a given Tree

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 265 Views

When it is required to find the count of the non leaf nodes in a Tree, a ‘Tree_structure’ class is created, methods to set a root value, and to add other values are defined. Various options are given that the user can select. Based on the user’s choice, the operation is performed on the Tree elements.Below is a demonstration of the same −Exampleclass Tree_structure:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data):       self.key = data    def add_vals(self, node):       self.children.append(node) ...

Read More

Python Program to Find the Sum of all Nodes in a Tree

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 471 Views

When it is required to get the sum of all the nodes in a Tree, a ‘Tree_structure’ class is created, methods to set a root value, and to add other values are defined. It also has a method to determine the sum of all elements of a Tree structure. Various options are given that the user can select. Based on the user’s choice, the operation is performed on the Tree elements.Below is a demonstration of the same −Exampleclass Tree_structure:    def __init__(self, data=None):       self.key = data       self.children = []    def set_root(self, data): ...

Read More

Python Program to Create a Lap Timer

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 668 Views

When it is required to create a lap timer using Python, the ‘time’ method is used. The number of laps is predefined, and a try catch block is defined, to start the lap timer.Below is a demonstration of the same −Exampleimport time start_time=time.time() end_time=start_time lap_num=1 print("Click on ENTER to count laps.Press CTRL+C to stop") try:    while True:       input()       time_laps=round((time.time() - end_time), 2)       tot_time=round((time.time() - start_time), 2)       print("Lap No. "+str(lap_num))       print("Total Time: "+str(tot_time))       print("Lap Time: "+str(time_laps)) ...

Read More

Find number of times every day occurs in a Year in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 687 Views

When it is required to find the number of times every day of the week occurs in a year, a list is defined, and it is iterated over, and is count is incremented respectively.Below is a demonstration of the same −Exampleimport math def num_of_occurrence( n, firstday):    my_days = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ]    my_count= [4 for i in range(0, 7)]    my_position = -1    for i in range(0, 7):       if (first_day == my_days[i]):          my_position = i          break   ...

Read More

Trim tuples by N elements in Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 268 Views

When it is required to trim a list of tuples by a specific number of elements, the ‘del’ operator can be used.Below is a demonstration of the same −Examplemy_list = [(1, 2, 11), (99, 76, 34, 89), (3.08, 11.56), ("Hi", "Will"), ("Rob", 'Ron')] n = 2 print("The list is :") print(my_list) print("The value of N is") print(n) del my_list[n] print("The list after deleting N elements is :") print(my_list)OutputThe list is : [(1, 2, 11), (99, 76, 34, 89), (3.08, 11.56), ('Hi', 'Will'), ('Rob', 'Ron')] The value of N is 2 The list after deleting N elements is : [(1, 2, ...

Read More
Showing 431–440 of 8,547 articles
« Prev 1 42 43 44 45 46 855 Next »
Advertisements