Creating Currency Converter | Python
pic credit: Christine Roy (unsplash)

Creating Currency Converter | Python

A currency converter could be really useful to anyone who needs to monitor currency fluctuations and it's measuring. In the online world, you could find a lot of currency converters out there but if you need a personalized converter for your pacific needs, you need to consider building one. by using python and APIs, you could do it very easily.

In this article, I have described how you could build a simple currency converter using Python and a free API. At the end of this article you will know:

  1. What is an API?
  2. How you could simply use it using python!
  3. How to build a custom currency converter.

What is an API: As all of you know API is the application programming interface. It helps computer programs to communicate and share data. It is like a railway track using which data can be transferred from one station to another station.

Through API the data always transferred in the JSON (JavaScript Object Notation) format. to use that data, you need to convert it to a suitable format and then use it.

How you could simply use it using python: In python, there is a very simple library called request. To get the data, you just need to use the .get method and then convert it to python dictionary data type using the .json method. You could also use Python's own JSON package .loads method for the same. I will explain to them later but first I would like to show you what I have made using Python and API.

Currency Converter: https://colab.research.google.com/drive/1MMhHoEqPm7JcJSeNep5E2RCYGnzXLzCA?usp=sharing

Historical Currency Data extractor: https://colab.research.google.com/drive/1BChPXnD7QcEogolV7bNATivUbu1t5zWp?usp=sharing

Please make a copy in your google drive and then run to play around with the converter. [open URL > file > Save a copy in Drive]

How to build a custom currency converter: To create the converter I have used the open-source Exchange rates API (https://exchangeratesapi.io/). It is pretty simple to use. You just need to give the base URL, add some parameters and you will get the exact response to play around with. For example, to get the latest or historical currency value, you need to add the desired data, base currency like USD or POUND, desired currency value like INR and that's it. You will get the response. After this, you can extract the data from this response using a request.Json method or Python .loads method.

Always check if the response status code is 200. If it is not, you will not get anything out of it. Please check the below code here

import requests


def currency_converter_1():

old_or_latest = input("Do you want the latest or historical value?\nif latest, please type 'latest' or else type historical value in '%Y-%m-%d' format:\t")

base_URL_new = 'https://api.exchangeratesapi.io/'+old_or_latest

response = requests.get(base_URL_new, params='base=USD')

if response.status_code == 200:
   data,base_value, Date = response.json().items()
   print('Available Currency\n>>', data[1].keys())
   desired_currency = input("Please provide the INR or othercurrency")
   print(f'{desired_currency}:{data[1]   [desired_currency]} | base:{base_value[1]} | date:{Date[1]}')

convert = input('Do you want to convert the '+ desired_currency+ ' value to USD?\t  yes or no: ')
            if convert == 'yes':
                value = float(input("value\n>>>"))
                USD_value = value/float(data[1][desired_currency])
                print(f'The value is:\t ${USD_value}')
            else:
                print("\nThank you")
        else:
            print ("\nPlease give the correct data")
    
#Here is a ordering issue, please check the code in Collab first

As you can see first I have structured the base_URL_new and then get the response using .get method. After that, just converted it to a python dictionary using .json() and getting the desired data. It is pretty simple, isn't it?

Whenever you are trying to get the periodic historical data, there is a one-trick you need to apply which is .dumps method. For historical data, you will not get the data in a structured way day by day as the JSON data is not structured. To get the ordered data, you need to convert the JSON to a string (using dumps method), sorting it, and then again convert it to a python dictionary to get the data.

data = json.dumps(response.json(), sort_keys=True)
data1 = json.loads(data)

#please see the Historical Currency Value converter for full code

Here data is the ordered string and data1 is the ordered dictionary. After getting the data, I just play around with the data1 and resulted in the desired output.

I hope you have liked the article and converters. By the way, .get is only applicable for free APIs or open-source APIs. For paid APIs, you need to just replace .get method with .post method. I hope in the future, I will come up with another application with .post method to demonstrate it to you.

Please like, share. If you have any questions, suggestions, please share it with me down in the comment below.

要查看或添加评论,请登录

Soumyabrata Roy的更多文章

  • Offline BI Dashboard You can send through Email

    Offline BI Dashboard You can send through Email

    Today data is the new oil. People who have data are always is in a competitive advantage over their peers.

  • Diabetes Predictor ML App

    Diabetes Predictor ML App

    Hello all. just see the diabetes predictor application in action.

  • What is Data Analysis? My intake from Google Data analysis course

    What is Data Analysis? My intake from Google Data analysis course

    I got the opportunity to take the new google data analytics course. This is the gist of my understanding of the data…

  • Some cool Data Science tricks, most of Pandas

    Some cool Data Science tricks, most of Pandas

    This week I have learned quite a few data tricks which I would like to share with you all. It might help you in your…

  • Create your own VE with Python

    Create your own VE with Python

    VE (virtual environment) could really help you in executing your products or services with different requirements. You…

    5 条评论
  • Automate the Google search using Python

    Automate the Google search using Python

    Whenever we need help, we take simple help from Google. With 5.

    2 条评论
  • Tableau the Best Tool for Data Visualization

    Tableau the Best Tool for Data Visualization

    We all know the data is important. Today millions of data are created every second and if we can analyze it, use it for…

    2 条评论
  • My Data Science Week 8

    My Data Science Week 8

    Ok. now you know the theory behind Binomial distribution, Poissons distribution from my previous article.

  • My Data Science Week 7

    My Data Science Week 7

    Hi There. In this week you will find more about the different distributions in Probability and about the…

  • My Data Science Week 6

    My Data Science Week 6

    Hi there, how are you doing today? Today I'm going to tell you more about the data and how you can use it to find out…

社区洞察

其他会员也浏览了