Python math.exp() Method

Python math.exp() is a built-in function that calculates the value of any number with a power of e. This means e^n, where n is the given number. The value of e is approximately equal to 2.71828. Syntax math.exp(num) Arguments The function takes only one argument num, which we want to find exponential. Return Value The … Read more

Python math.trunc() Method

Python math.trunc() method returns the truncated integer part of a number. For example, if the number is 3.14, the math.trunc() method will return 3. If the number is -1.23, the math.trunc() method will return -1. The syntax for the math.trunc() method is as follows: math.trunc(x) Parameters x : The number to be truncated. Return value The … Read more

How to Use If, Else and Elif in Lambda Functions in Python

Python lambda function is defined without a name. Instead, the anonymous functions are defined using a lambda keyword. Syntax of Lambda function lambda arguments: expression The lambda functions can have any number of parameters but only one expression. That expression is evaluated and returned. Thus, lambda functions can be used wherever a function object is … Read more

Python isinf() Method

Python isinf() is a built-in method that finds whether a number is infinite. It accepts a number (integer, float, NaN, inf) and checks whether it is infinite. Syntax math.isinf(n) The isinf() function takes n as an argument and checks whether n is infinite. Here n can be an integer, float, double, inf, NaN, etc. Return … Read more

How to Remove Rows from DataFrame in Pandas

You can remove rows from a data frame using the following approaches. Method 1: Using the drop() method To remove single or multiple rows from a DataFrame in Pandas, you can use the drop() method by specifying the index labels of the rows you want to remove. import pandas as pd # Create a sample DataFrame … Read more

How to Add Rows in Pandas DataFrame

There are the following methods to add rows in Pandas DataFrame. Method 1: Using the Dataframe.concat() method Method 2: Using the loc[ ] indexer Method 3: Using the insert() method Method 1: Using the Pandas Dataframe.concat() The concat() method can concatenate two or more DataFrames. For example, to add the row (‘new_row’, ‘value1’, ‘value2’) to … Read more

How to Remove an Element From Dictionary in Python

To remove an element from the dictionary in Python, you can use the “del” statement or the “dict.pop()” method. Method 1: Using the “del” statement To remove an element from a dictionary using the del statement, “specify the key of the element you want to remove after the del keyword”. The element with the specified … Read more

Python Tuple count() Method

Python tuple count() is a built-in method that helps us calculate the occurrence of one element in the tuple and returns the counted number. It searches the given component of a tuple and returns how often the element has occurred in it. Syntax tuple.count(element) Return Value Python tuple count() method returns the number of occurrences … Read more

How to Compare a String in Python

To compare a string in Python, you can use the “comparison operators” such as “==, !=, <, >, <=, and >=”. These operators allow you to check if two strings are equal, not equal, or if one string is lexicographically less than or greater than the other. Example 1 str1 = “hello” str2 = “world” … Read more

How to Add to a Dictionary in Python

To add an element to a dictionary in Python, you can use the “dictionary[key] = value” expression. Example 1 disneyplus = { ‘name’: ‘Mandalorian’, ‘mvp’: ‘Baby Yoda’ } print(‘Before add’, disneyplus) disneyplus[‘season’] = 1 print(‘After add’, disneyplus) Output Before add {‘name’: ‘Mandalorian’, ‘mvp’: ‘Baby Yoda’} After add {‘name’: ‘Mandalorian’, ‘mvp’: ‘Baby Yoda’, ‘season’: 1} Example 2 … Read more