Basic Programming in Python - Part 4

Install a Module.

You might have heard of package managers. We have one for python and it is pip. Any third-party module that you want to use in your programs, you could use the following command:

pip install numpy

this would install numpy in your program, and you are ready to use by importing the modules as per requirement.

Modules in python:

There are n number of modules available in python. You can find a big list here. You name it, you can find it. There is guide to create your own custom modules using different tools. You can check it out here.

Working with JSON:

Let us know what is JSON object in the first place. It is a data exchange format similar to XML. JSON stands for JavaScript Object Notation. Let us look at a simple JSON object representing some personal information of an employee as follows:

{
  "name" : "Rick Vivek",
  "age": 42,
  "phone": "44223422"
}

From the above example, you can see that a JSON object is identified with curly braces around the content. The strings on the left are called keys and the elements on the right are respective values of the keys. JSON is a lightweight format compared to XML.

Let us try an example here, to create a library of books with its details.

Screenshot 2022-07-17 at 10.49.29 AM.png

Importing JSON would help you access JSON methods that can be used to manipulate JSON objects. JSON.dump() method converts a dictionary into a JSON object. From above code-snippet, printing a JSON dump would return a string. We could use this data exchange format to write content into files and read content from .json files as well.

Screenshot 2022-07-17 at 11.06.28 AM.png

Screenshot 2022-07-17 at 11.08.24 AM.png

I hope this was helpful. Thank you. Happy Learning!