Posts for Python

How to Convert Dictionary to JSON in Python

In this article, you will learn how to convert a dictionary to JSON in Python. Let’s say you have a dictionary named ‘a’. Convert Dictionary to JSON in Python In order to convert a dictionary to JSON, you can use the json.dumps() method. Note: The json.dumps() method functions by converting Python objects to JSON string.

February 26, 2021 in Code examples

How to Append List Elements in Python

In this article, you will learn how to append list elements in Python. Let’s say you have 2 lists of food. Append List Elements in Python In order to append list elements, you can use the list.extend() keyword. Note: The list.extend() keyword functions by appending the elements of supplied iterable at the end of list.

February 25, 2021 in Code examples

How to Set Column As Index in Python

In this article, you will learn how to set a column as an index in Python. Let’s say you have a DataFrame of food. Set Column As Index in Python In order to set a column as an index, you can use the DataFrame.set_index() method. Note: The DataFrame.set_index() method functions by setting the supplied column […]

February 25, 2021 in Code examples

How to Create Multidimensional Arrays in Python

In this article, you will learn how to create multidimensional arrays in Python. In this example, you will be creating 1-dimensional, 2-dimensional, and 3-dimensional arrays. Create Multidimensional Arrays in Python In order to create multidimensional arrays, you can use the numpy.random.randn() method. Note: The numpy.random.randn() method functions by creating an array of specified dimensions and […]

February 25, 2021 in Code examples

How to Get Content Of A Class in Python

In this article, you will learn how to get the content of a class in Python. Let’s say you have a class named Dog with attributes breed and origin. In order to the content of a class, you can use the dir() method. Note: The dir() method functions by returning all properties, methods, and built-in […]

February 24, 2021 in Code examples

How to Reverse A List in Python

In this article, you will learn how to reverse a list in python in Python. Let’s say you have a list named ‘a’ with value [1, 2, 3, 4, 5]. In order to reverse a list, you can use the list.reverse() method. Note: The list.reverse() method functions by reversing the elements in a list.

February 24, 2021 in Code examples

How to Square A Number in Python

In this article, you will learn how to square a number in Python. Let’s say you have an integer named ‘a’ with value 22. In order to calculate the square of a number, you can use the pow() method. Note: The pow() method functions by raising the first argument to the power of the second […]

February 23, 2021 in Code examples

How to Calculate Mean of List in Python

In this article, you will learn how to calculate the mean of a list in Python. Let’s say you have a list named ‘a’ with value [1, 2, 3, 4, 5]. In order to calculate the mean of a list, you can use the statistics.mean() method. Note: The statistics.mean() method functions by returning the mean […]

February 23, 2021 in Code examples