Going into space with Python

ยท

3 min read

iss Img source: the Verge

Hi 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 PyPIusing pip. It will help us make HTTP requests. If you already have it installed, please skip this step.

pip install requests

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.

import requests

# The open-notify API
url = "http://api.open-notify.org/astros.json"

response = requests.get(url)

status_code  = response.status_code
print(status_code)

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.

python3 astros.py
#200

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.

data = response.text

# get the data type we received
print(type(data))
# print it
print(data)

Output

<class 'str'>
{"number": 3, "people": [{"craft": "ISS", "name": "Chris Cassidy"}, {"craft": "ISS", "name": "Anatoly Ivanishin"}, {"craft": "ISS", "name": "Ivan Vagner"}], "message": "success"}

We're getting dataaaaaaaa! ๐Ÿ˜œ

Yes, but it is of type string. We can't do much faster with data of string type. 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;

import requests
import json

Good. Now let's convert our data into a python dictionary using json.

clean_data = json.loads(data)
print(type(clean_data))
print(clean_data)
<class 'dict'>
{'number': 3, 'people': [{'craft': 'ISS', 'name': 'Chris Cassidy'}, {'craft': 'ISS', 'name': 'Anatoly Ivanishin'}, {'craft': 'ISS', 'name': 'Ivan Vagner'}], 'message': 'success'}

Now our data is of dict type. Great!

How many astronauts are in space?

astronauts = clean_data['number']
print("Number of Austronauts =>", austronauts)

Output

Number of Austronauts => 3

Who are they?

names = json_data['people']
print('Names => ', names)
Names =>  [{'craft': 'ISS', 'name': 'Chris Cassidy'}, {'craft': 'ISS', 'name': 'Anatoly Ivanishin'}, {'craft': 'ISS', 'name': 'Ivan Vagner'}]

Well, our names are in an array() of objects. Let's unpack it

list_of_austronauts = []

for item in range(len(names)):
    list_of_austronauts.append(names[item]['name'])
for austronaut in list_of_austronauts:
    print(austronaut)

Output

Chris Cassidy
Anatoly Ivanishin
Ivan Vagner

Lastly, let's get every astroanut and their craft

craft_and_name = clean_data['people']
for name in range(len(names)):
    print('Craft:', names[name]['craft'],"\n", "Astronaut:", names[name]['name'])

Output

Craft: ISS 
 Astronaut: Chris Cassidy
Craft: ISS 
 Astronaut: Anatoly Ivanishin
Craft: ISS 
 Astronaut: Ivan Vagner

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.

Next

You should go utilize the open-notify API to get different data such as the position of the ISS in space.

Happy coding