Python Articles

Page 521 of 855

Python Program to Multiply All the Items in a Dictionary

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 945 Views

When you need to multiply all values in a dictionary, you can iterate through the dictionary values and multiply them together. Python dictionaries store key-value pairs, and we can access the values to perform mathematical operations. Basic Approach Using a Loop The simplest method is to iterate through dictionary values and multiply them with a running product ? my_dict = {'Jane': 99, 'Will': 54, 'Mark': -3} result = 1 for value in my_dict.values(): result = result * value print("The result of multiplying all values in the dictionary is:") print(result) ...

Read More

Python Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form (x,x*x).

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

When it is required to generate a dictionary that contains numbers within a given range in a specific form, the input is taken from the user, and a simple 'for' loop is used. This creates key−value pairs where each number maps to its square. Example Below is a demonstration for the same − my_num = int(input("Enter a number.. ")) my_dict = dict() for elem in range(1, my_num + 1): my_dict[elem] = elem * elem print("The generated elements of the dictionary are : ") print(my_dict) Output Enter a number.. ...

Read More

Python Program to Concatenate Two Dictionaries Into One

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

When working with dictionaries in Python, you often need to combine multiple dictionaries into one. Python provides several methods to concatenate dictionaries, each with different behaviors and use cases. Using the update() Method The update() method modifies the original dictionary by adding key-value pairs from another dictionary ? my_dict_1 = {'J': 12, 'W': 22} my_dict_2 = {'M': 67} print("The first dictionary is:") print(my_dict_1) print("The second dictionary is:") print(my_dict_2) my_dict_1.update(my_dict_2) print("The concatenated dictionary is:") print(my_dict_1) The first dictionary is: {'J': 12, 'W': 22} The second dictionary is: {'M': 67} The concatenated dictionary ...

Read More

Python Program to find the factorial of a number without recursion

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

When it is required to find the factorial of a number without using recursion, the while loop can be used. The factorial of a number n is the product of all positive integers from 1 to n, denoted as n!. What is Factorial? Factorial of a number n (written as n!) is defined as: n! = n × (n-1) × (n-2) × ... × 2 × 1 0! = 1 (by definition) Examples: 5! = 5 × 4 × 3 × 2 × 1 = 120 Using While Loop We can calculate factorial ...

Read More

Python Program to Find the Fibonacci Series without Using Recursion

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

When it is required to find the Fibonacci series without using recursion technique, we can use iterative methods with loops. The Fibonacci sequence starts with two initial numbers, and each subsequent number is the sum of the two preceding ones. What is Fibonacci Series? The Fibonacci series is a sequence where each number is the sum of the two preceding numbers. For example: 0, 1, 1, 2, 3, 5, 8, 13, 21... Method 1: Using While Loop with User Input This approach takes custom starting numbers and term count from the user ? first_num ...

Read More

Python Program to Find the Length of a List Using Recursion

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

When it is required to find the length of a list using recursion, a user-defined method is used along with simple indexing technique. This approach demonstrates how recursion can solve problems by breaking them into smaller subproblems. A list can store heterogeneous values (i.e., data of any data type like integer, floating point, strings, and so on). Recursion computes output of small bits of the bigger problem and combines these bits to give the solution to the bigger problem. Method 1: Using Simple Recursion The most straightforward recursive approach processes one element at a time ? ...

Read More

Python Program to Find the Total Sum of a Nested List Using Recursion

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 741 Views

When working with nested lists, we often need to calculate the sum of all elements regardless of their depth. Recursion is an elegant technique where a function calls itself to solve smaller parts of the problem until reaching a base case. Recursion breaks down complex nested structures by processing each element. If an element is a list, the function calls itself recursively. If it's a number, it adds to the total sum. Syntax def recursive_sum(nested_list): total = 0 for element in nested_list: ...

Read More

Python Program to Reverse a String Using Recursion

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

When it is required to reverse a string using recursion technique, a user-defined method is used along with recursion. The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem. How Recursion Works for String Reversal The recursive approach works by taking the first character and appending it to the reversed substring. The base case is when the string becomes empty. Example Below is a demonstration for the same − def reverse_string(my_string): if len(my_string) == 0: ...

Read More

Python Program to Check Whether a String is a Palindrome or not Using Recursion

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

A palindrome is a string that reads the same forwards and backwards, such as "racecar" or "madam". We can check if a string is a palindrome using recursion by comparing characters from both ends and recursively checking the substring in between. Recursion works by breaking down the problem into smaller subproblems. For palindrome checking, we compare the first and last characters, then recursively check the remaining substring until we reach the base case. Syntax The recursive approach follows this logic ? def check_palindrome(string): if len(string)

Read More

Python Program to Find the Product of two Numbers Using Recursion

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 772 Views

When it is required to find the product of two numbers using recursion technique, a simple if condition and recursion is used. The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem. How Recursion Works for Multiplication Multiplication can be thought of as repeated addition. For example, 5 × 3 = 5 + 5 + 5. We can use recursion to break down multiplication into smaller addition problems ? Example Below is a demonstration for the same ? def compute_product(val_1, ...

Read More
Showing 5201–5210 of 8,546 articles
« Prev 1 519 520 521 522 523 855 Next »
Advertisements