AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 141 of 840

Count the elements till first tuple in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 220 Views

When you need to count the elements up to the first tuple in a collection, you can use the enumerate() method with isinstance() to check each element's type and stop at the first tuple found. Example Here's how to count elements until the first nested tuple ? my_tuple_1 = (7, 8, 11, 0, (3, 4, 3), (2, 22)) print("The tuple is :") print(my_tuple_1) for count, elem in enumerate(my_tuple_1): if isinstance(elem, tuple): break print("The number of elements up to the first tuple ...

Read More

Check for None Tuple in Python

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

When checking if a tuple contains only None values, Python provides several approaches. The most common method uses the all() function with a generator expression to verify that every element is None. The all() method returns True if all values in an iterable evaluate to True, otherwise returns False. For checking None values, we use the is operator which tests for object identity. Using all() with Generator Expression This method efficiently checks if every element in the tuple is None − my_tuple = (None, None, None, None, None, None, None) print("The tuple is:") print(my_tuple) ...

Read More

How to get Subtraction of tuples in Python

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

When you need to subtract corresponding elements of two tuples in Python, you can use the map() function with a lambda expression. This approach applies element-wise subtraction and returns a new tuple with the results. The map() function applies a given function to every item in an iterable (such as list, tuple). It returns a map object that can be converted to a tuple. A lambda function is an anonymous function defined without a name. Unlike regular functions defined with def, lambda functions are defined using the lambda keyword and can take any number of arguments but contain ...

Read More

Python Program to Create a Class and Compute the Area and the Perimeter of the Circle

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

When it is required to find the area and perimeter of a circle using classes, 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 find the area and perimeter of the circle. Below is a demonstration for the same − Example import math class CircleCompute: def __init__(self, radius): self.radius = radius ...

Read More

Python Program to Append, Delete and Display Elements of a List Using Classes

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

When it is required to append, delete, and display the elements of a list using classes, 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 add elements to the list, delete elements from the list and display the elements of the list using objects. Below is a demonstration for the same − Class Definition First, let's define a class that encapsulates list operations − class ListManager: ...

Read More

Python Program to Find the Area of a Rectangle Using Classes

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

When working with object-oriented programming in Python, we can create a class to represent geometric shapes and calculate their properties. This example demonstrates how to find the area of a rectangle using a class with attributes and methods. Below is a demonstration for the same − Example class RectangleShape: def __init__(self, my_length, my_breadth): self.length = my_length self.breadth = my_breadth def calculate_area(self): ...

Read More

Python | Remove empty tuples from a list

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

When it is required to remove empty tuples from a list of tuples, Python provides several approaches. An empty tuple () evaluates to False in boolean context, making it easy to filter out. 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 tuples basically contains tuples enclosed in a list. Using List Comprehension The most pythonic way is to use list comprehension with a boolean check − def remove_empty(my_tuple_list): my_tuple_list = [t for t ...

Read More

Python Program to Edit objects inside tuple

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 206 Views

When it is required to edit the objects inside a tuple, simple indexing can be used. While tuples are immutable, they can contain mutable objects like lists that can be modified in-place. A tuple can store heterogeneous values (i.e data of any data type like integer, floating point, strings, lists, and so on). When a tuple contains mutable objects, those objects can be modified without changing the tuple's identity. Example Below is a demonstration of editing a list inside a tuple − my_tuple = (45, 67, [35, 66, 74], 89, 100) print("The tuple is :") ...

Read More

Python Program to Find Longest Common Substring using Dynamic Programming with Bottom-Up Approach

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 382 Views

Finding the longest common substring using dynamic programming with a bottom-up approach involves building a solution by solving smaller subproblems first. This method uses a 2D table to store intermediate results, avoiding redundant calculations and ensuring optimal performance. The algorithm works by comparing characters from both strings and building up the solution from the bottom of the problem space ? Algorithm Overview The bottom-up approach creates a 2D table where val[i][j] represents the length of the common substring ending at position i in the first string and position j in the second string. We fill this table ...

Read More

Python - Assign a specific value to Non Max-Min elements in Tuple

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 179 Views

When working with tuples, you may need to replace all elements except the maximum and minimum values with a specific value. Python provides the max() and min() methods to find extreme values, which can be used with conditional logic to transform tuple elements. The max() method returns the largest element in an iterable, while min() returns the smallest. The tuple() method converts any iterable into a tuple. Example Here's how to replace non max-min elements with a specific value ? my_tuple = (25, 56, 78, 91, 23, 11, 0, 99, 32, 10) print("The tuple is:") ...

Read More
Showing 1401–1410 of 8,392 articles
« Prev 1 139 140 141 142 143 840 Next »
Advertisements