Python Articles

Page 523 of 855

Python Program to Create a class performing Calculator Operations

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 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 class calculator_implementation(): def __init__(self, in_1, in_2): self.a = in_1 self.b = in_2 ...

Read More

Remove duplicate tuples from list of tuples in Python

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

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 More

Initialize tuples with parameters in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 545 Views

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 More

Convert Tuple to integer in Python

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

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

Python Program to Implement Shell Sort

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

Shell Sort is an efficient sorting algorithm that improves upon insertion sort by comparing elements separated by a gap (interval). It starts with a large gap and gradually reduces it until the gap becomes 1, at which point it performs a final insertion sort on the nearly sorted array. The algorithm works by sorting sub-arrays of elements that are a certain distance apart, making the overall array more organized with each pass ? How Shell Sort Works Shell Sort follows these steps ? Start with a gap (interval) equal to half the array length Compare ...

Read More

Sort lists in tuple in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 416 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 without modifying the original. Generator expression 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 ...

Read More

Custom sorting in list of tuples in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 867 Views

Custom sorting in Python allows you to sort a list of tuples based on specific criteria using the sort() method with a key function. This is useful when you need to sort by a particular element within each tuple. Basic Custom Sorting The sort() method sorts elements in-place. Use the key parameter to specify which element of the tuple to sort by ? def tuple_sort(my_tup): my_tup.sort(key=lambda x: x[1]) return my_tup my_tuple = [('Will', 100), ('John', 67), ('Harold', 86), ('Jane', 35)] print("Original tuple:") print(my_tuple) print("Sorted by second ...

Read More

Get tuple element data types in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 914 Views

When you need to determine the data types of elements in a tuple, you can use Python's map() function combined with the type() function. The map() function applies a given function to every item in an iterable (such as list, tuple) and returns an iterator. The type() function returns the class type of any object passed to it. Basic Example Here's how to get data types of tuple elements ? my_tuple = ('Hi', 23, ['there', 'Will']) print("The tuple is:") print(my_tuple) # Apply type() to each element using map() data_types = list(map(type, my_tuple)) ...

Read More

Check order specific data type in tuple in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 515 Views

When working with tuples containing mixed data types, you might need to verify that elements appear in a specific order with expected data types. Python's isinstance() method combined with chained conditions provides an effective way to validate tuple structure. The isinstance() method checks if a given object belongs to a specific data type. Chained conditionals use the and operator to combine multiple conditions, ensuring all must be True for the overall result to be True. Basic Example Let's check if a tuple follows the pattern: string, list, integer ? my_tuple = ('Hi', ['there', 'Will'], 67) ...

Read More

Convert location coordinates to tuple in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 560 Views

When working with location data, you often need to convert coordinate strings into tuple format for mathematical operations or data processing. Python provides several methods to accomplish this conversion safely and efficiently. Method 1: Using eval() (Not Recommended) The eval() method can parse and execute string expressions, but it poses security risks ? my_string = "67.5378, -78.8523" print("The string is:") print(my_string) my_result = eval(my_string) print("The coordinates after converting the string to tuple is:") print(my_result) print("Type:", type(my_result)) The string is: 67.5378, -78.8523 The coordinates after converting the string to tuple is: ...

Read More
Showing 5221–5230 of 8,546 articles
« Prev 1 521 522 523 524 525 855 Next »
Advertisements