Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 420 of 2109
Python Program to Append, Delete and Display Elements of a List Using Classes
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 MorePython Program to Find the Area of a Rectangle Using Classes
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 MorePython - Replace duplicate Occurrence in String
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 MorePython | Remove empty tuples from a list
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 MorePython Program to Edit objects inside tuple
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 MorePython Program to Find Longest Common Substring using Dynamic Programming with Bottom-Up Approach
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 MorePython - Assign a specific value to Non Max-Min elements in Tuple
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 MorePython program to sort a list of tuples by second Item
When it is required to sort a list of tuples based on the second item, the lambda function and sorted() method can be used. 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. Anonymous function is a function which is defined without a name. 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 ...
Read MorePython Program to Print an Identity Matrix
An identity matrix is a square matrix where the diagonal elements are 1 and all other elements are 0. In Python, you can create an identity matrix using nested loops or the NumPy library. Using Nested Loops The basic approach uses two nested loops to check if the row index equals the column index ? n = 4 print("The value of n has been initialized to " + str(n)) for i in range(0, n): for j in range(0, n): if(i == j): ...
Read MorePython program to search an element in a doubly linked list
When searching for an element in a doubly linked list, we need to create a Node class with three attributes: the data, a pointer to the next node, and a pointer to the previous node. We also need a main class to manage the list operations including adding nodes, displaying nodes, and searching for specific elements. In a doubly linked list, each node has bidirectional pointers − one pointing to the next node and another to the previous node. This allows traversal in both directions, making it more flexible than a singly linked list. Node Class Structure ...
Read More