Lambda and Filter in Python Examples

Pavitra
Updated on 07-Aug-2019 08:15:51

3K+ Views

In this article, we will learn about lambda expressions and filter() functions in Python 3.x. Or earlier. These functions are present in the built-in Python standard library.What are lambda expressions?An inline function may be defined by the help of lambda expressions. A lambda expression consists of the lambda keyword followed by a comma seperated list of arguments and the expression to be evaluated using the list of arguments in the following format:Syntax Lambda arguments: expressionReturn value: The value computed by substituting arguments in the expressions.Lambda expressions are often abbreviated by the name of anonymous functions as they are not given any ... Read More

Casefold String in Python

Pradeep Elance
Updated on 07-Aug-2019 08:10:47

197 Views

This function is helpful in converting the letters of a word into lowercase. When applied to two strings it can match their values irrespective of the type up of the case of the letters.Applying casefold()The below example we apply the casefold() function to a string and the result comes out in all lower case letters.Examplestring = "BestTutorials" # print lowercase string print(" lowercase string: ", string.casefold())OutputRunning the above code gives us the following result −Lowercase String: besttutorialsComparing using casefold()We can compare two strings which have same letters but in different cases after applying the casefold() function. The result of the ... Read More

Capitalize First Letter of a Column in Pandas DataFrame

Pradeep Elance
Updated on 07-Aug-2019 08:09:11

1K+ Views

A pandas dataframe is similar to a table with rows and columns. Sometimes we may have a need of capitalizing the first letters of one column in the dataframe which can be achieved by the following methods.Creating a DataframeIn the below example we first create a dataframe with column names as Day and Subject.Exampleimport pandas as pd # A dataframe df = pd.DataFrame({'Day': ['mon', 'tue', 'wed', 'thu', 'fri'], 'Subject': ['Math', 'english', 'science', 'music', 'games']}) print(df)OutputRunning the above code gives us the following result −      Day    Subject 0     mon       Math 1     ... Read More

Callable in Python

Pradeep Elance
Updated on 07-Aug-2019 08:05:13

247 Views

The callable() function in python is part of its standard library which returns true if the object is callable and returns false if it is not.The object itself should have a call method to be callable. For example if we just declare a variable with value, it is not callable, but if we declare a function then it becomes callable.Callable ObjectBelow we declare a function which is callable. That can be verified by actually making a call to the function, as well as checking through the callable function.Exampledef func_callable():    x = 3    y = 5    z = ... Read More

Bin in Python

Pradeep Elance
Updated on 07-Aug-2019 07:57:15

1K+ Views

The bin() function converts a decimal to binary. You can use a positive or negative integer as the parameter to be converted.SyntaxBelow is the syntax of the function.bin(n) Parameters : an integer to convert Return Value : A binary string of an integer or int object. Exceptions : Raises TypeError when a float value is sent as argument.In the below example we convert a positive and a negative integer to binary. The results come out with a prefix of 0b to indicate that the number is a binary representation.Examplen = input("Enter an integer :") dec_number = int(n) bin_number = bin(dec_number) ... Read More

Backward Iteration in Python

Pradeep Elance
Updated on 07-Aug-2019 07:52:06

7K+ Views

Sometimes we need to go through the elements of a list in backward order. To achieve this we need to read the last element first and then the last but one and so on till the element at index 0. Various python programming features can be used to achieve this.Using range(N, -1, -1)We are using the range function but starting with the position -1. This value helps us read the list from the last index value also we iterate through steps of -1. In the below example we start at a position which is measured by taking the length of ... Read More

ASCII in Python

Pradeep Elance
Updated on 07-Aug-2019 07:50:08

1K+ Views

ASCII is abbreviated from of American Standard Code for Information Interchange. It is a character encoding standard. For example the ASCII value of English alphabet A is 65. Python provides this function to get the printable representation of an object. It takes one parameter which is an object that can be a list, strings, tuple etc. The output of the function is the hex representation. The non-ascii characters can be escaped using \x, \u or \U escapes.SyntaxThe syntax is −Syntax: ascii(object)ExamplePrinting strings with individual characters and multiple characters.# Individual Characters print(ascii("Ω")) print(ascii("θ")) # A string int multiple characters print(ascii("TΨtΦrial"))OutputRunning the ... Read More

Intersection Update in Python to Find Common Elements in N Arrays

Pavitra
Updated on 07-Aug-2019 07:43:42

213 Views

In this article, we will learn about the iintersection_update() in Python to find out common elements in n arrays.The problem is that we are given an array containing lists, find all common elements in given arrays?Algorithm1.Initializingres with the first list inside the array 2.Iterating through the array containing lists 3.Updating the res list by applying intersection_update() function to check the common elements. 4.Finally returning the list and display the output by the help of the print statement.Now let’s take a look at its implementationExampledef commonEle(arr): # initialize res with set(arr[0]) res = set(arr[0]) # new value will get ... Read More

Append and Extend in Python

Pradeep Elance
Updated on 07-Aug-2019 07:39:41

1K+ Views

The append() and extend() functions are used with the python list to increase its number of elements. But the two have different behaviors as shown below.append()Syntax: list_name.append(‘value’) It takes only one argument.This function appends the incoming element to the end of the list as a single new element. Even if the incoming element is itself a list, it will increase the count of the original list by only one.Examplelist = ['Mon', 'Tue', 'Wed' ] print("Existing list", list) # Append an element list.append('Thu') print("Appended one element: ", list) # Append a list list.append(['Fri', 'Sat']) print("Appended a list: ", list)OutputRunning the ... Read More

Add a Row at Top in Pandas DataFrame

Pradeep Elance
Updated on 07-Aug-2019 07:33:33

2K+ Views

In Pandas a DataFrame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. We can create a DataFrame using list, dict, series and another DataFrame. But when we want to add a new row to an already created DataFrame, it is achieved through a in-built method like append which add it at the end of the DataFrame. In this article we will find ways to add the new row DataFrame at the top of the DataFrame using some tricks involving the index of the elements in the DataFrame.ExampleLet's first create a new ... Read More

Advertisements