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
Python Articles
Page 14 of 854
Python Program to Create a Lap Timer
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 −Exampleimport time start_time=time.time() end_time=start_time lap_num=1 print("Click on ENTER to count laps.Press CTRL+C to stop") try: while True: input() time_laps=round((time.time() - end_time), 2) tot_time=round((time.time() - start_time), 2) print("Lap No. "+str(lap_num)) print("Total Time: "+str(tot_time)) print("Lap Time: "+str(time_laps)) ...
Read MoreFind number of times every day occurs in a Year in Python
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 −Exampleimport math def num_of_occurrence( n, firstday): my_days = [ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" ] my_count= [4 for i in range(0, 7)] my_position = -1 for i in range(0, 7): if (first_day == my_days[i]): my_position = i break ...
Read MoreTrim tuples by N elements in Python
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 −Examplemy_list = [(1, 2, 11), (99, 76, 34, 89), (3.08, 11.56), ("Hi", "Will"), ("Rob", 'Ron')] n = 2 print("The list is :") print(my_list) print("The value of N is") print(n) del my_list[n] print("The list after deleting N elements is :") print(my_list)OutputThe list is : [(1, 2, 11), (99, 76, 34, 89), (3.08, 11.56), ('Hi', 'Will'), ('Rob', 'Ron')] The value of N is 2 The list after deleting N elements is : [(1, 2, ...
Read MoreRemove Tuples from the List having every element as None in Python
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 −Examplemy_list = [(2, None, 12), (None, None, None), (23, 64), (121, 13), (None, ), (None, 45, 6)] print("The list is : ") print(my_list) my_result = [sub for sub in my_list if not all(elem == None for elem in sub)] print("The None tuples have been removed, the result is : " ) print(my_result)OutputThe list is : [(2, None, 12), (None, None, None), (23, 64), (121, 13), (None, ), ...
Read MoreConsecutive Nth column Difference in Tuple List using Python
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 of the same −Examplemy_list = [(67, 89, 32), (11, 23, 44), (65, 75, 88)] print("The list is : ") print(my_list) print("The value of k has been initialized") K = 1 my_result = [] for idx in range(0, len(my_list) - 1): my_result.append(abs(my_list[idx][K] - my_list[idx + 1][K])) ...
Read MorePython program to Sort Tuples by their Maximum element
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 same −Exampledef get_max_value(my_val): return max(my_val) my_list = [(4, 6, 8, 1), (13, 21, 42, 56), (7, 1, 9, 0), (1, 2)] print(“The list is : “) print(my_list) my_list.sort(key = get_max_value, reverse = True) print(“The sorted tuples are : “) print(my_list)OutputThe list is : [(4, 6, 8, ...
Read MorePython program to find tuples which have all elements divisible by K from a list of tuples
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 −Examplemy_list = [(45, 90, 135), (71, 92, 26), (2, 67, 98)] print("The list is : ") print(my_list) K = 45 print("The value of K has been initialized to ") print(K) my_result = [sub for sub in my_list if all(ele % K == 0 for ele in sub)] print("Elements divisible by K are : " + str(my_result))OutputThe list is : [(45, 90, 135), (71, 92, 26), (2, 67, ...
Read MorePython program to find Tuples with positive elements in List of tuples
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 −Examplemy_list = [(56, 43), (-31, 21, 23), (51, -65, 26), (24, 56)] print("The list is : ") print(my_list) my_result = [sub for sub in my_list if all(elem >= 0 for elem in sub)] print("The positive elements are : ") print(my_result)OutputThe list is : [(56, 43), (-31, 21, 23), (51, -65, 26), (24, 56)] The positive elements are : [(56, 43), (24, 56)]ExplanationA list of tuples is defined, and is displayed ...
Read MorePython Program to Convert a given Singly Linked List to a Circular List
When it is required to convert a singly linked list into a circular linked list, a method named ‘convert_to_circular_list’ is defined that ensures that the last elements points to the first element, thereby making it circular in nature.Below is a demonstration of the same −Exampleclass Node: def __init__(self, data): self.data = data self.next = None class LinkedList_struct: def __init__(self): self.head = None self.last_node = None def add_elements(self, data): if self.last_node is None: self.head = Node(data) ...
Read MorePython Program to Implement a Stack
When it is required to implement a stack using Python, a stack class is created, and an instance of this class is created. Methods to push, pop elements are defined and the instance is used to call these methods.Below is a demonstration of the same −Exampleclass Stack_struct: def __init__(self): self.items = [] def check_empty(self): return self.items == [] def add_elements(self, my_data): self.items.append(my_data) def delete_elements(self): return self.items.pop() my_instance = Stack_struct() while True: print('Push ') print('Pop') print('Quit') my_input ...
Read More