''If you are going to change the world, you've got to try really different things.''
- Frances Arnold, Nobel Prize in Chemistry
Img source: the Verge
Hello gorgeous Friends!
I love Python 😉
If you are just starting out as a beginner programmer, I highly recommend you to learn Python first. It is super easy to learn and beginner friendly.
Python has a great plethora of libraries, modules and frameworks that allows you to do just what you want with ease. Even going to space. 😊
In this article, we are doing to use the requests library and the open-notify API to find out which and how many astronauts are in space. Ready? Let's dive in...
First, we have to install the requests module from PyPI using pip. It will help us make HTTP requests. If you already have it installed, please skip this step.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Create a new python file and name it whatever you want. I'll name mine astros.py
Inside our file, we are going to type this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When you are performing a HTTPrequest, it should display a status code. We've assigned this status code to the status_code variable.
Let's run astros.py.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A 200 response status code means that our request to the server has been successfully received. To learn more about HTTP status codes check out MDN Docs.
Alright, so far so good! Let's get data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Yes, but it is of type string. We can't do much faster with data of type string. Well, there is another python module that will help us handle this. It is called json. Go and box this code import json just below or before where you wrote import requests in your file. It should look like this;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Good. Now let's convert our data into a python dictionary using json.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Well, our names are in an array() of objects. Let's unpack it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Lastly, let's get every astroanut and their craft.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We've just learned how to use python's requests library to perform HTTP requests. We've also learned how to use json to convert data of type string into a dictionary which allows you to access it easily within our code.
This article is also available on Hashnode. Signup for Hashnode.
Next
You should go utilize the open-notify API to get different data such as the position of the `ISS` in space.