Programming Articles

Page 423 of 2547

Convert tuple to float value in Python

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

Converting a tuple to a float value in Python can be useful when you have numeric values stored separately that need to be combined into a decimal number. This can be achieved using the join() method with a generator expression and the float() function. A generator expression is a concise way to create iterators that automatically implements __iter__() and __next__() methods. The str() method converts elements to string format, while float() converts the final result to float data type. Basic Conversion Example Here's how to convert a tuple containing two integers into a float value ? ...

Read More

Find Dissimilar Elements in Tuples in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 228 Views

When it is required to find dissimilar elements in tuples, the set operator and the ^ operator can be used. Python comes with a datatype known as set. This set contains elements that are unique only. The set is useful in performing operations such as intersection, difference, union and symmetric difference. The ^ operator performs the symmetric difference operation on sets. It returns elements that are in either set but not in both sets. Example Below is a demonstration of finding dissimilar elements between two tuples ? my_tuple_1 = ((7, 8), (3, 4), (3, ...

Read More

Convert tuple to adjacent pair dictionary in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 304 Views

When it is required to convert a tuple to an adjacent pair dictionary, the dict() method, dictionary comprehension, and slicing can be used. A dictionary stores values in the form of a (key, value) pair. Dictionary comprehension is a shorthand to iterate through sequences and create dictionaries efficiently. Slicing extracts elements from an iterable using [start:end] notation, where it includes the start index but excludes the end index. Using Dictionary Comprehension with Slicing This method creates pairs by taking every two consecutive elements from the tuple ? my_tuple = (7, 8, 3, 4, 3, ...

Read More

Count the elements till first tuple in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 215 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 - Replace duplicate Occurrence in String

Akshitha Mote
Akshitha Mote
Updated on 25-Mar-2026 909 Views

In this article, we will explore different ways to replace duplicate occurrences in a string. Before understanding the solution let's try to understand the problem statement with an example. Consider a string "Ram lives in Hyderabad. Ram is studying in class 10. Ram is the class Monitor of the class." In this string "Ram" appears multiple times we need to replace the duplicate occurrences with 'He', keeping the first occurrence unchanged. Methods to Replace Duplicate Occurrences Following are the multiple ways to replace duplicate occurrences in a string − Using List comprehension Using for loop ...

Read More
Showing 4221–4230 of 25,466 articles
« Prev 1 421 422 423 424 425 2547 Next »
Advertisements