Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 420 of 2547
Python program to check if the given number is a Disarium Number
A Disarium Number is a number where the sum of its digits raised to the power of their respective positions equals the original number itself. For example, 175 is a Disarium number because 11 + 72 + 53 = 1 + 49 + 125 = 175. Understanding Disarium Numbers To check if a number is a Disarium number, we need to ? Extract each digit from the number Calculate the total number of digits Raise each digit to the power of its position (1-based indexing) Sum all the results and compare with the original number ...
Read MorePython Program to Find All Numbers which are Odd and Palindromes Between a Range of Numbers
When it is required to find all numbers that are odd and palindromes within a given range, list comprehension and the modulo operator (%) can be used to achieve this efficiently. A palindrome is a number that reads the same forwards and backwards, such as 121 or 1331. For a number to be both odd and a palindrome, it must satisfy two conditions: divisibility check and string comparison. Syntax result = [x for x in range(start, end+1) if x%2!=0 and str(x)==str(x)[::-1]] Example Here's how to find all odd palindromes in a given range ...
Read MorePython Program to Implement Binomial Tree
A binomial tree is a data structure used in computer science and financial modeling. It consists of nodes where each tree of order k has 2^k nodes. In Python, we can implement this using object-oriented programming with a class that manages tree creation and combination operations. Binomial Tree Class Implementation Here's how to create a binomial tree class with methods to add children and combine trees ? class BinomialTree: def __init__(self, key): self.key = key self.children ...
Read MorePython Program to Split the array and add the first part to the end
When it is required to split the array and add the first part to the end, we can use list slicing or implement a rotation algorithm. This operation is commonly known as left rotation of an array. A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). Method 1: Using List Slicing The simplest approach is to use Python's list slicing to split and rearrange the array ? def split_and_rotate(arr, k): """Split array at position k and move ...
Read MorePython Program to Create a Class and Get All Possible Subsets from a Set of Distinct Integers
When it is required to create a class to get all the possible subsets of integers from a list, object oriented method is used. Here, a class is defined, and attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to perform operations. Below is a demonstration for the same − Example class get_subset: def sort_list(self, my_list): return self.subset_find([], sorted(my_list)) ...
Read MorePython Program to Create a Class wherein a Method accepts a String from the User and Another Prints it
When it is required to create a class that has a method to accept a string from the user and another method to print the string, object-oriented programming is used. Here, a class is defined with attributes and methods that perform specific operations on user input. Below is a demonstration for the same − Example class StringHandler: def __init__(self): self.string = "" def get_data(self): self.string = input("Enter ...
Read MorePython Program to Create a class performing Calculator Operations
When it is required to create a class that performs calculator operations, object oriented method is used. Here, a class is defined, and attributes are defined. Functions are defined within the class that perform certain operations. An instance of the class is created, and the functions are used to perform calculator operations. Below is a demonstration for the same − Example class calculator_implementation(): def __init__(self, in_1, in_2): self.a = in_1 self.b = in_2 ...
Read MoreRemove duplicate tuples from list of tuples in Python
When working with lists of tuples, you often need to remove duplicates to clean your data. Python provides several methods to accomplish this task efficiently. The any() method checks if any value in an iterable is True. If at least one value is True, it returns True; otherwise, it returns False. The enumerate() method adds a counter to an iterable and returns it as an enumerate object, which is useful for getting both index and value during iteration. Method 1: Using set() (Most Efficient) The simplest approach is to convert the list to a set and ...
Read MoreInitialize tuples with parameters in Python
When it is required to initialize tuples with certain parameters, the tuple() method and the * operator can be used. The tuple() method converts the iterable passed to it as a parameter into a tuple. The * operator can be used to repeat a single value multiple times, making it useful for creating tuples with default values. Basic Tuple Initialization Here's how to create a tuple with repeated default values and modify specific positions ? N = 6 print("The value of N has been initialized to " + str(N)) default_val = 2 print("The default ...
Read MoreConvert Tuple to integer in Python
When it is required to convert a tuple into an integer, the lambda function and the reduce function can be used. This approach treats each tuple element as a digit and combines them into a single integer. Anonymous function is a function which is defined without a name. The reduce() function takes two parameters − a function and a sequence, where it applies the function to all the elements of the tuple/sequence. It is present in the functools module. In general, functions in Python are defined using def keyword, but anonymous function is defined with the help of ...
Read More