Get Subtraction of Tuples in Python

AmitDiwan
Updated on 12-Mar-2021 05:33:54

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

Pass Individual Members as Arguments to Function Using Structure Elements

Bhanu Priya
Updated on 11-Mar-2021 14:02:48

2K+ Views

There are three ways by which the values of structure can be transferred from one function to another. They are as follows −Passing individual members as arguments to function.Passing entire structure as an argument to function.Passing the address of structure as an argument to function.Now let’s see how to pass an individual member of structure elements as arguments to function.Each member is passed as an argument in the function call.They are collected independently in ordinary variables in function header.ExampleGiven below is a C program to demonstrate passing individual arguments of structure to a function − Live Demo#include struct date{    int ... Read More

Create a Class to Compute Area and Perimeter of a Circle in Python

AmitDiwan
Updated on 11-Mar-2021 12:57:17

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

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

AmitDiwan
Updated on 11-Mar-2021 12:55:26

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

Find Area of Rectangle Using Classes in Python

AmitDiwan
Updated on 11-Mar-2021 12:52:41

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

Sum of Number Digits in List using Python

AmitDiwan
Updated on 11-Mar-2021 12:49:33

3K+ Views

When it is required to sum the number of digits in a list, a simple loop and the ‘str’ 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).The ‘str’ method converts the given value into a string data type.Below is a demonstration for the same −Example Live Demomy_list = [11, 23, 41, 62, 89, 0, 10] print("The list is : ") print(my_list) my_result = [] for elem in my_list:    sum_val = 0    for digit in str(elem):       sum_val += int(digit) ... Read More

Remove Empty Tuples from a List in Python

AmitDiwan
Updated on 11-Mar-2021 12:47:59

1K+ Views

When it is required to remove empty tuples from a list of tuples, a simple loop 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 tuple basically contains tuples enclosed in a list.Below is a demonstration for the same −Example Live Demodef remove_empty(my_tuple):    my_tuple = [t for t in my_tuple if t]    return my_tuple my_tuple = [(), (), (''), (" " , " "), (45, 67, 35, 66, 74, 89, 100) , 'jane'] print("The tuple is : ") print(my_tuple) print("The method ... Read More

Sort Values of First List Using Second List in Python

AmitDiwan
Updated on 11-Mar-2021 12:46:46

544 Views

When it is required to sort the values of the first list with the help of the second list, the ‘sorted’ method and the ‘zip’ methods are 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).The ‘sorted’ method is used to sort the elements of a list.The zip method takes iterables, aggregates them into a tuple, and returns it as the result.Below is a demonstration for the same −Example Live Demodef list_sort(my_list_1, my_list_2):    zipped_list_pairs = zip(my_list_2, my_list_1)    my_result = [x for _, x in sorted(zipped_list_pairs)] ... Read More

Edit Objects Inside Tuple in Python

AmitDiwan
Updated on 11-Mar-2021 12:44:58

136 Views

When it is required to edit the objects inside a tuple, simple indexing 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).Below is a demonstration for the same −Example Live Demomy_tuple = (45, 67, [35, 66, 74], 89, 100) print("The tuple is : ") print(my_tuple) my_tuple[2][1] = 63 print("The tuple after changes is : ") print(my_tuple)OutputThe tuple is : (45, 67, [35, 66, 74], 89, 100) The tuple after changes is : (45, 67, [35, 63, 74], 89, 100)ExplanationA tuple of list is defined, and ... Read More

Python Program to Display Common Letters in Two Strings

AmitDiwan
Updated on 11-Mar-2021 12:44:01

782 Views

When it is required to display the letters common to two strings, the ‘set’ method 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.Below is a demonstration for the same −Example Live Demostring_1 = 'hey' string_2 = 'jane' print("The first string is :") print(string_1) print("The second string is :") print(string_2) my_result = list(set(string_1)|set(string_2)) print("The letters are : ") for i in my_result:    print(i)OutputThe first string is : hey The second string is : jane The letters are ... Read More

Advertisements