Found 10784 Articles for Python

How to Align Text Strings using Python?

Vikram Chiluka
Updated on 24-Jan-2023 19:02:30

25K+ Views

In this article, we will learn how to align text strings using Python. We will be using f-strings for this aligning text strings in python. Python's Text Alignment feature is helpful for printing output that is well and cleanly formatted. Sometimes the length of the data to be printed varies, making it appear untidy when printed. By specifying the alignment as left, right, or center and the space (width) to reserve for the string, the output string can be aligned using String Alignment. The text will be formatted using the f-strings. The output string's alignment is defined by the symbols ... Read More

Finding unique elements from Tuple in Python

Vikram Chiluka
Updated on 24-Jan-2023 09:28:50

1K+ Views

In this article, we will learn a Python program to find unique elements from a tuple. Methods Used The following are the various methods to accomplish this task − Using for loop and append() function Using collections.Counter() function Using set() function Tuples are an immutable, unordered data type used to store collections in Python. It can store multiple values in it. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable. Method 1: Using for loop and ... Read More

Find the maximum element of each row in a matrix using Python

Vikram Chiluka
Updated on 23-Jan-2023 14:28:16

2K+ Views

In this article, we will learn a python program to find the maximum element of each row in a matrix. Assume we have taken an NxN input matrix. We will now find the maximum element of each row in an input matrix using the below methods. Methos Used The following are the various methods to accomplish this task − Using Nested For Loops Using max() function Using map(), lambda functions Method 1: Using Nested For Loops Algorithm (Steps) Following are the Algorithm/steps to be followed to perform the desired task. − Create a function maximumRowElement() to print ... Read More

Find the least frequent element in an array using Python

Vikram Chiluka
Updated on 23-Jan-2023 14:25:33

684 Views

In this article, we will learn the python program for getting the least frequent element in an array. Methods Used The following are the various methods to accomplish this task − Using sort() function(Naive Approach) Using Hashing Using Counter() Function Method 1: Using sort() function(Naive Approach) Running two loops is a simple fix. The outer loop selects each element one by one. The inner loop calculates the frequency of the selected element and compares it to the least value reached thus far. This solution's time complexity is O(n^2) Sorting is a better solution. The array is sorted first, ... Read More

Decoding and encoding hexadecimal digitals using Python

Vikram Chiluka
Updated on 23-Jan-2023 14:23:43

3K+ Views

In this article, we will learn how to decode and encode hexadecimal digits using python. Methods Used Using binascii module Using the base64 module Method 1: Using Binascii Module There are several methods to convert between binary and different ASCII-encoded binary representations in the binascii module. Use the binascii module if all you need to do is encode or decode a raw string of hex digits. Algorithm (Steps) Following are the Algorithms/steps to be followed to perform the desired task. − Use the import keyword to import the binascii module. Create a variable to store the ... Read More

Decimal Equivalents of 1/2, 1/3, . . . ,1/10 in Python

Vikram Chiluka
Updated on 23-Jan-2023 14:12:17

1K+ Views

In this article, we will learn a python program that prints out the decimal equivalents of 1/2, 1/3, 1/4, . . . ,1/10. Methods Used The following are the various methods to accomplish this task − Printing Decimal Equivalent of 1/2 Using the range() function Using the decimal module Method 1: Printing Decimal Equivalent of 1/2 We can easily use this code to find the decimal equivalents of 1/2. In this code, we just display the output after storing the result of 1/2 in a variable, inputNumber. Example The following program returns the decimal equivalents of 1/2 − ... Read More

Computing sums of diagonals of a matrix using Python

Vikram Chiluka
Updated on 23-Jan-2023 14:10:23

2K+ Views

In this article, we will learn a python program to efficiently compute sums of diagonals of a matrix. Methos Used The following are the various methods to accomplish this task − Using Nested For Loops Using Only Single Loop In a matrix we have two diagonals − Principal diagonal Secondary diagonal Example Let us take a 3x3 matrix as shown below − A00 A01 A02 A10 A11 A12 A20 A21 A22 Principal Diagonal Condition − The row-column condition is row = column. The principal diagonal is formed by A00, A11, and ... Read More

Check if the matrix is lower triangular using Python

Vikram Chiluka
Updated on 23-Jan-2023 14:07:40

253 Views

In this article, we will learn a python program to check if a matrix is lower triangular. Assume we have taken an input matrix. We will now check whether the input matrix is lower triangular using the below methods. Methos Used The following are the various methods to accomplish this task − Using nested for loop What is a Lower Triangular Matrix? If all the entries above the main/principal diagonal are zero, a square matrix is said to be lower triangular. Example 1 0 0 0 2 3 0 0 1 3 7 0 0 ... Read More

Check if a number in a list is perfect square using Python

Vikram Chiluka
Updated on 23-Jan-2023 14:03:46

2K+ Views

In this article, we will learn a Python program to check if a number is a Perfect Square in a List. What is a Perfect Square? A perfect square is an integer that can be written as the square of another integer. It is referred to as the product of some integer with itself in other words. Example 25 = 5x5 Methods Used The following are the various methods to accomplish this task − Using List comprehension & math module Using For Loop & math module Using ** Operator Using the filter() and lambda functions The sqrt() ... Read More

Check if a matrix is symmetric using Python

Vikram Chiluka
Updated on 23-Jan-2023 14:01:28

2K+ Views

In this article, we will learn a python program to check if a matrix is symmetric. What is a Symmetric Matrix? If the transpose of a square matrix matches the original matrix, the matrix is said to be symmetric. By changing from row to column and from column to row, the symmetric matrix may be obtained. For Example 2 6 8 6 1 3 8 3 9 Assume we have taken an NxN input matrix. We will now check whether the matrix is symmetric or not using the below methods. Methos Used The following ... Read More

Advertisements