Found 10476 Articles for Python

Python Program to Clear the Rightmost Set Bit of a Number

AmitDiwan
Updated on 19-Apr-2021 10:48:49

420 Views

When it is required to clear the rightmost bit of a number which was previously set, the ‘&’ operator can be used.Below is the demonstration of the same −Example Live Demodef clear_right_bit(my_val):    return my_val & (my_val-1) n_val = 6 print("The vlaue of n is :") print(n_val) print("The number after unsetting the rightmost set bit is ") print(clear_right_bit(n_val))OutputThe vlaue of n is : 6 The number after unsetting the rightmost set bit is 4ExplanationA method is defined that takes an integer as a parameter.It computes the ‘&’ operation between the number and the number decremented by 1.Outside the method, an integer ... Read More

Python Program to Search the Number of Times a Particular Number Occurs in a List

AmitDiwan
Updated on 19-Apr-2021 10:48:29

389 Views

When it is required to search the frequency of a number in a list, a method is defined, that takes a list and the number. It iterates through the list, and every time the number is encountered, the counter is incremented.Below is the demonstration of the same −Example Live Demodef count_num(my_list, x_val):    my_counter = 0    for elem in my_list:       if (elem == x_val):          my_counter = my_counter + 1    return my_counter my_list = [ 66, 26, 48, 140, 66, 20, 1, 96, 86] print("The list is :") print(my_list) occ_number = 66 ... Read More

Python Program to Determine all Pythagorean Triplets in the Range

AmitDiwan
Updated on 19-Apr-2021 10:40:17

2K+ Views

When it is required to determine the Pythagorean triplets within a given range, a method is defined, that helps calculate the triplet values.Below is the demonstration of the same −Example Live Demodef pythagorean_triplets(limits) :    c, m = 0, 2    while c < limits :    for n in range(1, m) :       a = m * m - n * n       b = 2 * m * n       c = m * m + n * n       if c > limits :          break   ... Read More

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

553 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

683 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

321 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

343 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

520 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

294 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

Advertisements