Going to space with Python
''If you are going to change the world, you've got to try really different things.''
- Frances Arnold, Nobel Prize in Chemistry
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.
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.
When you are performing a HTTP
request, it should display a status code. We've assigned this status code to the status_code
variable.
Let's run astros.py
.
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. Output
We're getting dataaaaaaaa! 😜
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;
Good. Now let's convert our data into a python dictionary using json
.
Output
Now our data is of type dict
. Great!
How many astronauts are in space?
OutputWho are they?
Output
Well, our names are in an array()
of objects. Let's unpack it
Output
Lastly, let's get every astroanut and their craft. Output
That was it.
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.