Programming Articles

Page 418 of 2547

Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 825 Views

When counting how many times a specific letter appears in a string, we can use recursion to break down the problem into smaller subproblems. Recursion solves the bigger problem by computing results from smaller parts and combining them together. Recursive Approach The recursive function checks each character one by one. If the current character matches our target, we count it and continue with the rest of the string ? def count_letter_recursive(text, target_letter): # Base case: empty string if not text: ...

Read More

Python Program to Determine Whether a Given Number is Even or Odd Recursively

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 967 Views

When determining whether a number is even or odd, we typically use the modulo operator. However, we can also solve this problem recursively by repeatedly subtracting 2 until we reach a base case. Recursion breaks down the problem into smaller subproblems. Here, we reduce the number by 2 in each recursive call until we reach 0 (even) or 1 (odd). How the Recursive Approach Works The algorithm works by: Base case: If the number is less than 2 (0 or 1), check if it's divisible by 2 Recursive case: Subtract 2 from the number and ...

Read More

Python Program that Displays which Letters are in the Two Strings but not in Both

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 346 Views

When working with two strings, we often need to find letters that appear in one string but not in both. Python's set operations provide an elegant solution using the symmetric difference operator (^). A list can store heterogeneous values (integers, strings, floats, etc.). The list() function converts any iterable to a list type. Python's set data type contains only unique elements. Sets are useful for operations like intersection, difference, union, and symmetric difference. Syntax set1 ^ set2 # Symmetric difference - elements in either set, but not both Example Below is ...

Read More

Python Program that Displays which Letters are in the First String but not in the Second

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 529 Views

When it is required to display the letters that are present in the first string but not in the second string, we can use Python's set data structure to find the difference between two strings efficiently. Python's set contains unique elements only and is useful for performing operations such as intersection, difference, union, and symmetric difference. Using Set Difference The most efficient approach is to convert both strings to sets and use the difference operator - ? # Define two strings first_string = "programming" second_string = "coding" # Find letters in first string but ...

Read More

Python Program to Create a Dictionary with Key as First Character and Value as Words Starting with that Character

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

When you need to group words by their first character, you can create a dictionary where keys are first characters and values are lists of words starting with that character. This uses the split() method, dictionary operations, and conditional logic. Example Below is a demonstration for the same − my_string = "Hey Jane how are you doing today" split_string = my_string.split() my_dict = {} for elem in split_string: if elem[0] not in my_dict.keys(): my_dict[elem[0]] = [] ...

Read More

Python Program to Form a Dictionary from an Object of a Class

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

When working with classes in Python, you often need to convert an object's attributes into a dictionary format. Python provides the __dict__ attribute that automatically creates a dictionary containing all instance attributes of an object. Basic Example Here's how to create a dictionary from a class object using the __dict__ attribute ? class BaseClass: def __init__(self): self.A = 32 self.B = 60 my_instance = BaseClass() print("An instance of the class has been created") print(my_instance.__dict__) ...

Read More

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
Showing 4171–4180 of 25,466 articles
« Prev 1 416 417 418 419 420 2547 Next »
Advertisements