Found 26504 Articles for Server Side Programming

Python Program to Compute the Value of Euler's Number e. Use the Formula: e = 1 + 1/1! + 1/2! + …… 1/n!

AmitDiwan
Updated on 19-Apr-2021 10:39:18

1K+ Views

When it is required to implement Euler’s number, a method is defined, that computes the factorial.Another method is defined that find the sum of these factorial numbers.Below is the demonstration of the same −Example Live Demodef factorial_result(n):    result = 1    for i in range(2, n + 1):       result *= i    return result def sum_result(n):    s = 0.0    for i in range(1, n + 1):       s += 1.0 / factorial_result(i)    print(s) my_value = 5 print("The value is :") print(my_value) print("The result is :") sum_result(my_value)OutputThe value is : ... Read More

Python Program to Print Numbers in a Range (1,upper) Without Using any Loops

AmitDiwan
Updated on 19-Apr-2021 10:39:00

547 Views

When it is required to print the numbers in a given range without using any loop, a method is defined, that keeps displays numbers from higher range by uniformly decrementing it by one after every print statement.Below is the demonstration of the same −Example Live Demodef print_nums(upper_num):    if(upper_num>0):       print_nums(upper_num-1)       print(upper_num) upper_lim = 6 print("The upper limit is :") print(upper_lim) print("The numbers are :") print_nums(upper_lim)OutputThe upper limit is : 6 The numbers are : 1 2 3 4 5 6ExplanationA method named ‘print_nums’ is defined.It checks if the upper limit is greater than 0.If ... Read More

Convert Nested Tuple to Custom Key Dictionary in Python

AmitDiwan
Updated on 19-Apr-2021 10:38:39

680 Views

When it is required to convert a nested tuple into a customized key dictionary, the list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_tuple = ((6, 'Will', 13), (2, 'Mark', 15), (9, 'Rob', 12)) print("Thw tuple is : ") print(my_tuple) my_result = [{'key': sub[0], 'value': sub[1], 'id': sub[2]}    for sub in my_tuple] print("The converted dictionary is : ") print(my_result)OutputThw tuple is : ((6, 'Will', 13), (2, 'Mark', 15), (9, 'Rob', 12)) The converted dictionary is : [{'key': 6, 'value': 'Will', 'id': 13}, {'key': 2, 'value': 'Mark', 'id': 15}, {'key': 9, 'value': 'Rob', ... Read More

Flatten tuple of List to tuple in Python

AmitDiwan
Updated on 19-Apr-2021 10:38:15

315 Views

When it is required to flatten tuple of a list to tuple, a method is defined, that takes input as tuple.The tuple is iterated over, and the same method is called on it over and over again until the result is obtained.Below is the demonstration of the same −Example Live Demodef flatten_tuple(my_tuple):    if isinstance(my_tuple, tuple) and len(my_tuple) == 2 and not isinstance(my_tuple[0], tuple):       my_result = [my_tuple]       return tuple(my_result)    my_result = []    for sub in my_tuple:       my_result += flatten_tuple(sub)    return tuple(my_result) my_tuple = ((35, 46), ((67, ... Read More

Python program to Order Tuples using external List

AmitDiwan
Updated on 17-Apr-2021 13:07:40

341 Views

When it is required to order the tuples using an external list, the list comprehension, and ‘dict’ method can be used.Below is the demonstration of the same −Example Live Demomy_list = [('Mark', 34), ('Will', 91), ('Rob', 23)] print("The list of tuple is : ") print(my_list) ordered_list = ['Will', 'Mark', 'Rob'] print("The ordered list is :") print(ordered_list) temp = dict(my_list) my_result = [(key, temp[key]) for key in ordered_list] print("The ordered tuple list is : ") print(my_result)OutputThe list of tuple is : [('Mark', 34), ('Will', 91), ('Rob', 23)] The ordered list is : ['Will', 'Mark', 'Rob'] The ordered tuple list is ... Read More

Remove Tuples of Length K in Python

AmitDiwan
Updated on 17-Apr-2021 13:07:20

517 Views

When it is required to remove the tuples of a specific length ‘K’, list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_list = [(32, 51), (22, 13 ), (94, 65, 77), (70, ), (80, 61, 13, 17)] print("The list is : " ) print(my_list) K = 1 print("The value of K is ") print(K) my_result = [ele for ele in my_list if len(ele) != K] print("The filtered list is : ") print(my_result)OutputThe list is : [(32, 51), (22, 13), (94, 65, 77), (70, ), (80, 61, 13, 17)] The value of K is ... Read More

All pair combinations of 2 tuples in Python

Tapas Kumar Ghosh
Updated on 11-Jul-2025 17:41:57

285 Views

Python tuples are immutable sequences that used to store collections of items. When working with two tuples, you may sometimes need to generate all possible pairs, where each pair contains one element from the first tuple and one from the second. In this article, we will learn different ways to generate all pair combinations from two tuples Following are input-output scenarios for pairing combinations of two tuples: Input: first_tuple = (1, 3) second_tuple = (7, 9) Output: [(1, 7), (1, 9), (3, 7), (3, 9), (7, 1), (7, 3), (9, 1), (9, 3)] Explanation: At index 0 of first_tuple pairs ... Read More

Extract digits from Tuple list Python

AmitDiwan
Updated on 17-Apr-2021 13:04:43

668 Views

When it is required to extract digits from a list of tuple, list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_list = [(67, 2), (34, 65), (212, 23), (17, 67), (18, )] print("The list is : ") print(my_list) N = 2 print("The value of N is ") print(N) my_result = [sub for sub in my_list if all(len(str(ele)) == N for ele in sub)] print("The extracted tuples are : " ) print(my_result)OutputThe list is : [(67, 2), (34, 65), (212, 23), (17, 67), (18, )] The value of N is 2 The extracted tuples ... Read More

Join Tuples if similar initial element in Python

AmitDiwan
Updated on 17-Apr-2021 13:04:17

513 Views

When it is required to join tuples if they contain a similar initial element, a simple ‘for’ loop and an ‘of’ condition can be used. To store elements to one list, the ‘extend’ method can be used.Below is the demonstration of the same −Example Live Demomy_list = [(43, 15), (66, 98), (64, 80), (14, 9), (47, 17)] print("The list is : ") print(my_list) my_result = [] for sub in my_list:    if my_result and my_result[-1][0] == sub[0]:       my_result[-1].extend(sub[1:])    else:       my_result.append([ele for ele in sub]) my_result = list(map(tuple, my_result)) print("The extracted elements ... Read More

Closest Pair to Kth index element in Tuple using Python

AmitDiwan
Updated on 17-Apr-2021 12:56:44

353 Views

When it is required to find the closest pair to the Kth index element in a tuple, the ‘enumerate’ method can be used along with ‘abs’ method.Below is the demonstration of the same −Example Live Demomy_list = [(5, 6), (66, 76), (21, 35), (90, 8), (9, 0)] print("The list is : ") print(my_list) my_tuple = (17, 23) print("The tuple is ") print(my_tuple) K = 2 print("The value of K has been initialized to ") print(K) min_diff, my_result = 999999999, None for idx, val in enumerate(my_list):    diff = abs(my_tuple[K - 1] - val[K - 1])    if diff ... Read More

Advertisements