How to Use numpy.bmat() Method in Python

Numpy.bmat() method is “used to create a matrix from a string, nested sequence, or array representation of the matrix.“ Syntax numpy.bmat(obj, ldict=None, gdict=None) Parameters obj: (string or array_like): Input data to be built in the matrix. If a string, variables may be referenced by name in the current scope. ldict:(dict, optional): The dictionary replaces the … Read more

How to Use the numpy.diagflat() Method

Numpy.diagflat() method is “used to create a two-dimensional array with the flattened input as a diagonal”. Syntax numpy.diagflat(v, k=0)  Parameters v: (array_like): Input data is flattened and set as the kth diagonal of the output. k:(int, optional): Diagonal is by default set to 0, corresponding to the “main diagonal”; the value of k (either positive … Read more

What is the Merge Operator in Python(Introduced in Python3.9)

The merge operator is a new feature in Python 3.9 that allows you to “merge two dictionaries”. The syntax of the merge operator is a “pipe character (|)”. Syntax dict1 | dict2 Operands dict1: Current dictionary object dict2: Another dictionary object Return value It returns the merged dictionary as a dict instance. The “merge operator” creates a new … Read more

Python Set isdisjoint() Method

Python Set isdisjoint() method “returns True if none of the elements are present in both sets; otherwise, False.” For example, if A={1,2,3} and B={5,7,9} are two sets, we can see that there is no common element in them, so they are known as disjoint. See the following figure. Syntax set1.isdisjoint(set2) Parameters Here, set1 is one set1, and … Read more

Python String swapcase() Method

Python string swapcase() method is “used to convert all the characters to their opposite letter case(uppercase to lowercase and vice versa) and returns the string.” Syntax string.swapcase()  Here, the string variable holds the input string on which the swapcase() method is applied. Parameters The swapcase() method doesn’t take any parameters. If any parameter is passed, it … Read more

Python Update Operator (|=)

Python update operator (|=) is “used to perform an in-place union operation between two sets or dictionaries. It updates the left operand with the union of itself and the right operand.” For sets, the |= operator adds all elements from the right operand (another set) that are not present in the left operand (the original … Read more

What is Python 0 in String Formatting

The format code {0} is replaced by the first argument of format() i.e 12 , while {1} is replaced by the second argument of format() i.e. 31. Python 0 is an indicator of the format method that you need to be replaced by the format’s first (index zero) parameter. It is used to execute a string … Read more

Python String isnumeric() Method

Python string isnumeric() method is “used to check if all the characters in the string are numeric”. It returns True if all the characters in the string are numeric and false in all other cases. Syntax string.isnumeric() Here, the string must be checked if it is numeric or not. Parameters The method isnumeric() and doesn’t contain … Read more

Python Set intersection_update() Method

Python set intersection_update() method is “used to remove the element that is not present in both sets (or in all sets if the comparison is done between more than two sets)”. Syntax set.intersection_update(set1, set2, …) Parameters set1: It is the set with which we find an intersection. set2: The other set to search for equal … Read more

What is the numpy.tri() Method

Numpy tri() method “returns an array with ones at and below the k-th diagonal and zeros elsewhere, where k is the given parameter”. Syntax numpy.tri(rows, columns, k, dtype) Parameters The tri() function takes four parameters, out of which three parameters are optional.  rows: It represents the number of rows. columns: It is the number of columns; … Read more

Python Set symmetric_difference_update() Method

Python Set symmetric_difference_update() method is “used to update the original set by removing elements present in both sets and inserting the other elements“. Syntax A.symmetric_difference_update(B)  Parameters B: The set to check for matches in. Return Value The symmetric_difference_update() method returns None. But it updates the value of the calling set with the difference between the two … Read more

What is the numpy.nanargmin() Method

Numpy.nanargmin() method “returns the indices of the minimum values in the specified axis, ignoring NaNs”. For all-NaN slices, ValueError is raised. Warning: The results cannot be trusted if a slice contains only NaNs and Infs. Syntax numpy.nanargmin(arr, axis=None) Parameters The NumPy nanargmin() function takes two arguments as a parameter: arr: The array from which we … Read more

What is the numpy.exp2() Method in Python

The numpy.exp2() method is “used to calculate element-wise 2**x for all x in the input array”. Syntax numpy.exp2(input_array, out = None, where = True, **kwargs) Parameters input_array: The array containing the input values for which we must find the exponential values. out: It is the output array that is placed with the result. where: It is … Read more

How to Use the numpy.iscomplex() Method

Numpy.iscomplex() method is “used to test element-wise whether it is a complex number or not(not infinity or not Not a Number) and returns the result as a boolean array”. Syntax numpy.iscomplex(input_array)  Parameters input_array: It is the input array or the input for which we want to check whether it is a complex number. Return Value … Read more

Python String expandtabs() Method

Python String expandtabs() method is “used to set the tab size to the specified number of whitespace”. Syntax string.expandtabs(size) Parameters size: It takes only one parameter, which is the size that specifies the number of spaces we want to give in place of the “\t” symbol. Return Value It returns the string in which space … Read more

Python math.asinh() Function

Python math.asinh() method returns the “inverse hyperbolic sine of a number”. Syntax math.asinh(var) Here, var is the variable of which hyperbolic sine arc we must find. Parameters var: It takes values of the numeric datatype and throws TypeError if the argument of any other data type is passed. Return Value It returns the hyperbolic arc … Read more

Python String isdecimal() Method

Python String isdecimal() method “returns True if all characters in a string are decimal characters. If not, it returns False.” Syntax string.isdecimal() Parameters The isdecimal() function doesn’t take any parameters. Therefore, an error will be shown if we try to pass any parameter to the method. Return value It returns True if the string consists only … Read more

Python Set issuperset() Method

Python Set issuperset() method “returns True if all elements in the specified set exist in the original set; otherwise, it returns False”. Consider this image, and there are two sets, A={1,2,4} and B={1,2,3,4,5,}. So we can see that all the elements of set A are present in set B. So here, B is called the … Read more

What is the numpy.logspace() Method

Numpy.logspace() method “returns number spaces evenly with respect to the interval on a log scale.“ It is helpful when generating an array of numbers with a logarithmic distribution. Syntax numpy.logspace(start, stop, num = 50, endpoint = True, base = 10.0, dtype = None) Parameters start: The base ** start is the starting value of the sequence. … Read more

Python Set issubset() Method

Python Set issubset() method “returns True if set A is the subset of B, i.e. if all the elements of set A are present in set B. Else, it returns False.” Syntax First_Set.issubset(Second_Set) Parameters Second_Set: Any other set to compare with. Return Value The function returns two types of values- True: If First_Set is a … Read more

Python math.expm1() Method

Python math.expm1() method “returns Ex – 1″. The ‘E’ is the base of the natural system of logarithms (approximately 2.718282), and x is the number passed to it. Syntax math.expm1(num) Parameters num: It is required and specifies the exponent. Return Value Python math expm1() method returns the floating type number by calculating e**n (e^n)-1 The expm1() Function … Read more

Python String isidentifier() Method

Python String isidentifier() method “returns True if the string is a valid identifier, otherwise False”. The string is considered a valid identifier if it only contains the alphanumeric letters (a-z) and (0-9) or underscores (_). In addition, the valid identifier cannot start with a number or contain any spaces. Syntax string.isidentifer() Parameters The string method … Read more

Python String istitle() Method

Python String istitle() method “returns True if all words in a text start with an upper case letter, and the rest are lower case letters; otherwise, False”. Syntax str.istitle() Parameters None. Return Value The istitle() function returns True if the string is title-cased and returns False in all other cases. Example 1: How to Use … Read more

Python String isspace() Method

Python string isspace() method “returns True if all the characters in a string are whitespaces, otherwise False”. This function is used to check if the argument contains all whitespace characters, such as: ‘ ‘ – Space ‘\t’ – Horizontal tab ‘\n’ – Newline ‘\v’ – Vertical tab ‘\f’ – Feed ‘\r’ – Carriage return Syntax … Read more

How to Add List to Set in Python

Here are three ways to add a list to set in Python: Set.update(): It adds a list to a set. Using | operator: The “|” operator adds elements to the Set. Using for loop: The add() function can’t add a list to the Set, but you can add one list element to the Set by using for … Read more

Python math.atanh() Method

Python math.atanh() method “returns the inverse hyperbolic tangent of a number.” Syntax math.atanh(var) Here, var is a variable of which hyperbolic tangent arc we must find. Parameters It takes one parameter var, which takes values of numeric datatype and throws TypeError if an argument of any other data type is passed. Return Value It returns a … Read more

Python String rindex() Method

Python rindex() method is “used to find the last occurrence of the specified value”. The rindex() method raises an exception if the value is not found. Syntax string.rindex(sub, start, end)  Parameters The rindex() function takes three arguments. sub: It’s the substring that needs to be searched in the given string. start: Starting position where the … Read more

Python math.ldexp() Method

Python math.ldexp() method “returns the value of x*(2**i) in floating numbers, and it is the inverse of the frexp() function”. Syntax math.ldexp(x, i) Parameters The ldexp() function takes two arguments: x: Any valid number can be positive or negative. i:  Any valid number can be positive or negative. Return Value The ldexp() function returns a … Read more

Python math.lgamma() Method

Python math.lgamma() method is “used to calculate the natural logarithm gamma value of a number”. Syntax math.lgamma(var) Parameters var: It brings values of numeric datatype and throws TypeError if the argument of any other data type is passed. Return Value It returns the natural log value of the gamma function value of the number in … Read more

Python “not in” Operator

Python “not in” operator is “used to check the presence of a specified value inside a given sequence, but its return values are opposite to that of the ‘in’ operator.” How to Use “not in” operator listA = [11, 21, 29, 46, 19] stringA = “Hello! This is AppDividend” tupleA = (11, 22, 33, 44) … Read more

What is the numpy.linalg.slogdet() Method

Numpy.linalg.slogdet() method is “used to calculate the sign and (natural) logarithm of the determinant of an array”. Syntax numpy.linalg.slogdet(array) Parameters The slogdet() function takes a single parameter, array: This is a 2D array that must be square.  Return Value The slogdet() function returns two values, sign:  A number represents the sign of the determinant. For … Read more

Python String rsplit() Method

Python string rsplit() method is “used to split a string into a list, starting from the right by the separator as a delimiter”. Syntax str.rsplit(separator, max)  Here str is the string variable that holds the main string on which we will use the rsplit() method. Parameter separator: It specifies the separator to use when splitting … Read more

Python os.path.commonprefix() Method

Python os.path.commonprefix() method is “used to get the longest common path prefix in a list of paths”. It returns only the common prefix value in the specified list; the returned value may or may not be a valid path as it checks for common prefixes by comparing character by character in the list. Syntax os.path.commonprefix(path_list) … Read more

Python Tuple index() Method

Python tuple index() method “returns the first occurrence of the given element from the tuple.“ Syntax tuple.index(element) Parameters Here, the tuple is the name of the tuple, and the element is the value whose index is to be found. Return Value The tuple index() method returns the index/position of the given element. If the element … Read more

How to Use the numpy.isrealobj() Method

Numpy.isrealobj() method is “used to check if an array is of a complex type or if the array contains any complex number”. If there is no complex number or the value of the imaginary part to the array elements is zero, it returns True otherwise, False. Syntax numpy.isrealobj(input array/value)  Parameters input array/value: It takes one … Read more

How to Use the numpy.isfortran() Method

Numpy isfortran() method is “used to check if the array is Fortran contiguous but not C contiguous”. There are three types of orders for it. C-contiguous order means that operating row-rise on the array will be quicker. The Second-order is Fortran or F-contiguous in memory, meaning column-wise operations will be faster. The third and final order … Read more

Python os.path.commonpath() Method

Python os.path.commonpath() method is “used to get the longest common sub-path in a list of paths.” It raises ValueError if the specified list of paths either contains both absolute and relative paths or is empty. Syntax os.path.commonpath(list) Parameters list: It is a list of path-like objects. A path-like object is either a string or bytes object … Read more

How to Use the numpy.isposinf() Method

The numpy.isposinf() method is “used to test element-wise whether it is positive infinity or not and returns the boolean array”. Syntax numpy.isposinf(array or the scalar value, out(output array))  Parameters The isposinf() function takes two parameters, out of which 1 is optional. input array: It is an input for which we want to check whether it … Read more

Python String isprintable() Method

Python string isprintable() method “returns True if all the characters are printable, otherwise, False”. A string is printable if it contains alphabets(Uppercase and Lowercase), digits, punctuation, or space. Syntax string.isprintable() Here, the string is the variable to be checked if it is printable. Parameters The isprintable() method doesn’t contain any parameter and throws an error … Read more

How to Use numpy.isneginf() Method

The numpy.isneginf() method is “used to test element-wise whether it is negative infinity or not, and returns the boolean array.” Syntax numpy.isneginf(array or the scalar value, out(output array)) Parameters The Numpy isneginf() function takes two parameters, out of which one is optional. input array: It is an input that we want to check whether it is -ve … Read more

How to Extract String between Two Substrings

Here are the methods to extract string between two substrings in Python: Using index() + loop Using a string slicing and string.index() Using re.search() method Using find()+ string slicing Using replace() and split() methods Using split() and join() methods Method 1: Using index() + loop main_string = “Homer Simpson is the best” print(“The original string … Read more

What is the Integer and Float Division in Python

The division function in Python has two variations: Float division: It returns the decimal value. Integer division returns the whole numbers (the division result is rounded to the nearest whole number). Float division ( / ) To divide float values in Python, you can use the “/” operator. The Division operator “/” takes two parameters and … Read more

Python String Concatenation [6 Easy Ways]

Here are the 6 ways to concatenate strings in Python: Using the “+” operator Using the “join()” method Using “% operator” Using format() function Using , (comma) Using “f-string” Method 1: Using the + operator The + operator concatenates two or more strings. When used between two strings, it combines them into a single string. … Read more

Python Program to Merge Two Dictionaries

Here are three ways to merge two dictionaries: Using the dict.update() method Using the ** operator Using the | operator Method 1: Using the dict.update() method To merge two dictionaries in Python, you can use the “dict.update()” method. The dict.update() method is used to update the dictionary with the items from another dictionary. It returns None, … Read more

Python File readlines() Method

Python file readlines() method is “used to read all the lines in one go and then return them as each line a string element in a list.“ Syntax file.readlines(hint) Parameters hint: It is an optional parameter: the number of bytes returned exceeds the hint number; no more lines will be returned. The default value is  … Read more

How to Delete File If Exists in Python

Here are two ways to delete a file if it exists in Python. Using the “os.remove()” along with “os.path.exists()” functions Using the “os.ulink()” method Method 1: Using the os.remove() along with os.path.exists() function To delete a file if it exists in Python, you can use the os.remove() function along with the os.path.exists() to check if the … Read more

How to Convert Int to Char in Python

To convert int to char in Python, you can use the “chr()” method. For example, the “chr(97)” expression returns “a”. The chr() method returns a character (a string) from an integer (it represents the Unicode code point of the character). Syntax chr(i) Parameters i: The chr() method takes a single parameter which is an integer. … Read more

How to Convert Int to Binary String in Python

Here are five ways to convert an int to a binary string in Python. Using the “bin()” function Using the “format()” method Using the “string.format()” function Using “f-strings” Using “custom function” Method 1: Using the bin() method To convert int to binary in Python, use the bin() method. The bin() method is used to convert a decimal … Read more

How to Remove Punctuation from a String in Python

Here are the ways to remove punctuation from a string in Python: Using “str.translate()” and “str.maketrans()” methods Using a “for loop” Using “regex” Using the “not in” operator Method 1: Using str.translate() and str.maketrans() methods To remove punctuation from a string in Python, you can use the “str.translate()” method along with a translation table created using … Read more

Python grep: How to Search a File Using Grep in Python

Python doesn’t have a built-in grep() function, but you can achieve similar functionality using the “re module” (Python’s regular expressions library) and list comprehensions or generator expressions. GREP stands for “Globally search for Regular Expression and Print matching lines”. The grep is a command-line utility in Unix-like systems for searching plain-text data sets for lines matching … Read more