Found 10476 Articles for Python

Python Program to Find All Numbers which are Odd and Palindromes Between a Range of Numbers

AmitDiwan
Updated on 12-Mar-2021 12:03:02

549 Views

When it is required to find all the numbers that are odd, and are palindromes and lie between a given range of values, and it has been told that recursion can’t be used, then, list comprehension, and ‘%’ operator can be used to achieve the same.Palindromes are string that are same when they are read in either way- left to right and right to left.Below is a demonstration for the same −Example Live Demomy_list = [] lower_limit = 5 upper_limit = 189 print("The lower limit is : ") print(lower_limit) print("The upper limit is : ") print(upper_limit) my_list = [x for x ... Read More

Python Program to Implement Binomial Tree

AmitDiwan
Updated on 12-Mar-2021 12:02:05

642 Views

When it is required to implement a binomial tree in Python, 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 Live Democlass binomial_tree:    def __init__(self, key):       self.key = key       self.children = []       self.order = 0    def add_at_end(self, t):       self.children.append(t)       self.order = self.order + 1 my_tree = ... Read More

Python Program to Split the array and add the first part to the end

AmitDiwan
Updated on 12-Mar-2021 11:57:55

503 Views

When it is required to split the list, and then add this first part to the end of the list, a simple iteration through the list and list slicing is required.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).Below is a demonstration for the same −Example Live Demodef split_list(my_list, n_val, k_val):    for i in range(0, k_val):       first_val = my_list[0]       for k in range(0, n_val-1):          my_list[k] = my_list[k + 1]       my_list[n_val-1] = first_val my_list ... Read More

Python Program to Create a Class and Get All Possible Subsets from a Set of Distinct Integers

AmitDiwan
Updated on 12-Mar-2021 11:56:16

692 Views

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 calculator operations.Below is a demonstration for the same −Example Live Democlass get_subset:    def sort_list(self, my_list):       return self. subset_find([], sorted(my_list))    def subset_find(self, curr, my_list):       if my_list:          return self. subset_find(curr, my_list[1:]) + self. subset_find(curr ... Read More

Python Program to Create a Class wherein a Method accepts a String from the User and Another Prints it

AmitDiwan
Updated on 12-Mar-2021 11:54:06

426 Views

When it is required to create a class that has a method that accepts a string from the user, and another method that prints the string, 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 Live Democlass print_it():    def __init__(self):       self.string = ""    def get_data(self):       self.string=input("Enter the string : ")    def put_data(self):     ... Read More

Python Program to Create a class performing Calculator Operations

AmitDiwan
Updated on 12-Mar-2021 11:50:52

3K+ Views

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 Live Democlass calculator_implementation():    def __init__(self, in_1, in_2):       self.a=in_1       self.b=in_2    def add_vals(self):       return self.a+self.b    def multiply_vals(self):       return self.a*self.b    def divide_vals(self):       return self.a/self.b    def ... Read More

Remove duplicate tuples from list of tuples in Python

AmitDiwan
Updated on 12-Mar-2021 06:11:22

1K+ Views

When it is required to remove duplicate tuples from a list of tuples, the loop, the 'any' method and the enumerate method can be used.The 'any' method checks to see if any value in the iterable is True, i.e atleast one single value is True. If yes, it returns 'True', else returns 'False'A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).A list of tuple basically contains tuples enclosed in a list.The enumerate method adds a counter to the given iterable, and returns it.Below is a demonstration ... Read More

Initialize tuples with parameters in Python

AmitDiwan
Updated on 12-Mar-2021 06:10:45

478 Views

When it is required to initialize the tuples with certain parameters, the 'tuple' method and the '*' operator can be used.The 'tuple' method will convert the iterable passed to it as parameter into a tuple class type.The * operator can be used to get the product of two values. It can also be used to multiple a single value multiple times and display it on the console.Below is a demonstration for the same −ExampleLive DemoN = 6 print("The value of N has been initialized to "+str(N)) default_val = 2 print("The default value has been initialized to " +str(default_val)) ... Read More

Convert Tuple to integer in Python

AmitDiwan
Updated on 12-Mar-2021 06:09:30

2K+ Views

When it is required to convert a tuple into an integer, the lambda function and the 'reduce' function can be used.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 list/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 'lambda' keyword. It takes a single expression, but can take any number of arguments. It uses the expression and returns the ... Read More

Python Program to Implement Shell Sort

AmitDiwan
Updated on 12-Mar-2021 06:08:43

986 Views

When it is required to implement shell sort, a function is defined, and this takes a list and the length of the list as arguments. This list is sorted up to a specific number of elements, wherein the number of elements is the largest value. This is done until the number of elements has the smallest value.This is done for all sub-lists in the list, and all these sub-lists are sorted.A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).Below is a demonstration of the same −ExampleLive ... Read More

Advertisements