AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 130 of 840

Trim tuples by N elements in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 284 Views

When working with lists of tuples, you might need to remove a specific number of elements from the beginning or end. Python provides several approaches to trim tuples by N elements. Using del Operator The del operator removes elements at a specific index ? my_list = [(1, 2, 11), (99, 76, 34, 89), (3.08, 11.56), ("Hi", "Will"), ("Rob", "Ron")] n = 2 print("Original list:") print(my_list) print(f"Removing element at index {n}") del my_list[n] print("List after deletion:") print(my_list) Original list: [(1, 2, 11), (99, 76, 34, 89), (3.08, 11.56), ('Hi', 'Will'), ('Rob', ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 711 Views

When working with calendar calculations, you might need to find how many times each day of the week occurs in a given month. This is useful for scheduling, payroll calculations, or planning recurring events. Understanding the Problem In any month, most days occur either 4 or 5 times. For a month with n days starting on a specific day, we need to calculate the frequency of each weekday. Algorithm Approach The solution works by: Starting with a base count of 4 for each day (since 4 × 7 = 28 days) Adding the extra ...

Read More

Python Program to Create a Lap Timer

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 684 Views

When it is required to create a lap timer using Python, the time module is used. The program tracks individual lap times and cumulative time using keyboard interrupts to control the timer flow. A lap timer records the duration of each lap in a sequence, commonly used in sports timing or performance tracking applications. Example import 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() ...

Read More

Python program to find difference between current time and given time

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 480 Views

When working with time calculations in Python, you often need to find the difference between the current time and a given time. This can be accomplished using Python's built-in datetime module, which provides powerful time manipulation capabilities. Method 1: Using datetime Module The most reliable approach uses Python's datetime module to get the current time and calculate differences ? from datetime import datetime, time def time_difference_from_now(target_hour, target_minute): # Get current time now = datetime.now() current_time = now.time() ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 564 Views

When working with dates in Python, you often need to find yesterday's, today's, and tomorrow's dates. Python's datetime module provides the timedelta class to easily calculate dates relative to the current date. Basic Date Calculation Here's how to find yesterday, today, and tomorrow using datetime.now() and timedelta ? from 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')) Yesterday was : 05-04-2021 Today is : 06-04-2021 Tomorrow would be : ...

Read More

Python Program to print the pattern 'G'

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 648 Views

When it is required to print the pattern of the letter 'G' using '*', a method can be defined, and nested loops can be used to iterate through the numbers and print '*' to form a 'G' pattern. Below is a demonstration of the same − Example def display_pattern(my_line): my_pattern = "" for i in range(0, my_line): for j in range(0, my_line): if ((j == 1 and i ...

Read More

Python Program to Display Fibonacci Sequence Using Recursion

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 481 Views

The Fibonacci sequence is a series where each number is the sum of the two preceding numbers, starting from 0 and 1. We can generate this sequence using recursion, where a function calls itself until it reaches a base case. Fibonacci Recursion Logic The recursive approach works by breaking down the problem: Base case: If n ≤ 1, return n Recursive case: Return fibonacci(n-1) + fibonacci(n-2) Example def fibonacci_recursion(n): if n

Read More

Python program to unique keys count for Value in Tuple List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 214 Views

When working with lists of tuples, you may need to count how many times each unique tuple appears. Python's collections.defaultdict provides an elegant solution for counting occurrences without manually checking if keys exist. Using collections.defaultdict The defaultdict(int) automatically initializes missing keys with a default value of 0, making counting operations simple ? import collections my_result = collections.defaultdict(int) my_list = [[('Hello', 'Hey')], [('Jane', 'Will')], [('William', 'John')], [('Hello', 'Hey')], [('z', 'q')]] print("The list of list is:") print(my_list) for elem in my_list: my_result[elem[0]] += 1 print("The result is:") print(my_result) ...

Read More

Python program to count Bidirectional Tuple Pairs

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 269 Views

When it is required to count the number of bidirectional tuple pairs in a list of tuples, we can iterate over the list using nested loops and check if pairs are reverse of each other. A bidirectional pair means if we have tuple (a, b), there exists another tuple (b, a) in the list. Below is a demonstration of the same − Example my_list = [(45, 67), (11, 23), (67, 45), (23, 11), (0, 9), (67, 45)] print("The list is : ") print(my_list) my_result = 0 for idx in range(0, len(my_list)): ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 490 Views

Finding the sum of all nodes in a tree is a fundamental operation in tree data structures. This can be accomplished using a Tree_structure class with methods to add nodes and calculate the total sum recursively. Tree Structure Implementation The following example demonstrates a complete tree implementation with sum calculation ? class Tree_structure: def __init__(self, data=None): self.key = data self.children = [] def set_root(self, data): ...

Read More
Showing 1291–1300 of 8,392 articles
« Prev 1 128 129 130 131 132 840 Next »
Advertisements