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
-
Economics & Finance
Python Articles
Page 112 of 855
Python Program to Convert Binary to Gray Code
When it is required to convert binary code to gray code, a method is defined that performs the XOR operation. Gray code is a binary numeral system where two successive values differ in only one bit, making it useful in digital circuits and error correction. Below is the demonstration of the same − Understanding Gray Code Conversion The conversion from binary to Gray code follows a simple rule: The most significant bit of Gray code is the same as the binary code For other bits, the Gray code bit is the XOR of the current ...
Read MorePython Program to Generate Gray Codes using Recursion
Gray codes are binary codes where consecutive numbers differ by exactly one bit. Python can generate Gray codes using recursion by building sequences iteratively. This approach uses bit manipulation and string operations to create the complete Gray code sequence. What are Gray Codes? Gray codes (also called reflected binary codes) are sequences where adjacent values differ by only one bit position. For example, in 2-bit Gray code: 00 → 01 → 11 → 10, each step changes exactly one bit. Gray Code Generation Algorithm The algorithm starts with the base case (0, 1) and recursively builds ...
Read MorePython Program to Clear the Rightmost Set Bit of a Number
When it is required to clear the rightmost set bit of a number, the bitwise AND (&) operator can be used with a clever mathematical property. This technique involves performing n & (n-1) which effectively removes the rightmost 1 bit from the binary representation. How It Works The operation n & (n-1) works because when you subtract 1 from a number, all bits to the right of the rightmost set bit get flipped, including the rightmost set bit itself. The AND operation then clears that bit. Binary Visualization Let's see how this works with number 6 ...
Read MorePython Program to Search the Number of Times a Particular Number Occurs in a List
When it is required to search the frequency of a number in a list, Python provides multiple approaches. You can define a custom method, use the built-in count() method, or utilize dictionary-based counting with collections.Counter. Method 1: Using Custom Function Define a function that iterates through the list and counts occurrences ? def count_num(my_list, x_val): my_counter = 0 for elem in my_list: if (elem == x_val): my_counter = ...
Read MorePython Program to Determine all Pythagorean Triplets in the Range
A Pythagorean triplet is a set of three positive integers a, b, c, such that a² + b² = c². The most famous example is 3, 4, 5 because 3² + 4² = 5² (9 + 16 = 25). This program finds all Pythagorean triplets where the hypotenuse c is within a given range. Understanding Pythagorean Triplets Pythagorean triplets can be generated using Euclid's formula: a = m² - n² b = 2mn c = m² + n² Where m > n > 0, and m and n are coprime integers. Example ...
Read MorePython Program to Print Numbers in a Range (1,upper) Without Using any Loops
When it is required to print numbers in a given range without using any loop, we can use recursion. This technique involves defining a function that calls itself with a decremented value until it reaches the base case. Below is the demonstration of the same − Example def 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) Output ...
Read MoreConvert Nested Tuple to Custom Key Dictionary in Python
Converting a nested tuple to a custom key dictionary is useful when transforming structured data. We can use list comprehension to iterate through tuples and create dictionaries with custom keys. Syntax [{key1: item[0], key2: item[1], key3: item[2]} for item in nested_tuple] Example Here's how to convert a nested tuple containing employee data into dictionaries with custom keys ? my_tuple = ((6, 'Will', 13), (2, 'Mark', 15), (9, 'Rob', 12)) print("The tuple is:") print(my_tuple) my_result = [{'key': sub[0], 'value': sub[1], 'id': sub[2]} ...
Read MoreFlatten tuple of List to tuple in Python
When working with nested tuple structures, you may need to flatten them into a single level of tuples. This involves recursively processing each element until all nested tuples are converted into individual tuples at the same level. The process uses recursion to traverse through nested structures and identify base cases where tuples contain exactly two non-tuple elements. Recursive Flattening Method Here's how to implement a recursive function to flatten nested tuples ? def flatten_tuple(my_tuple): # Base case: if tuple has 2 elements and first element is not a tuple ...
Read MorePython program to Order Tuples using external List
When you need to reorder tuples based on an external list, you can use dictionary conversion and list comprehension. This technique is useful when you have key-value pairs as tuples and want to sort them according to a specific order defined in another list. Example Here's how to order tuples using an external list ? my_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 ...
Read MoreRemove Tuples of Length K in Python
When working with lists of tuples, you may need to remove tuples based on their length. Python provides several approaches to filter out tuples of a specific length K. Using List Comprehension List comprehension offers a concise way to filter tuples by length ? my_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:", K) my_result = [ele for ele in my_list if len(ele) != K] print("The filtered list is:") print(my_result) The ...
Read More