Found 10805 Articles for Python

Count frequencies of all elements in array in Python using collections module

Pradeep Elance
Updated on 07-Aug-2019 08:18:37

227 Views

As python allows duplicate elements in a list we can have one element present multiple Times. The frequency of elements in a list indicates how many times an element occurs in a list. In this article we use the Counter function of the collections module to find out the frequency of each item in a list.SyntaxSyntax: Counter(list) Where list is an iterable in pythonExampleThe below code uses the Counter() to keep track of frequency and items() to iterate over each item in the result of counter function for printing in a formatted manner.from collections import Counter list = ['Mon', 'Tue', ... Read More

Count distinct elements in an array in Python

Pradeep Elance
Updated on 07-Aug-2019 08:15:52

450 Views

In a list in Python we may have duplicate elements. When we count the length of the list we get the total length including the duplicate elements. But in this article we will see how to get the total count of the distinct elements or unique elements in a list.ExampleIn the below example we use the counter() from the collections module. In this module a Counter is a dict subclass for counting hashable objects. Counter is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. So from the original list we ... Read More

Lambda and filter in Python Examples

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

2K+ 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

Iterate over characters of a string in Python

Pavitra
Updated on 01-Jul-2020 07:10:14

1K+ Views

In this article, we will learn about iterating/ traversing over characters of a string in Python 3.x. Or earlier.The string is a collection of characters which may contain spaces, alphabets or integers. They can be accessed using indexes or via references . Some commonly implemented methods are shown below.Method 1 − The direct itertor without indexingExamplestring_inp = "tutorialspoint" # Iterate over the string for value in string_inp:    print(value, end='')Method 2 − The most common way using index based accessExamplestring_inp = "tutorialspoint" # Iterate over the string for value in range(0, len(string_inp)):    print(string_inp[value], end='')Method 3 − The ... Read More

casefold() string in Python

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

91 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

907 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

236 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

Iterate over a set in Python

Pavitra
Updated on 01-Jul-2020 07:11:50

2K+ Views

In this article, we will learn about iterating/traversing over a set in Python 3.x. Or Earlier.It is an unordered collection of objects without any duplicates. This can be done by enclosing all the elements within curly braces. We can also form sets by using type casting via a keyword “set”.Method 1 − Using iterables without indexesExampleset_inp = {'t', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't'} # Iterate over the set for value in set_inp:    print(value, end='')Method 2 − Using indexed access by converting to list typeExampleset_inp = list({'t', 'u', 't', 'o', 'r', ... Read More

Iterate over a list in Python

Pavitra
Updated on 01-Jul-2020 07:13:07

383 Views

In this article, we will learn about iterating/traversing over a list in Python 3.x. Or earlier.A list is an ordered sequence of elements. It is a non-scalar data structure and mutable in nature. A list can contain distinct data types in contrast to arrays storing elements belonging to the same data types.Method 1 − Using iterable without indexExamplelist_inp = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'p', 'o', 'i', 'n', 't'] # Iterate over the list for value in list_inp:    print(value, end='')Method 2 − Using general way via indexExamplelist_inp = ['t', 'u', 't', 'o', 'r', 'i', ... Read More

bin() in Python

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

692 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

Advertisements