Python Articles - Page 949 of 1048

Python program to extract ‘k’ bits from a given position?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

2K+ Views

This function is use to extract k bits from pos position and returns the extracted value. Here we use python slicing technique. Example Input:: number=170 K=5 Pos=2 Output=21 Algorithm Extractionbit(no, k, pos) /*user input number is stored in variable no, extracted bit is stored in variable k and the position of bit is pos. */ Step 1 : first convert the number into its binary form using bin(). Step 2 : remove the first two character. Step 3 : then extracting k bits from starting ... Read More

Python program to reverse each word in a sentence?

Yaswanth Varma
Updated on 17-Jun-2025 17:27:38

3K+ Views

The string manipulation is the common task in the python programming, especially when working with the text based data. In this article we will explore how to reverse each word in a sentence. Generally, we reverse the entire sentence, where the both characters and the word positions are flipped, but in this task we are reversing only characters within each word while maintaining the original sequence of the words. For example if the input is "Welcome", the expected output would be "emocleW". Using Python split() Method The Python split() method is used to split all the words in ... Read More

Python program to list the difference between two lists.

Yaswanth Varma
Updated on 28-Aug-2025 13:40:29

1K+ Views

In Python, Lists are one of the built-in data type used for storing collections of data. Python list is a sequence of comma separated items, enclosed in square brackets[]. They are flexible, easy to manipulate and widely used in various applications. In this article, we will explore to list the difference between two lists. Python provides the several built-in methods for achieving this. Using set.difference() Method The python set.difference() method is used to return the new set containing the elements that are present in the first set but not in any other sets provided as arguments. ... Read More

Python program to print all distinct elements of a given integer array.

Yaswanth Varma
Updated on 28-Aug-2025 13:50:01

1K+ Views

The distinct elements are the values that appears only once or uniquely in the array. When working with the array we will come across the repeated or duplicate values. In this article, we are going to print all the distinct elements of a given array. Identifying and printing these distinct elements is a common task to avoid the unexpected results. This can be achieved by using the Python built-in tools like sets and dictionaries. Using Python set() Function In this approach we are using Python set() function, which removes all the duplicate value from the list because a ... Read More

Python program to print a checkboard pattern of n*n using numpy.

Yaswanth Varma
Updated on 17-Jun-2025 17:37:02

629 Views

The checkboard pattern is the square grid composed of alternating 0s and 1s, arranged in a way that no two adjacent cells have the same value. It looks like the chessboard, where black and white squares alternate in every row and column. This kind of pattern is not only seen in chess or checkers, but also in image processing, graphics, and visualization, etc. In this article, we are going to learn how to print a checkboard pattern of n*n using numpy. Using Python numpy.indices() Method The numpy.indices() method is used to return the grid of indices with ... Read More

Python program to cyclically rotate an array by one

Yaswanth Varma
Updated on 17-Jun-2025 17:37:34

2K+ Views

The cyclic rotation of an array involves moving every element of the array one position forward, and the last element gets moved to the first position. For example, if the given array is [1, 2, 3, 4] then the array after one cyclic rotation is [4, 1, 2, 3]. In this article, we are going to learn how to cyclically rotate an array by one position using Python. Using Python insert() and pop() Methods The Python insert() method is used to insert or add an element at the specified index. The index value starts from zero. Following ... Read More

Python program to find common elements in three sorted arrays?

Yaswanth Varma
Updated on 20-Jun-2025 12:52:42

780 Views

The given task is to find the common element in three sorted arrays. Input Output Scenario Following is an input-output scenario - Input Arrays: array1 = [1001, 1004, 1007, 1009, 1015, 1020] array2 = [1002, 1004, 1009, 1013, 1015, 1022] array3 = [1000, 1004, 1009, 1015, 1021] Output: [1004, 1009, 1015] This is useful in multiple scenarios, for example, if we are filtering mutual friends of three different users in apps like Facebook or comparing product availability in three different e-commerce applications on a third-party website. We are filtering elements that are common in multiple ... Read More

Python program to reverse an array in groups of given size?

Yaswanth Varma
Updated on 17-Jun-2025 17:38:21

650 Views

In Python, Arrays are commonly used to store and manage collections of data. In this article, we are going to learn how to reverse an array in groups of a given size. For example, let's consider the array and a number k, which represents the size of the array. The goal is to break the array into smaller parts, each containing k elements, and then reverse the elements inside each part. If the elements left at the end are less than k, we will reverse them. Suppose the array is [1, 2, 3, 4, 5, 6] and k=3, then ... Read More

Python program to communicate between parent and child process using the pipe.

Yaswanth Varma
Updated on 28-Aug-2025 13:39:57

1K+ Views

Inter-process communication (IPC) is essential, When multiple processes need to share the data and work together. In Python, multiprocessing module provides various IPC mechanisms, Out of which one is the pipe. The pipe allows the data to be transferred from one process to another in a two-way (duplex) or one-way mode. It is useful when a parent process creates a child process and want them to exchange messages or data. Python multiprocessing.Pipe() Method The multiprocessing.Pipe() is a method in the Python multiprocessing module that return the pair of connection objects connected by a pipe. This pipe can be ... Read More

Python program to check the validity of a Password?

Samual Sam
Updated on 30-Jul-2019 22:30:23

2K+ Views

Here given a password, our task is to check that this Password is valid or not. Here we use re module that provide regular expression and re.search() is used for checking the validation of alphabets, digits or special characters. Algorithm Step 1: first we take an alphanumeric string as a password. Step 2: first check that this string should minimum 8 characters. Step 3: the alphabets must be between a-z. Step 4: At least one alphabet should be in Uppercase A-Z. Step 5: At least 1 number or digit between 0-9. Step 6: At least 1 character from [_ ... Read More

Advertisements