
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10476 Articles for Python

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

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

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

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

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

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

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

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

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

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