Articles on Trending Technologies

Technical articles with clear explanations and examples

Python Program to Find the Cumulative Sum of a List where the ith Element is the Sum of the First i+1 Elements From The Original List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 502 Views

When it is required to find the cumulative sum of a list where the ith element is the sum of the first i+1 elements from the original list, we can use different approaches. The cumulative sum creates a new list where each element represents the running total up to that position. Method 1: Using List Comprehension This approach uses list comprehension with slicing to calculate cumulative sums ? def cumulative_sum(my_list): cumulative_list = [] my_length = len(my_list) cumulative_list = [sum(my_list[0:x:1]) for x in range(1, my_length+1)] ...

Read More

Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

When it is required to find all numbers in a range that are perfect squares and the sum of digits in the number is less than 10, list comprehension is used. A perfect square is a number that can be expressed as the product of an integer with itself. Below is the demonstration of the same − Example lower_limit = 5 upper_limit = 50 numbers = [] numbers = [x for x in range(lower_limit, upper_limit + 1) if (int(x**0.5))**2 == x and sum(list(map(int, str(x)))) < 10] print("The result is:") print(numbers) Output The ...

Read More

Python Program to Create a List of Tuples with the First Element as the Number and Second Element as the Square of the Number

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

When you need to create a list of tuples where each tuple contains a number and its square, Python provides several approaches. The most common methods are list comprehension, using loops, and the map() function. Using List Comprehension List comprehension provides a concise way to create the list of tuples ? numbers = [23, 42, 67, 89, 11, 32] print("The list is:") print(numbers) result = [(num, pow(num, 2)) for num in numbers] print("The resultant tuples are:") print(result) The list is: [23, 42, 67, 89, 11, 32] The resultant tuples are: [(23, 529), ...

Read More

Python Program to Find the Second Largest Number in a List Using Bubble Sort

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 998 Views

Finding the second largest number in a list can be accomplished using bubble sort to first arrange elements in ascending order, then accessing the second-to-last element. Bubble sort repeatedly compares adjacent elements and swaps them if they are in the wrong order. Understanding Bubble Sort Bubble sort works by comparing each pair of adjacent elements and swapping them if the first element is greater than the second. This process continues until no more swaps are needed. Bubble Sort Process Initial: ...

Read More

Python Program to Merge Two Lists and Sort it

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

When it is required to merge two lists and sort them, Python provides several approaches. You can use list concatenation with the sort() method, or combine merging and sorting in one step using sorted(). Below is the demonstration of the same − Method 1: Using List Concatenation and sort() Example Define a function that merges two lists and sorts them in-place − def merge_list(list_1, list_2): merged_list = list_1 + list_2 merged_list.sort() return merged_list numbers_1 = [20, 18, 9, 51, 48, 31] ...

Read More

Python Program to Find the Gravitational Force Acting Between Two Objects

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 527 Views

When calculating the gravitational force between two objects, we can use Newton's Law of Universal Gravitation. This law states that every particle attracts every other particle with a force proportional to the product of their masses and inversely proportional to the square of the distance between them. The formula is: F = G × (m₁ × m₂) / r², where G is the gravitational constant (6.673 × 10⁻¹¹ N⋅m²/kg²). Example Here's how to calculate gravitational force using a Python function ? def find_gravity(m_1, m_2, r): G_val = 6.673*(10**-11) # Gravitational ...

Read More

Python Program to Check If Two Numbers are Amicable Numbers

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 940 Views

Amicable numbers are two different numbers where the sum of proper divisors of each number equals the other number. For example, 220 and 284 are amicable because the proper divisors of 220 sum to 284, and the proper divisors of 284 sum to 220. What are Proper Divisors? Proper divisors are all positive divisors of a number except the number itself. For example, proper divisors of 220 are: 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110. Method 1: Using Mathematical Approach This method finds divisors efficiently by checking only up to the square ...

Read More

Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

When you need to compute a polynomial equation where the coefficients are stored in a list, you can use nested loops to calculate each term. A polynomial like 2x³ + 5x² + 3x + 0 can be evaluated by multiplying each coefficient by the corresponding power of x. Understanding Polynomial Evaluation For a polynomial represented as a list [2, 5, 3, 0], this corresponds to: 2x³ (coefficient 2, power 3) 5x² (coefficient 5, power 2) 3x¹ (coefficient 3, power 1) 0x⁰ (coefficient 0, power 0) Example Here's how to evaluate the polynomial when ...

Read More

Python Program to Check if a Number is a Strong Number

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 7K+ Views

A strong number is a number whose sum of all digits' factorial equals the original number. For example, 145 is a strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145. We can check this using the modulus operator and loops to extract digits and calculate factorials. Example Let's check if 145 is a strong number ? def factorial(n): if n == 0 or n == 1: return 1 fact = 1 ...

Read More

Python Program to Check if a Number is a Perfect Number

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

A Perfect Number is a positive integer that equals the sum of its positive divisors, excluding the number itself. For example, 6 is a perfect number because its divisors (1, 2, 3) sum to 6. The first few perfect numbers are 6, 28, 496, and 8128. Basic Method to Check Perfect Number We can iterate through all numbers from 1 to n-1 and check if they divide n evenly ? n = 6 my_sum = 0 for i in range(1, n): if n % i == 0: ...

Read More
Showing 4881–4890 of 61,297 articles
« Prev 1 487 488 489 490 491 6130 Next »
Advertisements