Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1336 of 3366
267 Views
In Python, tuples are used to store immutable sequence of elements. A nested tuple is a tuple that contains one or more tuples as their elements. For example: # A tuple in Python tup = ( 1, 2, 3, 4 ) # A nested tuple in Python nestedTup = ( (1, 2), (3, 4) ) You are given two nested tuples in Python, and your task is to add corresponding elements of those tuples.Consider the following example scenario:Scenario Input tuples: tup1 = ((7, 8), (3, 4), (3, 2)) tup2 = ((9, 6), (8, 2), (1, 4)) ... Read More
180 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 is a bitwise operator that performs the 'XOR' operation. It sets every bit to 1 if only one of the two bits is 1.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = ((7, 8), (3, 4), (3, 2)) my_tuple_2 = ((9, 6), (8, 2), (1, 4)) print ... Read More
252 Views
When it is required to convert a tuple to an adjacency pair dictionary, the 'dict' method, the dictionary comprehension, and slicing can be used.A dictionary stores values in the form of a (key, value) pair. The dictionary comprehension is a shorthand to iterate through the dictionary and perform operations on it.Slicing will give the values present in an iterable from a given lower index value to a given higher index value, but excludes the element at the higher index value.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = (7, 8, 3, 4, 3, 2) print ("The first tuple ... Read More
173 Views
When it is required to count the elements up to the first tuple, a simple loop, the 'isinstance' method, and the 'enumerate' method can be used.Below is a demonstration of the same −ExampleLive Demomy_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 are : ") print(count)OutputThe tuple is : (7, 8, 11, 0, (3, 4, 3), (2, 22)) The number of elements up to the first ... Read More
2K+ Views
When it is required to check for 'None' value in a tuple, the 'all' method and the generator expression can be used.The 'all' method checks to see if all the values inside an iterable are True values. If yes, it returns True, else returns False.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = (None, None, None, None, None, None, None ) print ("The tuple is : " ) print(my_tuple_1) my_result = all(elem is None for elem in my_tuple_1) print("Does the tuple contain only None values ? ") print(my_result)OutputThe tuple is : (None, None, None, None, None, ... Read More
3K+ Views
When it is required to subtract the tuples, the 'map' method and lambda function 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.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 any number of arguments. It uses the expression and returns the result of it.Below is a demonstration of the same −ExampleLive ... Read More
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 Live Demoimport math class circle_compute(): def __init__(self, my_radius): self.radius=my_radius def area_calculate(self): return math.pi*(self.radius**2) def perimeter_calculate(self): return 2*math.pi*self.radius my_result = int(input("Enter the radius ... Read More
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 −Example Live Democlass list_class(): def __init__(self): self.n=[] def add_val(self, a): return self.n.append(a) def remove_val(self, ... Read More
4K+ Views
When it is required to find the area of a rectangle using classes, object oriented method is used. Here, a class is defined, 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 of rectangle.Below is a demonstration for the same −Example Live Democlass shape_rectangle(): def __init__(self, my_length, my_breadth): self.length = my_length self.breadth = my_breadth def calculate_area(self): return self.length*self.breadth len_val = 6 bread_val = 45 print("The length of the rectangle is : ") print(len_val) print("The breadth of the rectangle ... Read More
806 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. Replacing Duplicate Occurrences in a String Following are the multiple ways to replace duplicate occurrences in a string − Using List comprehension ... Read More