Python Articles

Page 72 of 855

Python Program to Find the Product of two Numbers Using Recursion

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 755 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.ExampleBelow is a demonstration for the same −def compute_product(val_1,val_2):    if(val_1

Read More

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

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

When it is required to check if a string is a palindrome or not using recursion technique, simple indexing and a user defined function, along with recutsion is used.Palindromes are those strings or values which when read from left to right and right to left have the same characters in their respective indices.The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.Below is a demonstration for the same −Exampledef check_palindrome(my_str):    if len(my_str) < 1:       return True    else:       if my_str[0] ...

Read More

Python Program to Reverse a String Using Recursion

AmitDiwan
AmitDiwan
Updated on 11-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.ExampleBelow is a demonstration for the same −def reverse_string(my_string):    if len(my_string) == 0:       return my_string    else:       return reverse_string(my_string[1:]) + my_string[0] my_str = str(input("Enter the string that needs to be reversed : ")) print("The string is :") print(my_str) print("The reversed string is :") print(reverse_string(my_str))OutputEnter the string that needs to be reversed : ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 721 Views

When it is required to find the total sum of a nest list using the recursion technique, a user defined method is used, that takes the list as a parameter.The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).ExampleBelow is a demonstration for the same −def recursion_sum(my_list):    my_total = 0    for elem in my_list:       if (type(elem) == type([])):     ...

Read More

Python Program to Find the Length of a List Using Recursion

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

When it is required to find the length of a list with the help of recursion technique, a user defined method is used, and simple indexing technique is used.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.ExampleBelow is a demonstration for the same −def list_length(my_list):    if not my_list:       return 0    return 1 + list_length(my_list[1::2]) + list_length(my_list[2::2]) my_list = [1, ...

Read More

Python Program to Find the Fibonacci Series without Using Recursion

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

When it is required to find the Fibonacci series without using recursion technique, the input is taken from the user, and a ‘while’ loop is used to get the numbers in the sequence.ExampleBelow is a demonstration for the same −first_num = int(input("Enter the first number of the fibonacci series... ")) second_num = int(input("Enter the second number of the fibonacci series... ")) num_of_terms = int(input("Enter the number of terms... ")) print(first_num, second_num) print("The numbers in fibonacci series are : ") while(num_of_terms-2):    third_num = first_num + second_num    first_num=second_num    second_num=third_num    print(third_num)    num_of_terms=num_of_terms-1OutputEnter the first number of the fibonacci ...

Read More

Python Program to find the factorial of a number without recursion

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

When it is required to find the factorial of a number without using recursion, the ‘while’ loop can be used.ExampleBelow is a demonstration for the same −my_num = int(input("Enter a number :")) my_factorial = 1 while(my_num>0):    my_factorial = my_factorial*my_num    my_num=my_num-1 print("The factorial of the number is : ") print(my_factorial)OutputEnter a number :7 The factorial of the number is : 5040ExplanationThe input number is takne from the user.A variable is assigned to 1.It is checked to see for being 0.If not, it is multiplied by the previous value in the variable.It is assigned to the same variable.This is done ...

Read More

Python Program to Concatenate Two Dictionaries Into One

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

When it is required to concatenate two dictionaries into a single entity, the ‘update’ method can be used.A dictionary is a ‘key-value’ pair.Below is a demonstration for the same −Examplemy_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)OutputThe first dictionary is : {'J': 12, 'W': 22} The second dictionary is : {'M': 67} The concatenated dictionary is : {'J': 12, 'W': 22, 'M': 67}ExplanationTwo dictionaries are defined, and are displayed on the console. The ‘update’ method is called on the first dictionary by passing ...

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 11-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.ExampleBelow 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)OutputEnter a number.. 7 The generated elements of the dictionary are : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49}ExplanationThe number is taken as user input.An empty ...

Read More

Python Program to Multiply All the Items in a Dictionary

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 931 Views

When it is required to multiply all the elements in a dictionary, the key values in the dictionary are iterated over. The key is multiplied with the previous key, and output is determined.The dictionary is a set of key-value pairs.ExampleBelow is a demonstration for the same −my_dict = {'Jane':99, 'Will':54, 'Mark':-3} my_result = 2 for key in my_dict:    my_result = my_result * my_dict[key] print("The reuslt of multiplying keys in a dictionary is : ") print(my_result)OutputThe reuslt of multiplying keys in a dictionary is : -32076ExplanationA dictionary is defined.A variable is assigned a certain value.The ‘key’ in the dictionary is ...

Read More
Showing 711–720 of 8,547 articles
« Prev 1 70 71 72 73 74 855 Next »
Advertisements