Sign in
Log inSign up
Going to space with Python

Going to space with Python

Robson's photo
Robson
Ā·Sep 8, 2020Ā·

4 min read

Robson - 03-09-2020 | #python

If you are going to change the world, you've got to try really different things. Frances Arnold, Nobel Prize in Chemistry

iss 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.

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 = "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(data)
<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 data! šŸ’Ŗ

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 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 Astronauts =>", astronauts)

Output

Number of Astronauts => 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_astronauts = []

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

Output

Chris Cassidy
Anatoly Ivanishin
Ivan Vagner

Lastly, let's get every astronaut 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.

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

Happy coding

Made with ā¤ by Robson