Found 10476 Articles for Python

Find the difference of the sum of list elements that are missing from Matrix and vice versa in python

Vikram Chiluka
Updated on 27-Jan-2023 11:26:23

196 Views

In this article, we will learn how to find the difference of the sum of list elements that are missing from Matrix and vice versa. Methods Used The following are the various methods to accomplish this task − Using for loop & from_iterable() function Using sum() & from_iterable() functions Using the Counter() function Example Assume we have taken an input matrix and a target list. We will now find the difference of the sum of list elements that are missing from Matrix and vice versa. Input inputMatrix = [[6, 3, 2], [5, 4, 1], [10, 8, 1], [15, ... Read More

Case-insensitive string replacement using Python Program

Vikram Chiluka
Updated on 27-Jan-2023 11:20:58

5K+ Views

In this article, we will learn Case insensitive string replacement in python. Methods Used The following are the various methods to accomplish this task − Using re.IGNORECASE, re.escape(), re.sub() Using re.sub(), lambda, re.escape() functions Using split(), lower() & replace() functions Using split(), list() and join() functions Method 1: Using re.IGNORECASE, re.escape(), re.sub() re.compile() function A regular expression pattern can be combined with pattern objects, which can then be used for pattern matching. This function also allows searching for a pattern again without rewriting it. Syntax re.compile(pattern, repl, string) re.sub() function The string with replaced ... Read More

Interchange elements of the first and last rows in the matrix using Python

Vikram Chiluka
Updated on 27-Jan-2023 13:43:56

1K+ Views

In this article, we will learn a python program to interchange elements of the first and last rows in a matrix. Methos Used Using Temporary Variable(Using Nested Loops) Using Swapping(“, ”) Operator(Without Using Any Loops) Using pop(), insert() & append() functions Method 1: Using Temporary Variable(Using Nested Loops) Algorithm (Steps) Following are the Algorithms/steps to be followed to perform the desired task. − Create a function swapFirstandLastRow() to swap the first and last rows of an input matrix by accepting the input matrix, rows, and columns as arguments. Traverse through the matrix rows and columns and Swap ... Read More

Interchange Diagonals of Matrix using Python

Vikram Chiluka
Updated on 27-Jan-2023 13:40:13

666 Views

In this article, we will learn a python program to interchange the diagonals of the matrix. Assume we have taken an NxN input matrix. We will now interchange the diagonals of an input matrix using the below methods. Methos Used The following are the various methods to accomplish this task − Using the Nested For Loops with Temporary Variable Using ‘, ’ Swapping Operator Algorithm (Steps) Following are the Algorithms/steps to be followed to perform the desired task. − Create a variable to store the input number of rows of a matrix Create a function printGivenMatrix() that ... Read More

How to remove the last element from a set in Python?

Vikram Chiluka
Updated on 27-Jan-2023 13:38:08

9K+ Views

In this article, we will learn how to remove the last element from the set in Python. Methods Used The following are the various methods to accomplish this task − Using the discard() function Using the remove() function Using the pop() function Using the list slicing Method 1: Using the discard() function The discard() function removes the element from the set, bypassing the element as an argument to it. Algorithm (Steps) Following are the Algorithms/steps to be followed to perform the desired task. − Create a variable to store the input set containing integers. Use the discard() ... Read More

How to perform accurate Decimal Calculations using Python?

Vikram Chiluka
Updated on 27-Jan-2023 13:36:36

1K+ Views

In this article, we will learn how to perform Accurate Decimal Calculations in Python. Methods Used Using the Decimal() function of the decimal Module Using the fsum() function of the math module The inability of floating-point numbers to accurately represent all base-10 numbers is a well-known drawback. Furthermore, even straightforward mathematical computations have few errors. For instance − Example The following program shows the inability of floating-point integers to express all base-10 numbers accurately − x = 4.2 y = 3.1 # printing the sum of both the variables print("x + y =", x + ... Read More

How to Manipulating Pathnames using Python?

Vikram Chiluka
Updated on 27-Jan-2023 13:34:09

2K+ Views

In this article, we will learn Manipulating Pathnames using Python. Here are some different examples used mentioned below − Getting the main filename from the file path Getting the directory name from the file path Joining path components together Expanding the User's Home Directory Splitting the file extension from the file path Algorithm (Steps) Following are the Algorithms/steps to be followed to perform the desired task. − Use the import keyword to import the os module. Create a variable to store the input file path. Use the basename() function(returns the base name from the given ... Read More

How to Iterate over Tuples in Dictionary using Python

Vikram Chiluka
Updated on 27-Jan-2023 13:31:22

3K+ Views

In this article, we will learn how to iterate over the tuples in Dictionary in python. Methods Used The following are the various methods used to accomplish this task − Using Indexing Using dictionary.values() Method Using dictionary.items() Method Tuples are an immutable, unordered data type used to store collections in Python. 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 Indexing len() function − The number of items in an object is returned ... Read More

How to get the file name from the file path in Python?

Vikram Chiluka
Updated on 27-Jan-2023 13:25:13

9K+ Views

In this article, we will learn a python program to get the file name from the file Path. Methods Used The following are the various methods to accomplish this task − Using the OS module functions Using Pathlib Module Using Regular expressions(regex module) Method 1: Using the OS Module Functions Get the File Name From the File Path using the split() Function The split() function splits a string into a list. We can define the separator; the default separator is any whitespace. Algorithm (Steps) Following are the Algorithms/steps to be followed to perform the desired task. − ... Read More

How to Get a Negation of a Boolean in Python?

Vikram Chiluka
Updated on 27-Jan-2023 13:22:23

7K+ Views

In this article, we will learn how to get a negation of a Boolean in Python. In Python, the Boolean datatype is the built-in data type. It denotes the True or False values. For example, 520 is False. In this article, we will print the negation of a Boolean variable. The following are the various methods to accomplish this task − Using the “~” operator Using “not” Operator Using Operator Module Using Minus the value from ‘1’ Using bitwise_not(), logical_not() of Numpy Module Method 1: Using the “~” operator The negation of the operand can be returned using ... Read More

Advertisements