Python Articles

Page 15 of 854

Python Program to Find Nth Node in the Inorder Traversal of a Tree

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 280 Views

When it is required to find the ‘n’th node using inorder traversal of a binary tree, a binary tree class is created with methods to set the root element, add elements to left or right, perform in order traversal and so on. An instance of the class is created, and it can be used to access the methods.Below is a demonstration of the same −Exampleclass BinaryTree_struct:    def __init__(self, key=None):       self.key = key       self.left = None       self.right = None    def set_root(self, key):       self.key = key ...

Read More

Check whether a number has consecutive 0’s in the given base or not using Python

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 254 Views

When it is required to check if a number has consecutive zeroes of a specific base, a method is defined, that takes the number and base as parameters, and uses another method to return Yes or No depending on whether the base is present or not.Below is a demonstration of the same −Exampledef check_consecutive_zero(N, K):    my_result = convert_to_base(N, K)    if (check_n(my_result)):       print("Yes")    else:       print("No") def convert_to_base(N, K):    weight = 1    s = 0    while (N != 0):       r = N % K     ...

Read More

Python Program to Construct a Tree & Perform Insertion, Deletion, Display

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 701 Views

When it is required to construct a binary tree, and perform operations such as inserting an element, deleting an element and displaying elements of the tree, a class is defined with methods in it. An instance of the class is defined and this is used to access the elements and perform operations.Below is a demonstration of the same −Exampleclass Tree_struct:    def __init__(self, data=None, parent=None):       self.key = data       self.children = []       self.parent = parent    def set_root(self, data):       self.key = data    def add_node(self, node):   ...

Read More

Python program to print the elements of an array present on odd position

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

When it is required to print the elements of a list that is present in odd index/position, a loop can be used to iterate over the elements, and only check the odd positions in the list by specifying the step size as 2 in the range function.Below is a demonstration of the same −Examplemy_list = [31, 42, 13, 34, 85, 0, 99, 1, 3] print("The list is :") print(my_list) print("The elements in odd positions are : ") for i in range(1, len(my_list), 2):    print(my_list[i])OutputThe list is : [31, 42, 13, 34, 85, 0, 99, 1, 3] The elements in ...

Read More

Python program to print the elements of an array present on even position

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 720 Views

When it is required to print the elements of a list that are present at even index/position, a loop can be used to iterate over the elements, and only check the even positions in the list by specifying the step size as 2 in the range function.Below is a demonstration of the same −Examplemy_list = [31, 42, 13, 34, 85, 0, 99, 1, 3] print("The list is :") print(my_list) print("The elements in odd positions are : ") for i in range(0, len(my_list), 2):    print(my_list[i])OutputThe list is : [31, 42, 13, 34, 85, 0, 99, 1, 3] The elements in ...

Read More

Python Program to Input a Number n and Compute n+nn+nnn

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

When it is required to take a number and compute a specific pattern, the value of n is taken from the user. Next, two variables are assigned this specific pattern and their sum is calculated.Below is a demonstration of the same −Examplemy_input = int(input("Enter a value for n...")) temp_val = str(my_input) t_1=temp_val+temp_val t_2=temp_val+temp_val+temp_val my_result = my_input+int(t_1)+int(t_2) print("The computed value is : ") print(my_result)OutputEnter a value for n...4 The computed value is : 492ExplanationAn input from the user is taken.It is converted into a string and assigned to a variable.The value of ‘n*n’ and the value of ‘n*n*n’ is calculated.Their sum ...

Read More

Python Program to Print all Numbers in a Range Divisible by a Given Number

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

When it is required to print all the elements in a given range that is divisible by a specific number, a simple for loop can be used.Below is a demonstration of the same −Examplelower_num = int(input("Enter lower range limit...")) upper_num = int(input("Enter upper range limit...")) div_num = int(input("Enter the number that should be divided by...")) for i in range(lower_num, upper_num+1):    if(i%div_num==0):       print(i)OutputEnter lower range limit...3 Enter upper range limit...8 Enter the number that should be divided by...2 4 6 8ExplanationThe upper and lower range of numbers is taken as input from the user.The number by which ...

Read More

Python Program to Read Two Numbers and Print Their Quotient and Remainder

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

When it is required to read two numbers and print the quotient and remainder when they are divided, the ‘//’ and ‘%’ operators can be used.Below is a demonstration of the same −Examplefirst_num = int(input("Enter the first number...")) second_num = int(input("Enter the second number...")) print("The first number is ") print(first_num) print("The second number is ") print(second_num) quotient_val = first_num//second_num remainder_val = first_num%second_num print("The quotient is :") print(quotient_val) print("The remainder is :") print(remainder_val)OutputEnter the first number...44 Enter the second number...56 The first number is 44 The second number is 56 The quotient is : 0 The remainder is : 44ExplanationThe ...

Read More

Python Program to Accept Three Digits and Print all Possible Combinations from the Digits

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

When it is required to print all possible combination of digits when the input is taken from the user, nested loop is used.Below is a demonstration of the same −Examplefirst_num = int(input("Enter the first number...")) second_num = int(input("Enter the second number...")) third_num = int(input("Enter the third number...")) my_list = [] print("The first number is ") print(first_num) print("The second number is ") print(second_num) print("The third number is ") print(third_num) my_list.append(first_num) my_list.append(second_num) my_list.append(third_num) for i in range(0, 3):    for j in range(0, 3):       for k in range(0, 3):          if(i!=j&j!=k&k!=i):       ...

Read More

Python Program to Find the Smallest Divisor of an Integer

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

When it is required to find the smallest divisor of a an integer, a simple ‘for’ loop is used.Below is a demonstration of the same −Examplefirst_num = int(input("Enter a number...")) my_list = [] print("The number is ") print(first_num) for i in range(2, first_num+1):    if(first_num%i==0):       my_list.append(i) my_list.sort() print("The smallest divisor is : ") print(my_list[0])OutputEnter a number...56 The number is 56 The smallest divisor is : 2ExplanationThe number is taken as an input from the user.An empty list is defined.The number taken from user is displayed on the console.The number range is iterated over.It is checked ...

Read More
Showing 141–150 of 8,532 articles
« Prev 1 13 14 15 16 17 854 Next »
Advertisements