Going to space with Python

Robson - | #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
view raw astros.py hosted with ❤ by GitHub

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)
view raw astros.py hosted with ❤ by GitHub

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.

python3 astros.py
#200
view raw astros.py hosted with ❤ by GitHub

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)
view raw astros.py hosted with ❤ by GitHub
Output
<class 'str'>
{"number": 3, "people": [{"craft": "ISS", "name": "Chris Cassidy"}, {"craft": "ISS", "name": "Anatoly Ivanishin"}, {"craft": "ISS", "name": "Ivan Vagner"}], "message": "success"}
view raw astros.py hosted with ❤ by GitHub

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;

import requests
import json
view raw astros.py hosted with ❤ by GitHub

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)
view raw astros.py hosted with ❤ by GitHub
Output
<class 'dict'>
{'number': 3, 'people': [{'craft': 'ISS', 'name': 'Chris Cassidy'}, {'craft': 'ISS', 'name': 'Anatoly Ivanishin'}, {'craft': 'ISS', 'name': 'Ivan Vagner'}], 'message': 'success'}
view raw astros.py hosted with ❤ by GitHub

Now our data is of type dict. Great!

How many astronauts are in space?

astronauts = clean_data['number']
print("Number of Astronauts =>", astronauts)
view raw astros.py hosted with ❤ by GitHub
Output
Number of Astronauts => 3
view raw astros.py hosted with ❤ by GitHub

Who are they?

names = json_data['people']
print('Names => ', names)
view raw astros.py hosted with ❤ by GitHub
Output
Names => [{'craft': 'ISS', 'name': 'Chris Cassidy'}, {'craft': 'ISS', 'name': 'Anatoly Ivanishin'}, {'craft': 'ISS', 'name': 'Ivan Vagner'}]
view raw astros.py hosted with ❤ by GitHub

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)
view raw astros.py hosted with ❤ by GitHub
Output
Chris Cassidy
Anatoly Ivanishin
Ivan Vagner
view raw astros.py hosted with ❤ by GitHub

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'])
view raw astros.py hosted with ❤ by GitHub
Output
Craft: ISS
Astronaut: Chris Cassidy
Craft: ISS
Astronaut: Anatoly Ivanishin
Craft: ISS
Astronaut: Ivan Vagner
view raw astros.py hosted with ❤ by GitHub

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.

Happy Coding!