AmitDiwan has Published 10744 Articles

Python program to find Tuples with positive elements in List of tuples

AmitDiwan

AmitDiwan

Updated on 15-Apr-2021 13:20:44

600 Views

When it is required to find tuples that have position elements from a list of tuples, list comprehension can be used.Below is a demonstration of the same −Example Live Demomy_list = [(56, 43), (-31, 21, 23), (51, -65, 26), (24, 56)] print("The list is : ") print(my_list) my_result = ... Read More

Python program to find tuples which have all elements divisible by K from a list of tuples

AmitDiwan

AmitDiwan

Updated on 15-Apr-2021 13:20:04

889 Views

When it is required to find tuples that have elements that are divisible by a specific element ‘K’, the list comprehension can be used.Below is a demonstration of the same −Example Live Demomy_list = [(45, 90, 135), (71, 92, 26), (2, 67, 98)] print("The list is : ") print(my_list) ... Read More

Python program to Sort Tuples by their Maximum element

AmitDiwan

AmitDiwan

Updated on 15-Apr-2021 13:19:45

307 Views

When it is required to sort the tuples based on the maximum element in it, a method is defined that uses the ‘max’ method to return the highest element.Next the ‘sort’ method can be used to sort the list based on the previously defined function.Below is a demonstration of the ... Read More

Consecutive Nth column Difference in Tuple List using Python

AmitDiwan

AmitDiwan

Updated on 15-Apr-2021 13:19:27

195 Views

When it is required to find the consecutive column difference in a list of tuple, it can be iterated over, and the ‘abs’ method and the ‘append’ method can be used.The ‘abs’ method returns the absolute or positive value, and the append adds elements to a list.Below is a demonstration ... Read More

Remove Tuples from the List having every element as None in Python

AmitDiwan

AmitDiwan

Updated on 15-Apr-2021 13:15:53

365 Views

When it is required to remove tuples from a list of tuples where a ‘None’ element is present, a list comprehension can be used.Below is a demonstration of the same −Example Live Demomy_list = [(2, None, 12), (None, None, None), (23, 64), (121, 13), (None, ), (None, 45, 6)] print("The ... Read More

Trim tuples by N elements in Python

AmitDiwan

AmitDiwan

Updated on 15-Apr-2021 13:15:35

213 Views

When it is required to trim a list of tuples by a specific number of elements, the ‘del’ operator can be used.Below is a demonstration of the same −Example Live Demomy_list = [(1, 2, 11), (99, 76, 34, 89), (3.08, 11.56), ("Hi", "Will"), ("Rob", 'Ron')] n = 2 print("The list is ... Read More

Find number of times every day occurs in a Year in Python

AmitDiwan

AmitDiwan

Updated on 15-Apr-2021 13:13:41

617 Views

When it is required to find the number of times every day of the week occurs in a year, a list is defined, and it is iterated over, and is count is incremented respectively.Below is a demonstration of the same −Example Live Demoimport math def num_of_occurrence( n, firstday):    my_days ... Read More

Convert date string to timestamp in Python

AmitDiwan

AmitDiwan

Updated on 15-Apr-2021 13:12:49

1K+ Views

When it is required to convert a string into a timestamp, the ‘mktime’ method is used. This method is present in the ‘time’ package.Below is a demonstration of the same −Example Live Demoimport time import datetime my_string = "24/03/2021" print("The date string is :") print(my_string) print("The timestamp is :") print(time.mktime(datetime.datetime.strptime(my_string, ... Read More

Python Program to Create a Lap Timer

AmitDiwan

AmitDiwan

Updated on 15-Apr-2021 13:07:41

600 Views

When it is required to create a lap timer using Python, the ‘time’ method is used. The number of laps is predefined, and a try catch block is defined, to start the lap timer.Below is a demonstration of the same −Example Live Demoimport time start_time=time.time() end_time=start_time lap_num=1 print("Click on ... Read More

Python program to find difference between current time and given time

AmitDiwan

AmitDiwan

Updated on 15-Apr-2021 13:06:06

397 Views

When it is required to find the difference between the current time and a given time, a method can be defined, that takes the hours, minutes, and seconds as parameter. It then calculates the difference between two given times.Below is a demonstration of the same −Example Live Demodef difference_time(h_1, m_1, h_2, ... Read More

Advertisements