Found 10805 Articles for Python

Iterate over a dictionary in Python

Pavitra
Updated on 01-Jul-2020 07:15:00

204 Views

In this article, we will learn about iteration/traversal of a dictionary in Python 3.x. Or earlier.A dictionary is an unordered sequence of key-value pairs. Indices can be of any immutable type and are called keys. This is also specified within curly braces.Method 1 − Using iterables directlyExampledict_inp = {'t':'u', 't':'o', 'r':'i', 'a':'l', 's':'p', 'o':'i', 'n':'t'} # Iterate over the string for value in dict_inp:    print(value, end='')OutputtrasonMethod 2 − Using iterables for values of the dictionaryExampledict_inp = {'t':'u', 't':'o', 'r':'i', 'a':'l', 's':'p', 'o':'i', 'n':'t'} # Iterate over the string for value in dict_inp.values():    print(value, end='')OutputoilpitMethod 3 − ... Read More

Backward iteration in Python

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

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

isupper(), islower(), lower(), upper() in Python and their applications

Pavitra
Updated on 03-Jul-2020 07:51:06

1K+ Views

In this article, we will learn about isupper(), islower() ,upper() , lower() function in Python 3.x. or earlier.These are the functions that can be used on strings and related types. They are included in Python Standard Library.All the functions accept no arguments. The isupper() & islower() return boolean values whereas the upper() & lower() function returns strings either in uppercase or lowercase.Now let’s see the implementation using an exampleExamplestring = 'tutorialspoint' # checking for uppercase characters print(string.isupper()) # checking for lowercase characters print(string.islower()) # convert to uppercase characters print(string.upper()) # convert to lowercase characters print(string.lower())OutputFalse True ... Read More

Ascii() in python

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

784 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

isdisjoint() function in Python

Pavitra
Updated on 03-Jul-2020 07:53:20

129 Views

In this article, we will learn about how we can implement isdisjoint() function on set() data type. This function checks whether the sets passed as arguments have any element in common . In case any element is found, False is returned and True otherwise.The isdisjoint() function can take lists, tuples & dictionaries as input arguments in addition to set inputs. THses types are implicitly converted to set type by the Python interpreter.Syntax.isdisjoint()Return valueBoolean True/FalseNow let’s consider an illustration related to the implementationExample#declaration of the sample sets set_1 = {'t', 'u', 't', 'o', 'r', 'i', 'a', 'l'} set_2 = {'p', 'o', ... Read More

intersection_update() in Python to find common elements in n arrays

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

103 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

Intersection() function Python

Pavitra
Updated on 03-Jul-2020 07:54:35

133 Views

In this article, we will learn about intersection() function that can be performed on any given set. According to mathematics intersection means finding out common elements from the two sets.Syntax.intersection( ……..)Return Value common elements in sets passed as arguments.Exampleset_1 = {'t', 'u', 't', 'o', 'r', 'i', 'a', 'l'} set_2 = {'p', 'o', 'i', 'n', 't'} set_3 = {'t', 'u', 't'} #intersection of two sets print("set1 intersection set2 : ", set_1.intersection(set_2)) # intersection of three sets print("set1 intersection set2 intersection set3 :", set_1.intersection(set_2, set_3))Outputset1 intersection set2 : {'i', 'o', 't'} set1 intersection set2 intersection set3 : {'t'}Explanation Here a search is made ... 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

The intersection of two arrays in Python (Lambda expression and filter function )

Pavitra
Updated on 07-Aug-2019 07:32:56

511 Views

In this article, we will learn about the intersection of two arrays in Python with the help of Lambda expression and filter function.The problem is that we are given two arrays we have to find out common elements in both of them.Algorithm1. Declaring an intersection function with two arguments. 2. Now we use the lambda expression to create an inline function for selection of elements with the help of filter function checking that element is contained in both the list or not. 3. Finally, we convert all the common elements in the form of a list by the help of ... Read More

Add a row at top in pandas DataFrame

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

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