Python Articles - Page 524 of 829

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

468 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

4K+ 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

2K+ 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

514 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

1K+ 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

Sort lists in tuple in Python

AmitDiwan
Updated on 12-Mar-2021 06:07:21

394 Views

When it is required to sort the lists in a tuple, the 'tuple' method, the 'sorted' method and a generator expression can be used.The 'sorted' method is used to sort the elements of a list. It is a built-in function, that returns the sorted list.Generator is a simple way of creating iterators. It automatically implements a class with '__iter__()' and '__next__()' methods and keeps track of the internal states, as well as raises 'StopIteration' exception when no values are present that could be returned.The 'tuple' method takes an iterable as argument, and converts it into a tuple type.A list can ... Read More

Custom sorting in list of tuples in Python

AmitDiwan
Updated on 12-Mar-2021 06:05:31

846 Views

When it is required to sort the list of tuples in a customized manner, the 'sort' method can be used.The 'sort' method sorts the elements of the iterable in a specific order, i.e ascending or descending. It sorts the iterable in-place.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.Below is a demonstration of the same −ExampleLive Demodef tuple_sort(my_tup):    my_tup.sort(key = lambda x: x[1])    return my_tup my_tuple = [('Will', 100), ('John', 67), ('Harold', 86), ... Read More

Get tuple element data types in Python

AmitDiwan
Updated on 12-Mar-2021 06:04:58

885 Views

When it is required to get the tuple element of data types, the 'map' method and the 'type' method can be used.The map function applies a given function/operation to every item in an iterable (such as list, tuple). It returns a list as the result.The 'type' method returns the type of class of the argument that is passed to it.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 Demomy_tuple = ('Hi', 23, ['there', 'Will']) print("The tuple is : ... Read More

Check order specific data type in tuple in Python

AmitDiwan
Updated on 12-Mar-2021 06:04:02

493 Views

When it is required to check the order of a specific data type in a tuple, the 'isinstance' method and the 'chained if' can be used.The 'isinstance' method checks to see if a given parameter belong to a specific data type or not.The 'chained if' is a chained conditional statement. It is a different way of writing nested selection statements. It basically means multiple if statements are combined using 'and' operator, and their result is evaluated.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 ... Read More

Advertisements